Skip to content

VaultMD / parseFrontmatter

Function: parseFrontmatter()

parseFrontmatter(content): ParsedFrontmatter

Defined in: src/frontmatter/parse.ts:53

Parse the YAML frontmatter from a markdown file's raw content string. Handles files with no frontmatter, empty blocks, invalid YAML, and nested blocks. Never throws.

A nested block is read: its keys come back and are indexed, and valid is 'nested' rather than 'flat' to say that editFrontmatter will refuse it. Only a block that cannot be stored at all — unparseable, a non-map root, or holding a cycle, a non-finite number, or nesting deep enough to overflow the serializer — comes back empty.

Parameters

content

string

Raw UTF-8 content of a markdown file.

Returns

ParsedFrontmatter

A ParsedFrontmatter with the parsed key-value map, tag tokens, body text, and a FrontmatterValidity descriptor.

Example

ts
const { frontmatter, tags, body, valid } = parseFrontmatter(fileContent);
if (valid === 'flat') { // safe to pass to editFrontmatter }
if (valid === 'nested') { // read frontmatter, but do not edit it }

Released under the MIT License.