* Parse frontmatter from a markdown file using vfile-matter * Works with any markdown file that has YAML frontmatter (agents, prompts, instructions) * @param {string} filePath - Path to the markdown file * @returns {object|null} Parsed frontmatter object or null on error
(filePath)
| 21 | * @returns {object|null} Parsed frontmatter object or null on error |
| 22 | */ |
| 23 | function parseFrontmatter(filePath) { |
| 24 | return safeFileOperation( |
| 25 | () => { |
| 26 | const content = fs.readFileSync(filePath, "utf8"); |
| 27 | const file = new VFile({ path: filePath, value: content }); |
| 28 | |
| 29 | // Parse the frontmatter using vfile-matter |
| 30 | matter(file); |
| 31 | |
| 32 | // The frontmatter is now available in file.data.matter |
| 33 | const frontmatter = file.data.matter; |
| 34 | |
| 35 | // Normalize string fields that can accumulate trailing newlines/spaces |
| 36 | if (frontmatter) { |
| 37 | if (typeof frontmatter.name === "string") { |
| 38 | frontmatter.name = frontmatter.name.replace(/[\r\n]+$/g, "").trim(); |
| 39 | } |
| 40 | if (typeof frontmatter.title === "string") { |
| 41 | frontmatter.title = frontmatter.title.replace(/[\r\n]+$/g, "").trim(); |
| 42 | } |
| 43 | if (typeof frontmatter.description === "string") { |
| 44 | // Remove only trailing whitespace/newlines; preserve internal formatting |
| 45 | frontmatter.description = frontmatter.description.replace( |
| 46 | /[\s\r\n]+$/g, |
| 47 | "" |
| 48 | ); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | return frontmatter; |
| 53 | }, |
| 54 | filePath, |
| 55 | null |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Extract agent metadata including MCP server information |
no test coverage detected