@@ -2,28 +2,59 @@
22
33 const FM_RE = /^---\r?\n([\s\S]*?)\r?\n---\r?\n?([\s\S]*)$/;
44
5+type FrontmatterValue = string | Record<string, string>;
6+
57 /**
68 * Parse simple YAML-like frontmatter from a text string.
7- * Returns key-value pairs (string values only) and the body after the closing `---`.
9+ * Supports one level of nesting (e.g. `metadata:` with indented sub-keys).
10+ * Returns key-value pairs and the body after the closing `---`.
811 */
912 export function parseFrontmatter(raw: string): {
10- meta: Record<string, string>;
13+ meta: Record<string, FrontmatterValue>;
1114 body: string;
1215 } {
1316 const m = raw.match(FM_RE);
1417 if (!m) return { meta: {}, body: raw };
1518
16- const meta: Record<string, string> = {};
17- const yamlBlock = m[1] ?? "";
18- for (const line of yamlBlock.split("\n")) {
19- const colon = line.indexOf(":");
19+ const meta: Record<string, FrontmatterValue> = {};
20+ const lines = (m[1] ?? "").split("\n");
21+ let parentKey = "";
22+
23+ for (const line of lines) {
24+ const stripped = line.replace(/\r$/, "");
25+ const indent = stripped.length - stripped.trimStart().length;
26+
27+ if (indent > 0 && parentKey) {
28+ const colon = stripped.indexOf(":");
29+ if (colon === -1) continue;
30+ const key = stripped.slice(0, colon).trim();
31+ const val = stripped
32+ .slice(colon + 1)
33+ .trim()
34+ .replace(/^["']|["']$/g, "");
35+ if (!key) continue;
36+ const parent = meta[parentKey];
37+ if (typeof parent === "object") parent[key] = val;
38+ else meta[parentKey] = { [key]: val };
39+ continue;
40+ }
41+
42+ const colon = stripped.indexOf(":");
2043 if (colon === -1) continue;
21- const key = line.slice(0, colon).trim();
22- const val = line
44+ const key = stripped.slice(0, colon).trim();
45+ const val = stripped
2346 .slice(colon + 1)
2447 .trim()
2548 .replace(/^["']|["']$/g, "");
26- if (key) meta[key] = val;
49+ if (!key) continue;
50+
51+ if (val === "") {
52+ parentKey = key;
53+ meta[key] = {};
54+ } else {
55+ parentKey = "";
56+ meta[key] = val;
57+ }
2758 }
2859
2960 return { meta, body: (m[2] ?? "").trim() };