Skip to main content

Crate frontmatter_lossless

Crate frontmatter_lossless 

Source
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 \r stays in the YAML slice, so callers see the exact bytes. compose rebuilds ---\r\n fences 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. split still flags it in Split::unterminated for 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§

LenientSplit
The result of a tolerant read-only split: see split_lenient.
NewShapeOptions
Options for new_shape.
NoteShape
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§

CloseEol
Line ending after the closing --- fence.
OpenEol
Line ending after the opening --- fence.

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 in opts.eol and 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.