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