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 00000000..094965d8 --- /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/SPEC/COMARK/frontmatter-closing-delimiter-with-text.md b/packages/comark/SPEC/COMARK/frontmatter-closing-delimiter-with-text.md new file mode 100644 index 00000000..cf0cb4c5 --- /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 00000000..415e133d --- /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/auto-close.ts b/packages/comark/src/auto-close.ts index b306cfcf..725772ad 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/src/internal/frontmatter.ts b/packages/comark/src/internal/frontmatter.ts index 76283950..cf186681 100644 --- a/packages/comark/src/internal/frontmatter.ts +++ b/packages/comark/src/internal/frontmatter.ts @@ -4,27 +4,58 @@ import { parseYaml, stringifyYaml } from './yaml.ts' const FRONTMATTER_DELIMITER_DEFAULT = '---' const LF = '\n' const CR = '\r' +const BOM = '\uFEFF' -/** Extract leading YAML frontmatter (`---`) from markdown content. */ -export function parseFrontmatter(content: string) { - let data: Record = {} +/** + * 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) + } +} + +/** Split leading frontmatter (`---`) from markdown content without parsing its YAML. */ +export function splitFrontmatter(content: string): { content: string; frontmatterText: string } { 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)) + 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/internal/parse/syntax/props.ts b/packages/comark/src/internal/parse/syntax/props.ts index 5fe3eb16..99da509f 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 @@ -123,5 +125,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 ac3c4f71..0c96163d 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 diff --git a/packages/comark/src/plugins/frontmatter.ts b/packages/comark/src/plugins/frontmatter.ts index 05d3703e..2df25266 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/auto-close.test.ts b/packages/comark/test/auto-close.test.ts index 2c8ad1ad..ad34f042 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/frontmatter.test.ts b/packages/comark/test/frontmatter.test.ts index 305b80bc..5ae98631 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,7 +126,7 @@ Content` expect(result.content.trim()).toBe('Content') }) - it('should still throw for malformed YAML in frontmatter', () => { + it('should throw for malformed YAML in frontmatter', () => { const input = `--- foo: [1,2 --- @@ -135,6 +135,33 @@ 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 = 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() + }) + + 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', () => { const input = `--- title: "Hello: World" diff --git a/packages/comark/test/streaming.test.ts b/packages/comark/test/streaming.test.ts index d177d91e..1db0d816 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() diff --git a/test/bundle.test.ts b/test/bundle.test.ts index 6cfe2a71..19cff971 100644 --- a/test/bundle.test.ts +++ b/test/bundle.test.ts @@ -77,7 +77,7 @@ describe('package bundle size', { timeout: 60_000 }, () => { "@comark/react": "43.3k (87 files)", "@comark/svelte": "51.0k (95 files)", "@comark/vue": "62.7k (93 files)", - "comark": "455k (177 files)", + "comark": "457k (177 files)", } `) })