mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-17 13:05:03 +00:00
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:
parent
df58d3191e
commit
ce7e73b503
14 changed files with 213 additions and 126 deletions
|
|
@ -1,6 +1,7 @@
|
|||
import { existsSync, readdirSync, readFileSync, statSync } from "fs";
|
||||
import { join, resolve } from "path";
|
||||
import { CONFIG_DIR_NAME, getPromptsDir } from "../config.js";
|
||||
import { parseFrontmatter } from "../utils/frontmatter.js";
|
||||
|
||||
/**
|
||||
* Represents a prompt template loaded from a markdown file
|
||||
|
|
@ -12,36 +13,6 @@ export interface PromptTemplate {
|
|||
source: string; // e.g., "(user)", "(project)", "(project:frontend)"
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse YAML frontmatter from markdown content
|
||||
* Returns { frontmatter, content } where content has frontmatter stripped
|
||||
*/
|
||||
function parseFrontmatter(content: string): { frontmatter: Record<string, string>; content: string } {
|
||||
const frontmatter: Record<string, string> = {};
|
||||
|
||||
if (!content.startsWith("---")) {
|
||||
return { frontmatter, content };
|
||||
}
|
||||
|
||||
const endIndex = content.indexOf("\n---", 3);
|
||||
if (endIndex === -1) {
|
||||
return { frontmatter, content };
|
||||
}
|
||||
|
||||
const frontmatterBlock = content.slice(4, endIndex);
|
||||
const remainingContent = content.slice(endIndex + 4).trim();
|
||||
|
||||
// Simple YAML parsing - just key: value pairs
|
||||
for (const line of frontmatterBlock.split("\n")) {
|
||||
const match = line.match(/^(\w+):\s*(.*)$/);
|
||||
if (match) {
|
||||
frontmatter[match[1]] = match[2].trim();
|
||||
}
|
||||
}
|
||||
|
||||
return { frontmatter, content: remainingContent };
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse command arguments respecting quoted strings (bash-style)
|
||||
* Returns array of arguments
|
||||
|
|
@ -145,7 +116,7 @@ function loadTemplatesFromDir(dir: string, source: "user" | "project", subdir: s
|
|||
} else if (isFile && entry.name.endsWith(".md")) {
|
||||
try {
|
||||
const rawContent = readFileSync(fullPath, "utf-8");
|
||||
const { frontmatter, content } = parseFrontmatter(rawContent);
|
||||
const { frontmatter, body } = parseFrontmatter<Record<string, string>>(rawContent);
|
||||
|
||||
const name = entry.name.slice(0, -3); // Remove .md extension
|
||||
|
||||
|
|
@ -160,7 +131,7 @@ function loadTemplatesFromDir(dir: string, source: "user" | "project", subdir: s
|
|||
// Get description from frontmatter or first non-empty line
|
||||
let description = frontmatter.description || "";
|
||||
if (!description) {
|
||||
const firstLine = content.split("\n").find((line) => line.trim());
|
||||
const firstLine = body.split("\n").find((line) => line.trim());
|
||||
if (firstLine) {
|
||||
// Truncate if too long
|
||||
description = firstLine.slice(0, 60);
|
||||
|
|
@ -174,7 +145,7 @@ function loadTemplatesFromDir(dir: string, source: "user" | "project", subdir: s
|
|||
templates.push({
|
||||
name,
|
||||
description,
|
||||
content,
|
||||
content: body,
|
||||
source: sourceStr,
|
||||
});
|
||||
} catch (_error) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue