Something went wrong. Try again.
fork of prime agent with changes i want but are not upstreamable probqbly (and some fixes)
Something went wrong. Try again.
1.2 kB · 43 lines
TypeScript
at main
1234567891011121314151617181920212223242526272829303132333435363738394041424344import { parse } from "yaml";
type ParsedFrontmatter<T extends Record<string, unknown>> = { frontmatter: T; body: string;};
const normalizeNewlines = (value: string): string => value .replace(/^\uFEFF/, "") .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;