export interface FrontmatterResult { frontmatter: Record body: string } export function parseFrontmatter(content: string): FrontmatterResult { const trimmed = content.trimStart() if (!trimmed.startsWith("---")) { return { frontmatter: {}, body: content } } const endMatch = trimmed.indexOf("---", 3) if (endMatch === -1 || endMatch < 4) { return { frontmatter: {}, body: content } } const frontmatterStr = trimmed.slice(3, endMatch).trim() const body = trimmed.slice(endMatch + 3).trimStart() const frontmatter: Record = {} for (const line of frontmatterStr.split("\n")) { const colonIdx = line.indexOf(":") if (colonIdx === -1) continue const key = line.slice(0, colonIdx).trim() let value: unknown = line.slice(colonIdx + 1).trim() if (typeof value === "string") { if (value === "true") value = true else if (value === "false") value = false else if (/^\d+$/.test(value)) value = Number(value) else if (value.startsWith("[") && value.endsWith("]")) { value = value .slice(1, -1) .split(",") .map((s) => s.trim()) .filter(Boolean) } else { value = value.replace(/^["']|["']$/g, "") } } frontmatter[key] = value } return { frontmatter, body } }