From 3a7e0a552d9a4794e264611b4316938b3202a553 Mon Sep 17 00:00:00 2001 From: onmax Date: Thu, 5 Nov 2026 23:47:57 +0100 Subject: [PATCH 1/4] fix(attributes): keep an unclosed brace group as text `a {b c` turned into paragraph attributes `:b` and `:c`, so the text after the brace was lost. `searchProps` now reports whether it found the closing `}`, and the attributes rule treats an unclosed group as text. Part of #525 (case 25). --- .../attributes/paragraph-unclosed-brace.md | 77 +++++++++++++++++++ .../comark/src/internal/parse/syntax/props.ts | 3 + packages/comark/src/plugins/attributes.ts | 10 +++ 3 files changed, 90 insertions(+) create mode 100644 packages/comark/SPEC/COMARK/attributes/paragraph-unclosed-brace.md diff --git a/packages/comark/SPEC/COMARK/attributes/paragraph-unclosed-brace.md b/packages/comark/SPEC/COMARK/attributes/paragraph-unclosed-brace.md new file mode 100644 index 000000000..094965d87 --- /dev/null +++ b/packages/comark/SPEC/COMARK/attributes/paragraph-unclosed-brace.md @@ -0,0 +1,77 @@ +## Input + +```md +a {b c + +x **y** {b c d + +# Title {.red + +- item {#id +``` + +## AST + +```json +{ + "frontmatter": {}, + "meta": {}, + "nodes": [ + [ + "p", + {}, + "a {b c" + ], + [ + "p", + {}, + "x ", + [ + "strong", + {}, + "y" + ], + " {b c d" + ], + [ + "h1", + { + "id": "title-red" + }, + "Title {.red" + ], + [ + "ul", + {}, + [ + "li", + {}, + "item {#id" + ] + ] + ] +} +``` + +## HTML + +```html +

a {b c

+

x y {b c d

+

Title {.red

+ +``` + +## Markdown + +```md +a \{b c + +x **y** \{b c d + +# Title \{.red + +- item \{#id +``` diff --git a/packages/comark/src/internal/parse/syntax/props.ts b/packages/comark/src/internal/parse/syntax/props.ts index 155cef15f..abfeb7e8a 100644 --- a/packages/comark/src/internal/parse/syntax/props.ts +++ b/packages/comark/src/internal/parse/syntax/props.ts @@ -23,6 +23,7 @@ export function searchProps(content: string, index = 0) { if (content[index] !== '{') throw new Error(`Invalid props, expected \`{\` but got '${content[index]}'`) const props: [string, string][] = [] + let closed = false // Skip Vue mustache `{{ }}` syntax if (content[index + 1] === '{') return undefined @@ -34,6 +35,7 @@ export function searchProps(content: string, index = 0) { index += 2 } else if (content[index] === '}') { index += 1 + closed = true break } else if (content[index] === ' ') { index += 1 @@ -124,5 +126,6 @@ export function searchProps(content: string, index = 0) { return { props, index, + closed, } } diff --git a/packages/comark/src/plugins/attributes.ts b/packages/comark/src/plugins/attributes.ts index c783837b6..af852397f 100644 --- a/packages/comark/src/plugins/attributes.ts +++ b/packages/comark/src/plugins/attributes.ts @@ -22,6 +22,9 @@ const markdownItInlineProps: PluginSimple = (md) => { // after text inside it would be rejected too, so it is skipped without // rescanning the same suffix (keeps O(n)). const rejectedUntil = new WeakMap() + // Start of the first brace group without a closing `}`, per inline state. Its + // scan ran to the end of the source, so a later `{` is text too (keeps O(n)). + const unclosedFrom = new WeakMap() // Set while re-tokenizing a restored brace group: the end of that source is // not the end of the line, since text followed the group in the original. let restoring = false @@ -46,8 +49,15 @@ const markdownItInlineProps: PluginSimple = (md) => { lastDelimiter.token === state.tokens.length - 1 if (afterText && !afterCloser && start < (rejectedUntil.get(state) ?? -1)) return false + if (start > (unclosedFrom.get(state) ?? Infinity)) return false + const search = searchProps(state.src, start) if (!search) return false + // An unclosed group, such as `a {b c`, is text + if (!search.closed) { + unclosedFrom.set(state, Math.min(start, unclosedFrom.get(state) ?? Infinity)) + return false + } const { props, index: end } = search if (end === start) return false From 2ac844f04b450f11e0074285330f6e7db72dfb9b Mon Sep 17 00:00:00 2001 From: onmax Date: Thu, 5 Nov 2026 23:47:58 +0100 Subject: [PATCH 2/4] fix(frontmatter): close only on a bare `---` line and report invalid YAML Frontmatter closed at any line that starts with `---`, such as `--- x`, and a leading byte order mark hid the frontmatter. Invalid YAML made `parseMarkdown` throw and lose the whole document. The closing delimiter is now `---` alone on its line, with optional trailing spaces or tabs. A leading BOM is skipped. Invalid YAML logs a warning, leaves `frontmatter` empty, and the body still parses. Part of #525 (cases 26 and 27). --- ...frontmatter-closing-delimiter-with-text.md | 38 ++++++++++++++++++ .../SPEC/COMARK/frontmatter-invalid-yaml.md | 39 +++++++++++++++++++ packages/comark/src/internal/frontmatter.ts | 34 +++++++++++++--- packages/comark/test/frontmatter.test.ts | 24 ++++++++++-- test/bundle.test.ts | 2 +- 5 files changed, 127 insertions(+), 10 deletions(-) create mode 100644 packages/comark/SPEC/COMARK/frontmatter-closing-delimiter-with-text.md create mode 100644 packages/comark/SPEC/COMARK/frontmatter-invalid-yaml.md diff --git a/packages/comark/SPEC/COMARK/frontmatter-closing-delimiter-with-text.md b/packages/comark/SPEC/COMARK/frontmatter-closing-delimiter-with-text.md new file mode 100644 index 000000000..cf0cb4c5d --- /dev/null +++ b/packages/comark/SPEC/COMARK/frontmatter-closing-delimiter-with-text.md @@ -0,0 +1,38 @@ +## Input + +```md +--- +title: a +--- x +--- + +Body +``` + +## AST + +```json +{ + "frontmatter": {}, + "meta": {}, + "nodes": [ + [ + "p", + {}, + "Body" + ] + ] +} +``` + +## HTML + +```html +

Body

+``` + +## Markdown + +```md +Body +``` diff --git a/packages/comark/SPEC/COMARK/frontmatter-invalid-yaml.md b/packages/comark/SPEC/COMARK/frontmatter-invalid-yaml.md new file mode 100644 index 000000000..415e133d0 --- /dev/null +++ b/packages/comark/SPEC/COMARK/frontmatter-invalid-yaml.md @@ -0,0 +1,39 @@ +## Input + +```md +--- +title: [ +--- + +# Hi +``` + +## AST + +```json +{ + "frontmatter": {}, + "meta": {}, + "nodes": [ + [ + "h1", + { + "id": "hi" + }, + "Hi" + ] + ] +} +``` + +## HTML + +```html +

Hi

+``` + +## Markdown + +```md +# Hi +``` diff --git a/packages/comark/src/internal/frontmatter.ts b/packages/comark/src/internal/frontmatter.ts index 64d8a9195..72dab42cf 100644 --- a/packages/comark/src/internal/frontmatter.ts +++ b/packages/comark/src/internal/frontmatter.ts @@ -4,6 +4,22 @@ import { parseYaml, stringifyYaml } from './yaml.ts' const FRONTMATTER_DELIMITER_DEFAULT = '---' const LF = '\n' const CR = '\r' +const BOM = '\uFEFF' + +/** + * Find the line that closes the frontmatter: `---` alone on its line, with optional trailing spaces or tabs. + * @returns The index of the line feed before the delimiter, and the index after the delimiter line's content + */ +function findClosingDelimiter(content: string, from: number) { + let idx = content.indexOf(LF + FRONTMATTER_DELIMITER_DEFAULT, from) + while (idx !== -1) { + let end = idx + 1 + FRONTMATTER_DELIMITER_DEFAULT.length + while (content[end] === ' ' || content[end] === '\t') end++ + if (content[end] === CR) end++ + if (end === content.length || content[end] === LF) return { idx, end } + idx = content.indexOf(LF + FRONTMATTER_DELIMITER_DEFAULT, idx + 1) + } +} /** * Parse frontmatter from content @@ -13,14 +29,20 @@ const CR = '\r' export function parseFrontmatter(content: string) { let data: Record = {} let frontmatter = '' - if (content.startsWith(FRONTMATTER_DELIMITER_DEFAULT)) { - const idx = content.indexOf(LF + FRONTMATTER_DELIMITER_DEFAULT) - if (idx !== -1) { + const start = content.startsWith(BOM) ? BOM.length : 0 + if (content.startsWith(FRONTMATTER_DELIMITER_DEFAULT, start)) { + const close = findClosingDelimiter(content, start) + if (close) { + const { idx, end } = close const hasCarriageReturn = content[idx - 1] === CR - frontmatter = content.slice(4, idx - (hasCarriageReturn ? 1 : 0)) + frontmatter = content.slice(start + 4, idx - (hasCarriageReturn ? 1 : 0)) if (frontmatter) { - data = parseYaml(frontmatter) ?? {} - content = content.slice(idx + 4 + (hasCarriageReturn ? 1 : 0)) + try { + data = parseYaml(frontmatter) ?? {} + } catch (error) { + console.warn(`[comark/plugins/frontmatter] ignoring invalid YAML frontmatter: ${(error as Error).message}`) + } + content = content.slice(end) } } } diff --git a/packages/comark/test/frontmatter.test.ts b/packages/comark/test/frontmatter.test.ts index 305b80bc8..b885b06f2 100644 --- a/packages/comark/test/frontmatter.test.ts +++ b/packages/comark/test/frontmatter.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, it } from 'vitest' +import { describe, expect, it, vi } from 'vitest' import { parseFrontmatter, renderFrontmatter } from '../src/internal/frontmatter' import { createMarkdownParser, parseMarkdown } from '../src/index' @@ -126,13 +126,31 @@ Content` expect(result.content.trim()).toBe('Content') }) - it('should still throw for malformed YAML in frontmatter', () => { + it('should warn and keep the content for malformed YAML in frontmatter', () => { + const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}) const input = `--- foo: [1,2 --- Content` - expect(() => parseFrontmatter(input)).toThrow() + const result = parseFrontmatter(input) + expect(result.data).toEqual({}) + expect(result.content.trim()).toBe('Content') + expect(warn).toHaveBeenCalledOnce() + expect(warn.mock.calls[0][0]).toContain('invalid YAML frontmatter') + warn.mockRestore() + }) + + it('should close frontmatter at a delimiter with trailing spaces', () => { + const result = parseFrontmatter('---\ntitle: Hello\n--- \t\n\nContent') + expect(result.data).toEqual({ title: 'Hello' }) + expect(result.content.trim()).toBe('Content') + }) + + it('should find frontmatter after a byte order mark', () => { + const result = parseFrontmatter('\uFEFF---\ntitle: Hello\n---\n\nContent') + expect(result.data).toEqual({ title: 'Hello' }) + expect(result.content.trim()).toBe('Content') }) it('should handle frontmatter with special characters', () => { diff --git a/test/bundle.test.ts b/test/bundle.test.ts index 6b6931461..e2fe8c5bf 100644 --- a/test/bundle.test.ts +++ b/test/bundle.test.ts @@ -67,7 +67,7 @@ describe('package bundle size', { timeout: 60_000 }, () => { "@comark/react": "42.9k (83 files)", "@comark/svelte": "50.3k (91 files)", "@comark/vue": "60.2k (87 files)", - "comark": "515k (176 files)", + "comark": "516k (176 files)", } `) }) From 09b792b5115617e5551a86649bcc67dec08b4137 Mon Sep 17 00:00:00 2001 From: Farnabaz Date: Thu, 19 Nov 2026 14:08:43 +0100 Subject: [PATCH 3/4] refactor(frontmatter): let parseFrontmatter throw and handle invalid YAML in the plugin --- packages/comark/src/internal/frontmatter.ts | 29 ++++++++++++++------- packages/comark/src/plugins/frontmatter.ts | 11 ++++++-- packages/comark/test/frontmatter.test.ts | 17 +++++++++--- 3 files changed, 41 insertions(+), 16 deletions(-) diff --git a/packages/comark/src/internal/frontmatter.ts b/packages/comark/src/internal/frontmatter.ts index d566734b5..cf1866812 100644 --- a/packages/comark/src/internal/frontmatter.ts +++ b/packages/comark/src/internal/frontmatter.ts @@ -21,9 +21,8 @@ function findClosingDelimiter(content: string, from: number) { } } -/** Extract leading YAML frontmatter (`---`) from markdown content. */ -export function parseFrontmatter(content: string) { - let data: Record = {} +/** Split leading frontmatter (`---`) from markdown content without parsing its YAML. */ +export function splitFrontmatter(content: string): { content: string; frontmatterText: string } { let frontmatter = '' const start = content.startsWith(BOM) ? BOM.length : 0 if (content.startsWith(FRONTMATTER_DELIMITER_DEFAULT, start)) { @@ -33,20 +32,30 @@ export function parseFrontmatter(content: string) { const hasCarriageReturn = content[idx - 1] === CR frontmatter = content.slice(start + 4, idx - (hasCarriageReturn ? 1 : 0)) if (frontmatter) { - try { - data = parseYaml(frontmatter) ?? {} - } catch (error) { - console.warn(`[comark/plugins/frontmatter] ignoring invalid YAML frontmatter: ${(error as Error).message}`) - } content = content.slice(end) } } } + return { content, frontmatterText: frontmatter } +} + +/** + * Extract leading YAML frontmatter (`---`) from markdown content. + * @throws When the frontmatter is not valid YAML + */ +export function parseFrontmatter(content: string): { + content: string + data: Record + frontmatterText: string +} { + const split = splitFrontmatter(content) + const data: Record = split.frontmatterText ? (parseYaml(split.frontmatterText) ?? {}) : {} + return { - content, + content: split.content, data, - frontmatterText: frontmatter, + frontmatterText: split.frontmatterText, } } diff --git a/packages/comark/src/plugins/frontmatter.ts b/packages/comark/src/plugins/frontmatter.ts index 05d3703eb..2df252662 100644 --- a/packages/comark/src/plugins/frontmatter.ts +++ b/packages/comark/src/plugins/frontmatter.ts @@ -19,12 +19,19 @@ */ import { defineComarkPlugin } from '../utils/helpers.ts' -import { parseFrontmatter } from '../internal/frontmatter.ts' +import { parseFrontmatter, splitFrontmatter } from '../internal/frontmatter.ts' export default defineComarkPlugin(() => ({ name: 'frontmatter', pre(state) { - const { content, data, frontmatterText } = parseFrontmatter(state.markdown) + let result: ReturnType + try { + result = parseFrontmatter(state.markdown) + } catch (error) { + console.warn(`[comark/plugins/frontmatter] ignoring invalid YAML frontmatter: ${(error as Error).message}`) + result = { ...splitFrontmatter(state.markdown), data: {} } + } + const { content, data, frontmatterText } = result state.markdown = content state.frontmatter = data state.frontmatterText = frontmatterText diff --git a/packages/comark/test/frontmatter.test.ts b/packages/comark/test/frontmatter.test.ts index b885b06f2..5ae986318 100644 --- a/packages/comark/test/frontmatter.test.ts +++ b/packages/comark/test/frontmatter.test.ts @@ -126,16 +126,25 @@ Content` expect(result.content.trim()).toBe('Content') }) - it('should warn and keep the content for malformed YAML in frontmatter', () => { + it('should throw for malformed YAML in frontmatter', () => { + const input = `--- +foo: [1,2 +--- + +Content` + expect(() => parseFrontmatter(input)).toThrow() + }) + + it('should warn and strip malformed YAML frontmatter in the frontmatter plugin', async () => { const warn = vi.spyOn(console, 'warn').mockImplementation(() => {}) const input = `--- foo: [1,2 --- Content` - const result = parseFrontmatter(input) - expect(result.data).toEqual({}) - expect(result.content.trim()).toBe('Content') + const result = await parseMarkdown(input) + expect(result.frontmatter).toEqual({}) + expect(result.nodes).toEqual([['p', {}, 'Content']]) expect(warn).toHaveBeenCalledOnce() expect(warn.mock.calls[0][0]).toContain('invalid YAML frontmatter') warn.mockRestore() From e03c07cfc10c30b14cc6e1ea0cbeff07bd720ed7 Mon Sep 17 00:00:00 2001 From: Farnabaz Date: Thu, 19 Nov 2026 14:10:14 +0100 Subject: [PATCH 4/4] fix(auto-close): recognize frontmatter opener after a byte order mark --- packages/comark/src/auto-close.ts | 12 +++++++++++- packages/comark/test/auto-close.test.ts | 5 +++++ packages/comark/test/streaming.test.ts | 9 +++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/packages/comark/src/auto-close.ts b/packages/comark/src/auto-close.ts index b306cfcf9..725772ad5 100644 --- a/packages/comark/src/auto-close.ts +++ b/packages/comark/src/auto-close.ts @@ -263,7 +263,17 @@ function scanBlocks(src: string, o: Opts): DocState { const dashes = c0 === 45 /* - */ && en - st === 3 && src.charCodeAt(st + 1) === 45 && src.charCodeAt(st + 2) === 45 - if (i === 0 && o.frontmatter && dashes) { + // Matches `parseFrontmatter`, which accepts a leading byte order mark before the opener. + if ( + i === 0 && + o.frontmatter && + (dashes || + (c0 === 0xfeff && + en - st === 4 && + src.charCodeAt(st + 1) === 45 && + src.charCodeAt(st + 2) === 45 && + src.charCodeAt(st + 3) === 45)) + ) { fmOpen = true continue } diff --git a/packages/comark/test/auto-close.test.ts b/packages/comark/test/auto-close.test.ts index 2c8ad1ad7..ad34f0424 100644 --- a/packages/comark/test/auto-close.test.ts +++ b/packages/comark/test/auto-close.test.ts @@ -583,6 +583,11 @@ describe('frontmatter', () => { const expected = '---\ntitle: Test\n---' expect(autoCloseMarkdown(input, { frontmatter: true })).toBe(expected) }) + it('should complete partial frontmatter after a byte order mark', () => { + const input = '\uFEFF---\ntitle: Test' + const expected = '\uFEFF---\ntitle: Test\n---' + expect(autoCloseMarkdown(input, { frontmatter: true })).toBe(expected) + }) it('should complete a partial closing delimiter (-) when frontmatter option is enabled', () => { const input = '---\ntitle: Test\n-' const expected = '---\ntitle: Test\n---' diff --git a/packages/comark/test/streaming.test.ts b/packages/comark/test/streaming.test.ts index d177d91e9..1db0d816c 100644 --- a/packages/comark/test/streaming.test.ts +++ b/packages/comark/test/streaming.test.ts @@ -211,6 +211,15 @@ describe('streaming mode', () => { expect(result2.frontmatter).toEqual({ title: 'Hello', category: 'Test' }) }) + it('closes partial frontmatter after a byte order mark', async () => { + const parse = createMarkdownParser() + + const result = await parse('\uFEFF---\ntitle: Hello', { streaming: true }) + + expect(result.frontmatter).toEqual({ title: 'Hello' }) + expect(result.nodes).toEqual([]) + }) + it('frontmatter is preserved across continuation calls', async () => { const parse = createMarkdownParser()