Skip to content

VaultMD / editFrontmatter

Function: editFrontmatter()

editFrontmatter(content, mutate): object

Defined in: src/frontmatter/edit.ts:33

Apply a mutator callback to a note's frontmatter and return the rewritten file content. Preserves the existing YAML structure and only writes back changed keys.

Flat blocks only. The file is left untouched, with an 'unverifiable' outcome, when the existing block is nested or cannot be stored, when the mutation would make it either, or when the rewrite would orphan a YAML anchor. A nested block is read through parseFrontmatter and rewritten, if at all, through transformNote — the mutator is not even called for one.

Parameters

content

string

Raw UTF-8 content of the markdown file.

mutate

(fm) => void

Callback that receives a mutable copy of the frontmatter object. Add, update, or delete keys in place.

Returns

Object with the updated content string and an EditOutcome describing whether the frontmatter was changed.

content

content: string

The rewritten file content (identical to input when outcome is not 'edited').

outcome

outcome: EditOutcome

Whether the mutation produced a change, no change, or was skipped.

Example

ts
const { content: updated, outcome } = editFrontmatter(raw, (fm) => {
  fm.status = 'done';
});

Released under the MIT License.