mirror of
https://seed.radicle.dev/rad:z4V1sjrXqjvFdnCUbxPFqd5p4DtH5.git
synced 2026-11-15 19:30:38 +08:00
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.
22 lines
592 B
TypeScript
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) };
|
|
}
|