diff --git a/src/formatters/captureChoiceFormatter-frontmatter.test.ts b/src/formatters/captureChoiceFormatter-frontmatter.test.ts index 4bbd6422..69e79e13 100644 --- a/src/formatters/captureChoiceFormatter-frontmatter.test.ts +++ b/src/formatters/captureChoiceFormatter-frontmatter.test.ts @@ -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'); + }); +}); diff --git a/src/formatters/captureChoiceFormatter.ts b/src/formatters/captureChoiceFormatter.ts index f5391824..438e4c39 100644 --- a/src/formatters/captureChoiceFormatter.ts +++ b/src/formatters/captureChoiceFormatter.ts @@ -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) {