mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-16 05:03:26 +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
39
packages/coding-agent/src/utils/frontmatter.ts
Normal file
39
packages/coding-agent/src/utils/frontmatter.ts
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
import { parse } from "yaml";
|
||||
|
||||
type ParsedFrontmatter<T extends Record<string, unknown>> = {
|
||||
frontmatter: T;
|
||||
body: string;
|
||||
};
|
||||
|
||||
const normalizeNewlines = (value: string): string => value.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
|
||||
|
||||
const extractFrontmatter = (content: string): { yamlString: string | null; body: string } => {
|
||||
const normalized = normalizeNewlines(content);
|
||||
|
||||
if (!normalized.startsWith("---")) {
|
||||
return { yamlString: null, body: normalized };
|
||||
}
|
||||
|
||||
const endIndex = normalized.indexOf("\n---", 3);
|
||||
if (endIndex === -1) {
|
||||
return { yamlString: null, body: normalized };
|
||||
}
|
||||
|
||||
return {
|
||||
yamlString: normalized.slice(4, endIndex),
|
||||
body: normalized.slice(endIndex + 4).trim(),
|
||||
};
|
||||
};
|
||||
|
||||
export const parseFrontmatter = <T extends Record<string, unknown> = Record<string, unknown>>(
|
||||
content: string,
|
||||
): ParsedFrontmatter<T> => {
|
||||
const { yamlString, body } = extractFrontmatter(content);
|
||||
if (!yamlString) {
|
||||
return { frontmatter: {} as T, body };
|
||||
}
|
||||
const parsed = parse(yamlString);
|
||||
return { frontmatter: (parsed ?? {}) as T, body };
|
||||
};
|
||||
|
||||
export const stripFrontmatter = (content: string): string => parseFrontmatter(content).body;
|
||||
Loading…
Add table
Add a link
Reference in a new issue