xonecas.io
64875
Mc

mini-coder / commit 4f0311f

fix: support multiline frontmatter fields

xonecas2026-03-31 4f0311ff35ef8844b53ee245641e104ad7438a87 parent 01716b4

4 files changed

TODO.md+1−1
@@ -1,3 +1,3 @@
11 # mini-coder TODO
22
3-- Skills that have a multiline frontmatter field, like the description only show the multiline start char `>` in the `/help` output with no description. We need to debug this and ensure that our frotmatter parser supports these multiline fields correctly.
3+_No open items._
src/cli/frontmatter.test.ts+24−0
@@ -70,4 +70,28 @@ describe("parseFrontmatter", () => {
7070 body: "End",
7171 });
7272 });
73+ test("parses folded block scalar values", () => {
74+ const raw =
75+ "---\nname: debugger\ndescription: >\n Diagnose bugs methodically\n and keep an audit trail.\ncompatibility: Requires git\n---\n\nBody";
76+ expect(parseFrontmatter(raw)).toEqual({
77+ meta: {
78+ name: "debugger",
79+ description: "Diagnose bugs methodically and keep an audit trail.",
80+ compatibility: "Requires git",
81+ },
82+ body: "Body",
83+ });
84+ });
85+
86+ test("parses literal block scalar values", () => {
87+ const raw =
88+ "---\nname: reviewer\ndescription: |\n Line one\n\n Line two\n---\n\nBody";
89+ expect(parseFrontmatter(raw)).toEqual({
90+ meta: {
91+ name: "reviewer",
92+ description: "Line one\n\nLine two",
93+ },
94+ body: "Body",
95+ });
96+ });
7397 });
src/cli/frontmatter.ts+104−16
@@ -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
src/cli/skills.test.ts+11−0
@@ -53,6 +53,17 @@ describe("skills loader", () => {
5353 expect(loadSkillContent("test-skill", dir, fakeHome)?.content).toBe(raw);
5454 });
5555
56+ test("loads folded multiline descriptions from skill frontmatter", () => {
57+ const raw =
58+ "---\nname: multiline-skill\ndescription: >\n Diagnose and fix bugs\n using a structured process.\n---\n\nDo the thing.";
59+ writeSkill(dir, ".agents", "multiline-skill", raw);
60+
61+ const index = loadSkillsIndex(dir, fakeHome);
62+ expect(index.get("multiline-skill")?.description).toBe(
63+ "Diagnose and fix bugs using a structured process.",
64+ );
65+ });
66+
5667 test("discovers skills from parent directories when cwd is nested", () => {
5768 writeSkill(
5869 dir,