Parse SDK
Frontmatter
Extract and split YAML frontmatter from Markdown strings.
datamark provides two frontmatter extraction functions for cases where you need them standalone.
import { extractFrontmatter, splitFrontmatter, FrontmatterError } from "datamark/parse";
const extracted = extractFrontmatter(`---
title: Hello
---
# Body`);
const split = splitFrontmatter(`---
title: Hello
---
# Body`);
const noFrontmatter = splitFrontmatter("# No frontmatter\n\nBody");extractFrontmatter()
import { extractFrontmatter } from "datamark/parse";
const result = extractFrontmatter(`---
title: Hello
---
Body here`);
// { frontmatter: "title: Hello", body: "Body here" }Supports both --- and +++ style fences. Returns null if no frontmatter is found.
splitFrontmatter()
import { splitFrontmatter } from "datamark/parse";
const result = splitFrontmatter(`---
title: Hello
---
Body here`);
// { frontmatter: "title: Hello", body: "Body here" }More lenient than extractFrontmatter. Uses line-by-line scanning and handles missing closing fences gracefully. Always returns an object (frontmatter may be empty string).
You usually don't need these directly. parse() handles frontmatter extraction and YAML parsing automatically.
FrontmatterError
Both functions throw FrontmatterError on unexpected issues:
import { FrontmatterError } from "datamark/parse";
try {
extractFrontmatter(badContent);
} catch (err) {
if (err instanceof FrontmatterError) {
console.error(err.message);
}
}