Skip to content

preserveFrontmatterFormatting

Rewrites the front matter of a note, keeping the original bytes of everything the change does not reach.

It is the engine enableFrontmatterFormattingPreservation registers, and can be called directly as well.

null means “this one is not mine”: the block does not exist, the new front matter is empty (both are the parse-and-stringify path’s business, unchanged), or the splice could not be proved faithful. The proof is not hand-waved — the spliced block is re-parsed and compared against the value the parse-and-stringify path would have produced for the same input, and any difference returns null. That is what makes this strictly better than that path or identical to it, and never worse.

Import:

import { preserveFrontmatterFormatting } from 'obsidian-dev-utils/obsidian/frontmatter-formatting';

Signature:

function preserveFrontmatterFormatting(content: string, newFrontmatter: object): string | null

Parameters:

Parameter Type Description
content string The note to rewrite the front matter of.
newFrontmatter object The front matter to write.

Returns: string | null — The new note, or null when the caller should parse the block and write the whole of it out again instead.