#front-matter #parser #json-toml #yaml #prefixed

matterhorn

A lenient front matter parsing crate that supports files prefixed with YAML, JSON, and TOML front matter

3 releases

0.1.2 Oct 11, 2024
0.1.1 Oct 11, 2024
0.1.0 Oct 11, 2024

#1430 in Parser implementations

Download history 250/week @ 2024-10-05 154/week @ 2024-10-12 7/week @ 2024-10-19

411 downloads per month

MIT license

17KB
393 lines

Matterhorn

A lenient front matter parsing crate that supports files prefixed with YAML, JSON, and TOML front matter. The type of front matter is detected automatically.

The order of input keys is retained, and parsing is lenient where possible. Notably, duplicate keys are supported in YAML front matter.

All parsed front matter is returned as a serde_json Value.

Usage

cargo add matterhorn
const YAML_SOURCE_FILE: &str = r#"
---
title: Hello World
order: 12
---
# Main Title

Cras mattis consectetur purus sit amet fermentum.
"#;

const TOML_SOURCE_FILE: &str = r#"
+++
title = "Hello World"
order = 12
+++
# Main Title

Cras mattis consectetur purus sit amet fermentum.
"#;

const JSON_SOURCE_FILE: &str = r#"
{
  "title": "Hello World",
  "order": 12
}
# Main Title

Cras mattis consectetur purus sit amet fermentum.
"#;

fn main() {
    let document = matterhorn::parse_document(YAML_SOURCE_FILE).expect("Input should be valid");

    println!("{:#?}", document.front_matter);
    // Returns:
    // serde_json::Value::Object {
    //     "title": serde_json::Value::String("Hello World"),
    //     "order": serde_json::Value::Number(12),
    // }

    println!("{:#?}", document.content);
    // Returns:
    // "# Main Title\n\nCras mattis consectetur purus sit amet fermentum.\n"

    assert_eq!(matterhorn::parse_document(YAML_SOURCE_FILE), matterhorn::parse_document(TOML_SOURCE_FILE));
    assert_eq!(matterhorn::parse_document(TOML_SOURCE_FILE), matterhorn::parse_document(JSON_SOURCE_FILE));
}

Dependencies

~3–4.5MB
~78K SLoC