-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,65 @@ | ||
# 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 | ||
|
||
```bash | ||
cargo add matterhorn | ||
``` | ||
|
||
```rust | ||
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)); | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters