import { parse } from "yaml"; type ParsedFrontmatter> = { 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 function parseFrontmatter = Record>( content: string, ): ParsedFrontmatter { const { yamlString, body } = extractFrontmatter(content); if (!yamlString) { return { frontmatter: {} as T, body }; } const parsed = parse(yamlString) as T | null | undefined; return { frontmatter: (parsed ?? ({} as T)), body }; } export function stripFrontmatter(content: string): string { return parseFrontmatter(content).body; }