Expand description
Byte-exact Markdown frontmatter framing.
A note is ----fenced YAML frontmatter followed by a body. This crate
splits the two and joins them again without changing any byte the author
wrote. The YAML text comes back as written (without the fences), with the
byte offsets an edit needs, and compose rebuilds the original file
exactly: the same BOM, the same \n or \r\n fences, and the same ending.
It has no YAML parsing, no filesystem access, no regex, and nothing but
std. Reading the frontmatter’s values is a job for a YAML parser. This
crate only finds where the block is and puts it back. It does not parse
the body.
It handles three cases that real notes contain:
- BOM. Some Windows editors start a note with
\u{feff}---. The split recognises the BOM and records it, so a later write can restore it. - CRLF. The
\rstays in the YAML slice, so callers see the exact bytes.composerebuilds---\r\nfences rather than rewriting them to---\n, which would change every line of the file in a diff. - A missing or malformed closing fence is not an error. An unterminated
block is read as body, so code that reads thousands of notes keeps going
instead of stopping at one bad note.
splitstill flags it inSplit::unterminatedfor a caller that wants to report the note as malformed.
use frontmatter_lossless::split;
let note = "---\ntitle: Hello\n---\n# Body\n";
let s = split(note);
assert_eq!(s.yaml, Some("title: Hello"));
assert_eq!(s.body, "# Body\n");Structs§
- Lenient
Split - The result of a tolerant read-only split: see
split_lenient. - NewShape
Options - Options for
new_shape. - Note
Shape - Everything about a note’s framing that a writer has to put back exactly.
- Split
- A note split into its parts, with byte offsets into the original source.
Enums§
Functions§
- compose
- Rebuild a note from a frontmatter block (possibly edited) and a body. This is
the exact inverse of
split. - new_
shape - The shape for a new note, one with no source text to
split: both fences end inopts.eoland the closing fence has its line ending. - split
- Split a note into its frontmatter YAML and body.
- split_
lenient - A tolerant, read-only frontmatter read for input that may be loosely written.