From 5dd3b98fec99836b5563019f6918580edd18c8a9 Mon Sep 17 00:00:00 2001 From: Elias Stiffel Date: Sat, 23 May 2026 13:29:19 +0200 Subject: [PATCH] fix: avoid duplicate H1 when template begins with one `buildNoteBody` unconditionally prepended an empty `# ` placeholder whenever `initialEmptyHeading` was true, which collided with templates whose first line is already an H1 (e.g. a Weekly Notes type whose template starts with `# Woche 2026.21`). The placeholder is now suppressed when the template's first non-whitespace line is `# `. Fixes: https://github.com/refactoringhq/tolaria/issues/736 Co-Authored-By: Claude Opus 4.7 (1M context) --- src/hooks/useNoteCreation.test.ts | 22 ++++++++++++++++++++++ src/hooks/useNoteCreation.ts | 3 ++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/hooks/useNoteCreation.test.ts b/src/hooks/useNoteCreation.test.ts index 1616b948..5994ebee 100644 --- a/src/hooks/useNoteCreation.test.ts +++ b/src/hooks/useNoteCreation.test.ts @@ -148,6 +148,28 @@ describe('buildNoteContent', () => { }) expect(content).toBe('---\ntype: Project\nstatus: Active\n---\n\n# \n\n## Objective\n\n') }) + + it('skips the empty H1 when the template already starts with one', () => { + const content = buildNoteContent({ + title: null, + type: 'Weekly', + status: null, + template: '# Woche 2026.21\n\nWochennotiz\n', + initialEmptyHeading: true, + }) + expect(content).toBe('---\ntype: Weekly\n---\n\n# Woche 2026.21\n\nWochennotiz\n') + }) + + it('skips the empty H1 when the template starts with an H1 after leading whitespace', () => { + const content = buildNoteContent({ + title: null, + type: 'Weekly', + status: null, + template: '\n\n# Woche 2026.21\n', + initialEmptyHeading: true, + }) + expect(content).toBe('---\ntype: Weekly\n---\n\n\n\n# Woche 2026.21\n') + }) }) describe('resolveNewNote', () => { diff --git a/src/hooks/useNoteCreation.ts b/src/hooks/useNoteCreation.ts index be023ff7..07ced057 100644 --- a/src/hooks/useNoteCreation.ts +++ b/src/hooks/useNoteCreation.ts @@ -117,7 +117,8 @@ export interface TypeInstanceDefault { } function buildNoteBody({ template, initialEmptyHeading }: Pick): string { - if (initialEmptyHeading) { + const templateStartsWithH1 = template?.trimStart().startsWith('# ') ?? false + if (initialEmptyHeading && !templateStartsWithH1) { return template ? `\n# \n\n${template}` : '\n# \n\n' } return template ? `\n${template}` : ''