From 6d4a01f66f81d1bd74779f2c822fa56767478e52 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sun, 31 May 2026 02:52:04 -0700 Subject: [PATCH] fix: parse empty markdown front matter --- .../editor/markdown-frontmatter.test.ts | 20 +++++++++++++++++++ .../components/editor/markdown-frontmatter.ts | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/renderer/src/components/editor/markdown-frontmatter.test.ts b/src/renderer/src/components/editor/markdown-frontmatter.test.ts index f021a815128..e7875093f1a 100644 --- a/src/renderer/src/components/editor/markdown-frontmatter.test.ts +++ b/src/renderer/src/components/editor/markdown-frontmatter.test.ts @@ -49,6 +49,26 @@ describe('extractFrontMatter', () => { expect(result!.raw).toBe('---\n\n---\n') expect(result!.body).toBe('# Body\n') }) + + it('handles empty front-matter block without a blank line', () => { + const content = '---\n---\n# Body\n' + const result = extractFrontMatter(content) + expect(result).not.toBeNull() + expect(result!.raw).toBe('---\n---\n') + expect(result!.body).toBe('# Body\n') + }) + + it('handles empty TOML front-matter block without a blank line', () => { + const content = '+++\n+++\nBody\n' + const result = extractFrontMatter(content) + expect(result).not.toBeNull() + expect(result!.raw).toBe('+++\n+++\n') + expect(result!.body).toBe('Body\n') + }) + + it('does not treat same-line delimiter text as a closing delimiter', () => { + expect(extractFrontMatter('---\ntitle: Hello---\n# Body\n')).toBeNull() + }) }) describe('prependFrontMatter', () => { diff --git a/src/renderer/src/components/editor/markdown-frontmatter.ts b/src/renderer/src/components/editor/markdown-frontmatter.ts index 6bb6d793e2f..101981549f7 100644 --- a/src/renderer/src/components/editor/markdown-frontmatter.ts +++ b/src/renderer/src/components/editor/markdown-frontmatter.ts @@ -1,4 +1,4 @@ -const FRONTMATTER_RE = /^(---|\+\+\+)\r?\n([\s\S]*?)\r?\n\1(?:\r?\n|$)/ +const FRONTMATTER_RE = /^(---|\+\+\+)\r?\n(?:[\s\S]*?\r?\n)?\1(?:\r?\n|$)/ export type FrontMatter = { /** The full raw front-matter block including delimiters and trailing newline. */