frontmatter-lossless
Split a Markdown note into its YAML frontmatter and its body, with offsets
into the source, and put it back together byte for byte. The byte-order mark,
CRLF line endings and the exact fence bytes are kept. No dependencies beyond
std.
Why
A program that edits a note's frontmatter has to take the note apart and put
it back. The common tools parse the YAML and write it out again, so the
result can differ from the input even when no value changed: a BOM is lost,
---\r\n fences become ---\n, or the YAML is reformatted. In version
control every line of the note then shows as changed.
This crate does only the framing. It finds the --- block, returns the YAML
text exactly as written, and rebuilds the note from the parts. Hand it back
what it gave you and you get the original text.
It is for programs that read or rewrite notes that people also edit: note apps like Obsidian, Logseq, Foam and Zettlr, static site generators like Jekyll, Hugo, Astro, Eleventy and Zola, and the linters and sync tools around them.
When not to use it:
- To read the frontmatter's values. Pass the
yamlstring to a YAML parser. - For
+++TOML or;;;JSON frontmatter. Only---YAML blocks are found. - To parse the Markdown body. The body is returned as a string.
Install
Rust 1.89 or later (the workspace MSRV), edition 2024.
Quick start
use ;
let note = "---\r\ntitle: Old\r\n---\r\n# Heading\r\n";
let s = split;
assert_eq!; // the YAML text, verbatim
assert_eq!;
// Change the YAML text however you like, then put the note back.
let new_yaml = s.yaml.unwrap.replace;
let updated = compose;
assert_eq!; // CRLF fences stay
// Hand back what split gave you and you get the original, byte for byte.
assert_eq!;
API
split(src)returns aSplit:yaml(the text between the fences, without them, orNonewhen the note has no block),body, the offsetsyaml_range(a byte range) andbody_start, and ashape. A block is found only when the note starts with---and a line break, after an optional BOM, and is closed by---alone on a line. Anything else, including a block with no closing fence, is body. It never panics.unterminatedistruewhen the note opens a block that never closes, so a caller can report that note as malformed rather than read it as body.compose(frontmatter, body, shape)rebuilds the note.frontmatterisNonefor no block,Some("")for an empty block, or the YAML text. Passing back whatsplitreturned gives the original text.NoteShapeholds the framing:bom,open_eol(anOpenEol,LforCrlf),close_eol(aCloseEol, which can also beNonewhen the note ends at the closing fence) andclose_sep. Take it fromsplitand pass it tocompose. Do not build one by hand: it is#[non_exhaustive]and a minor release may add a field. Build one for a new note withnew_shape.new_shape(opts)returns theNoteShapefor a new note that has no source text to split. ItsNewShapeOptionsareeol(OpenEol::Lfby default, orOpenEol::Crlf) for both fences andbom(falseby default). Pass the YAML text the waysplitreturns it, without the line break before the closing fence: trim the trailing newline a YAML serializer adds.split_lenient(content)reads hand-written input thatsplitrejects: a leading BOM, blank lines or indentation before the opening fence, and trailing spaces on the fence lines. The closing fence must not be indented. It returns aLenientSplit(yamlandbody), orNonewhen there is no block. It keeps no framing, so use it to read, not to rewrite.
use ;
let yaml = "title: New\ntags: [a]\n"; // as a YAML serializer writes it
let note = compose;
assert_eq!;
Offsets are UTF-8 byte offsets into the source:
&src[s.yaml_range] == yaml and &src[s.body_start..] == s.body.
use split_lenient;
let ls = split_lenient.unwrap;
assert_eq!;
assert!;
Each item has Rustdoc in the source. Your editor shows it, and cargo doc
builds it.
How it compares
Several Rust crates read frontmatter. They parse the block into values. This crate does not. It returns the YAML text as written, with byte offsets, and rebuilds the note from the parts, so a round trip is byte-exact. That is a different job.
frontmatterwrapsyaml-rustand hands back parsed data. It is a value-level parser, not byte-exact framing.frontmatter-genparses YAML, TOML and JSON frontmatter into a typed value.markdown-frontmatterparses the block and deserializes it withserde.pulldown-cmark-frontmatterextracts frontmatter while parsing Markdown withpulldown-cmark.mif-frontmatterparses a frontmatter block into values.
Use one of those when you want the values. Use this crate when the note must
come back unchanged. To edit single YAML values in place, keeping comments and
key order, run a YAML editor on the yaml string, then compose the result.
This crate is the layer under such an editor.
Twin
@osdlc/frontmatter-lossless is the TypeScript version, with the same
algorithm and the same conformance corpus. Its offsets are UTF-16 code units; this crate's
are UTF-8 bytes.
Stability
Every public item is stable from 1.0.0. A breaking change needs a new major
version and is listed in the CHANGELOG under "Breaking". NoteShape and
Split are #[non_exhaustive], so a later minor release can add a field
without breaking callers. There are no experimental items.
Runtimes and platforms
Any platform Rust supports, tested on Linux and macOS. The code is pure std
string handling with no file system or platform code.
License
MIT OR Apache-2.0, at your option.