Skip to content

vaultmd / serializeFrontmatter

Function: serializeFrontmatter()

serializeFrontmatter(frontmatter): string

Defined in: src/frontmatter/serialize.ts:55

Serialize a flat frontmatter map to a fenced YAML block ready to prepend to a markdown note. The output is byte-identical to the fresh frontmatter block createNote / editFrontmatter emit when a note has no existing block (they preserve an existing block's styling, which this does not reproduce). parseFrontmatter is its inverse: every accepted input round-trips.

An empty map yields the empty string (no block), matching what createNote / editFrontmatter write for empty frontmatter. Non-empty arrays serialize as block sequences; an empty array serializes as flow [].

Parameters

frontmatter

Record<string, unknown>

Flat key-value map (scalars and arrays of scalars only).

Returns

string

A string of the form ---\n<yaml>\n---\n, or '' for an empty map.

Throws

MdVaultError with code FRONTMATTER_INVALID when the input contains nested objects, arrays of non-scalars, Dates, or non-finite numbers — none of which survive a parse round-trip.

Example

ts
const header = serializeFrontmatter({ title: 'Hello', tags: ['a', 'b'] });
// "---\ntitle: Hello\ntags:\n  - a\n  - b\n---\n"