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. */