Files
radicle-explorer/src/lib/frontmatter.ts
T
Rūdolfs Ošiņš 3c2935704f Upgrade npm dependencies
This also upgrades dompurify to the latest version, outside our cooldown
window, for a security fix (verified safe with extra security checks).

Newer dompurify versions expose a bug where happy-dom renders one of our
custom elements incorrectly in tests, unlike real browsers. Switch the unit
test environment from happy-dom to jsdom to fix the failing tests.
2026-07-16 11:34:41 +02:00

22 lines
592 B
TypeScript

import { load } from "js-yaml";
const FRONTMATTER_RE = /^---\r?\n([\s\S]*?)\r?\n---\r?\n?/;
export interface ParsedFrontmatter {
data: Record<string, unknown>;
content: string;
}
export function parseFrontmatter(input: string): ParsedFrontmatter {
const match = input.match(FRONTMATTER_RE);
if (!match) {
return { data: {}, content: input };
}
const parsed = load(match[1]);
const data =
parsed && typeof parsed === "object" && !Array.isArray(parsed)
? (parsed as Record<string, unknown>)
: {};
return { data, content: input.slice(match[0].length) };
}