|
| 1 | +# 139 - Markdown Frontmatter Checker |
| 2 | + |
| 3 | +```rig |
| 4 | +import { agent, p, s, defineTool } from "rig"; |
| 5 | +import { repair } from "rig/addons"; |
| 6 | +import { readFileSync, existsSync } from "node:fs"; |
| 7 | +
|
| 8 | +const parseFrontmatter = defineTool("parseFrontmatter", { |
| 9 | + description: "Parse YAML frontmatter from a markdown file and check for required fields", |
| 10 | + parameters: s.object({ filePath: s.string }), |
| 11 | + handler: ({ filePath }) => { |
| 12 | + if (!existsSync(filePath)) return JSON.stringify({ error: "file not found" }); |
| 13 | + const content = readFileSync(filePath, "utf8"); |
| 14 | + if (!content.startsWith("---")) return JSON.stringify({ presentFields: [], missingFields: ["title", "description", "date"], status: "missing" }); |
| 15 | + const end = content.indexOf("---", 3); |
| 16 | + if (end === -1) return JSON.stringify({ presentFields: [], missingFields: ["title", "description", "date"], status: "missing" }); |
| 17 | + const fm = content.slice(3, end); |
| 18 | + const required = ["title", "description", "date"]; |
| 19 | + const present = required.filter((f) => new RegExp(`^${f}\\s*:`, "m").test(fm)); |
| 20 | + const missing = required.filter((f) => !present.includes(f)); |
| 21 | + const status = missing.length === 0 ? "complete" : present.length === 0 ? "missing" : "partial"; |
| 22 | + return JSON.stringify({ presentFields: present, missingFields: missing, status }); |
| 23 | + }, |
| 24 | +}); |
| 25 | +
|
| 26 | +// Agent role: check markdown files for required YAML frontmatter fields (title, description, date). |
| 27 | +const markdownFrontmatterChecker = agent({ |
| 28 | + model: "small", |
| 29 | + maxTurns: 4, |
| 30 | + addons: [repair()], |
| 31 | + instructions: p`Check markdown files for required YAML frontmatter fields. |
| 32 | +
|
| 33 | +Markdown files: ${p.bash("find . -name '*.md' -not -path '*/node_modules/*' | head -50")} |
| 34 | +
|
| 35 | +For each markdown file, use parseFrontmatter to detect which required fields (title, description, date) are present or missing. Return a record keyed by filename.`, |
| 36 | + output: s.record(s.object({ |
| 37 | + presentFields: s.array(s.string), |
| 38 | + missingFields: s.array(s.string), |
| 39 | + status: s.enum("complete", "partial", "missing"), |
| 40 | + })), |
| 41 | + tools: [parseFrontmatter], |
| 42 | +}); |
| 43 | +
|
| 44 | +export default markdownFrontmatterChecker; |
| 45 | +``` |
0 commit comments