Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/formatters/captureChoiceFormatter-frontmatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,3 +534,40 @@ describe('CaptureChoiceFormatter insert after inline', () => {
expect(reportError).toHaveBeenCalled();
});
});

describe('CaptureChoiceFormatter append task newline regression (issue #124)', () => {
beforeEach(() => {
vi.resetAllMocks();
(global as any).navigator = {
clipboard: {
readText: vi.fn().mockResolvedValue(''),
},
};
});

it('inserts a newline before an appended task when the file does not end with a newline', async () => {
const app = createMockApp();
const plugin = {
settings: {
enableTemplatePropertyTypes: false,
globalVariables: {},
showCaptureNotification: false,
showInputCancellationNotification: true,
},
} as any;
const formatter = new CaptureChoiceFormatter(app, plugin);

const choice = createChoice({ prepend: true, task: true });
const file = createTFile('Test.md');
const fileContent = '- [ ] Old task';

const result = await formatter.formatContentWithFile(
'- [ ] New task\n',
choice,
fileContent,
file,
);

expect(result).toBe('- [ ] Old task\n- [ ] New task\n');
});
});
11 changes: 9 additions & 2 deletions src/formatters/captureChoiceFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,16 @@ export class CaptureChoiceFormatter extends CompleteFormatter {
if (formattedContentIsEmpty) return this.fileContent;

if (this.choice.prepend) {
// When appending to the end of a file, ensure the capture starts on a new line.
// Notes are not guaranteed to end with a trailing newline (see issue #124).
const shouldInsertLinebreak = !this.choice.task;
return `${this.fileContent}${shouldInsertLinebreak ? "\n" : ""
}${formatted}`;
const needsLeadingNewline =
this.fileContent.length > 0 &&
!this.fileContent.endsWith("\n") &&
!formatted.startsWith("\n");
const separator = shouldInsertLinebreak || needsLeadingNewline ? "\n" : "";

return `${this.fileContent}${separator}${formatted}`;
}

if (this.choice.insertAfter.enabled) {
Expand Down