Skip to content

Commit a4b4e4a

Browse files
authored
fix: add newline when appending tasks (#1102)
1 parent 0dd209d commit a4b4e4a

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

src/formatters/captureChoiceFormatter-frontmatter.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,3 +534,40 @@ describe('CaptureChoiceFormatter insert after inline', () => {
534534
expect(reportError).toHaveBeenCalled();
535535
});
536536
});
537+
538+
describe('CaptureChoiceFormatter append task newline regression (issue #124)', () => {
539+
beforeEach(() => {
540+
vi.resetAllMocks();
541+
(global as any).navigator = {
542+
clipboard: {
543+
readText: vi.fn().mockResolvedValue(''),
544+
},
545+
};
546+
});
547+
548+
it('inserts a newline before an appended task when the file does not end with a newline', async () => {
549+
const app = createMockApp();
550+
const plugin = {
551+
settings: {
552+
enableTemplatePropertyTypes: false,
553+
globalVariables: {},
554+
showCaptureNotification: false,
555+
showInputCancellationNotification: true,
556+
},
557+
} as any;
558+
const formatter = new CaptureChoiceFormatter(app, plugin);
559+
560+
const choice = createChoice({ prepend: true, task: true });
561+
const file = createTFile('Test.md');
562+
const fileContent = '- [ ] Old task';
563+
564+
const result = await formatter.formatContentWithFile(
565+
'- [ ] New task\n',
566+
choice,
567+
fileContent,
568+
file,
569+
);
570+
571+
expect(result).toBe('- [ ] Old task\n- [ ] New task\n');
572+
});
573+
});

src/formatters/captureChoiceFormatter.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,16 @@ export class CaptureChoiceFormatter extends CompleteFormatter {
120120
if (formattedContentIsEmpty) return this.fileContent;
121121

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

128135
if (this.choice.insertAfter.enabled) {

0 commit comments

Comments
 (0)