@@ -3,10 +3,85 @@
33 const FM_RE = /^---\r?\n([\s\S]*?)\r?\n---\r?\n?([\s\S]*)$/;
44
55 type FrontmatterValue = string | Record<string, string>;
6+type BlockScalarStyle = ">" | "|";
7+
8+function trimQuotedValue(value: string): string {
9+ return value.replace(/^["']|["']$/g, "");
10+}
11+
12+function lineIndent(line: string): number {
13+ return line.length - line.trimStart().length;
14+}
15+
16+function parseBlockScalarStyle(value: string): BlockScalarStyle | null {
17+ if (/^>[+-]?$/.test(value)) return ">";
18+ if (/^\|[+-]?$/.test(value)) return "|";
19+ return null;
20+}
21+
22+function collectBlockScalarLines(
23+ lines: string[],
24+ startIndex: number,
25+ parentIndent: number,
26+): { lines: string[]; nextIndex: number } {
27+ const blockLines: string[] = [];
28+ let minIndent = Number.POSITIVE_INFINITY;
29+ let index = startIndex;
30+
31+ for (; index < lines.length; index++) {
32+ const line = lines[index]?.replace(/\r$/, "") ?? "";
33+ if (line.trim() === "") {
34+ blockLines.push("");
35+ continue;
36+ }
37+
38+ const indent = lineIndent(line);
39+ if (indent <= parentIndent) break;
40+ if (indent < minIndent) minIndent = indent;
41+ blockLines.push(line);
42+ }
43+
44+ if (!Number.isFinite(minIndent)) {
45+ return { lines: blockLines, nextIndex: index };
46+ }
47+
48+ return {
49+ lines: blockLines.map((line) =>
50+ line === "" ? "" : line.slice(Math.min(minIndent, line.length)),
51+ ),
52+ nextIndex: index,
53+ };
54+}
55+
56+function foldBlockScalarLines(lines: string[]): string {
57+ let result = "";
58+ let pendingNewlines = 0;
59+
60+ for (const line of lines) {
61+ if (line === "") {
62+ pendingNewlines++;
63+ continue;
64+ }
65+
66+ if (result !== "") {
67+ if (pendingNewlines > 0) result += "\n".repeat(pendingNewlines);
68+ else result += " ";
69+ }
70+
71+ result += line;
72+ pendingNewlines = 0;
73+ }
74+
75+ return result;
76+}
77+
78+function blockScalarValue(style: BlockScalarStyle, lines: string[]): string {
79+ return style === ">" ? foldBlockScalarLines(lines) : lines.join("\n");
80+}
681
782 /**
883 * Parse simple YAML-like frontmatter from a text string.
9- * Supports one level of nesting (e.g. `metadata:` with indented sub-keys).
84+ * Supports one level of nesting and block scalar strings (`>` / `|`).
1085 * Returns key-value pairs and the body after the closing `---`.
1186 */
1287 export function parseFrontmatter(raw: string): {
@@ -20,40 +95,53 @@ export function parseFrontmatter(raw: string): {
2095 const lines = (m[1] ?? "").split("\n");
2196 let parentKey = "";
2297
23- for (const line of lines) {
24- const stripped = line.replace(/\r$/, "");
25- const indent = stripped.length - stripped.trimStart().length;
98+ for (let index = 0; index < lines.length; index++) {
99+ const stripped = (lines[index] ?? "").replace(/\r$/, "");
100+ const indent = lineIndent(stripped);
26101
27102 if (indent > 0 && parentKey) {
28103 const colon = stripped.indexOf(":");
29104 if (colon === -1) continue;
30105 const key = stripped.slice(0, colon).trim();
31- const val = stripped
32- .slice(colon + 1)
33- .trim()
34- .replace(/^["']|["']$/g, "");
106+ const rawValue = stripped.slice(colon + 1).trim();
35107 if (!key) continue;
108+
109+ const style = parseBlockScalarStyle(rawValue);
36110 const parent = meta[parentKey];
37- if (typeof parent === "object") parent[key] = val;
38- else meta[parentKey] = { [key]: val };
111+ if (typeof parent !== "object") continue;
112+
113+ if (style) {
114+ const block = collectBlockScalarLines(lines, index + 1, indent);
115+ parent[key] = blockScalarValue(style, block.lines);
116+ index = block.nextIndex - 1;
117+ continue;
118+ }
119+
120+ parent[key] = trimQuotedValue(rawValue);
39121 continue;
40122 }
41123
42124 const colon = stripped.indexOf(":");
43125 if (colon === -1) continue;
44126 const key = stripped.slice(0, colon).trim();
45- const val = stripped
46- .slice(colon + 1)
47- .trim()
48- .replace(/^["']|["']$/g, "");
127+ const rawValue = stripped.slice(colon + 1).trim();
49128 if (!key) continue;
50129
51- if (val === "") {
130+ const style = parseBlockScalarStyle(rawValue);
131+ if (style) {
132+ const block = collectBlockScalarLines(lines, index + 1, indent);
133+ parentKey = "";
134+ meta[key] = blockScalarValue(style, block.lines);
135+ index = block.nextIndex - 1;
136+ continue;
137+ }
138+
139+ if (rawValue === "") {
52140 parentKey = key;
53141 meta[key] = {};
54142 } else {
55143 parentKey = "";
56- meta[key] = val;
144+ meta[key] = trimQuotedValue(rawValue);
57145 }
58146 }
59147