Centralize frontmatter parsing + parse frontmatter with yaml library (#728)

* Add frontmatter utility and tidy coding agent prompts

* Add frontmatter parsing utilities and tests

* Parse frontmatter with YAML parser

* Simplify frontmatter parsing utilities

* strip body in 1 place

* Improve frontmatter parsing error handling

* Normalize multiline skill and select-list descriptions
This commit is contained in:
Richard Gill 2026-01-15 23:31:53 +00:00 committed by GitHub
parent df58d3191e
commit ce7e73b503
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 213 additions and 126 deletions

View file

@ -0,0 +1,8 @@
---
name: invalid-yaml
description: [unclosed bracket
---
# Invalid YAML Skill
This skill has invalid YAML in the frontmatter.

View file

@ -0,0 +1,11 @@
---
name: multiline-description
description: |
This is a multiline description.
It spans multiple lines.
And should be normalized.
---
# Multiline Description Skill
This skill tests that multiline YAML descriptions are normalized to single lines.

View file

@ -0,0 +1,60 @@
import { describe, expect, it } from "vitest";
import { parseFrontmatter, stripFrontmatter } from "../src/utils/frontmatter.js";
describe("parseFrontmatter", () => {
it("parses keys, strips quotes, and returns body", () => {
const input = "---\nname: \"skill-name\"\ndescription: 'A desc'\nfoo-bar: value\n---\n\nBody text";
const { frontmatter, body } = parseFrontmatter<Record<string, string>>(input);
expect(frontmatter.name).toBe("skill-name");
expect(frontmatter.description).toBe("A desc");
expect(frontmatter["foo-bar"]).toBe("value");
expect(body).toBe("Body text");
});
it("normalizes newlines and handles CRLF", () => {
const input = "---\r\nname: test\r\n---\r\nLine one\r\nLine two";
const { body } = parseFrontmatter<Record<string, string>>(input);
expect(body).toBe("Line one\nLine two");
});
it("throws on invalid YAML frontmatter", () => {
const input = "---\nfoo: [bar\n---\nBody";
expect(() => parseFrontmatter<Record<string, string>>(input)).toThrow(/at line 1, column 10/);
});
it("parses | multiline yaml syntax", () => {
const input = "---\ndescription: |\n Line one\n Line two\n---\n\nBody";
const { frontmatter, body } = parseFrontmatter<Record<string, string>>(input);
expect(frontmatter.description).toBe("Line one\nLine two\n");
expect(body).toBe("Body");
});
it("returns original content when frontmatter is missing or unterminated", () => {
const noFrontmatter = "Just text\nsecond line";
const missingEnd = "---\nname: test\nBody without terminator";
const resultNoFrontmatter = parseFrontmatter<Record<string, string>>(noFrontmatter);
const resultMissingEnd = parseFrontmatter<Record<string, string>>(missingEnd);
expect(resultNoFrontmatter.body).toBe("Just text\nsecond line");
expect(resultMissingEnd.body).toBe(
"---\nname: test\nBody without terminator".replace(/\r\n/g, "\n").replace(/\r/g, "\n"),
);
});
it("returns empty object for empty or comment-only frontmatter", () => {
const input = "---\n# just a comment\n---\nBody";
const { frontmatter } = parseFrontmatter(input);
expect(frontmatter).toEqual({});
});
});
describe("stripFrontmatter", () => {
it("removes frontmatter and trims body", () => {
const input = "---\nkey: value\n---\n\nBody\n";
expect(stripFrontmatter(input)).toBe("Body");
});
it("returns body when no frontmatter present", () => {
const input = "\n No frontmatter body \n";
expect(stripFrontmatter(input)).toBe("\n No frontmatter body \n");
});
});

View file

@ -95,6 +95,28 @@ describe("skills", () => {
expect(warnings.some((w) => w.message.includes("description is required"))).toBe(true);
});
it("should warn and skip skill when YAML frontmatter is invalid", () => {
const { skills, warnings } = loadSkillsFromDir({
dir: join(fixturesDir, "invalid-yaml"),
source: "test",
});
expect(skills).toHaveLength(0);
expect(warnings.some((w) => w.message.includes("at line"))).toBe(true);
});
it("should preserve multiline descriptions from YAML", () => {
const { skills, warnings } = loadSkillsFromDir({
dir: join(fixturesDir, "multiline-description"),
source: "test",
});
expect(skills).toHaveLength(1);
expect(skills[0].description).toContain("\n");
expect(skills[0].description).toContain("This is a multiline description.");
expect(warnings).toHaveLength(0);
});
it("should warn when name contains consecutive hyphens", () => {
const { skills, warnings } = loadSkillsFromDir({
dir: join(fixturesDir, "consecutive-hyphens"),