Skip to content

Commit

Permalink
Fix flat json front matter
Browse files Browse the repository at this point in the history
  • Loading branch information
bglw committed Oct 11, 2024
1 parent 7e4e3ef commit 515cdfe
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

## Unreleased

* Fix parsing of flat JSON front matter objects

## v0.1.1 (October 11, 2024)

* Automated release setup
Expand Down
62 changes: 62 additions & 0 deletions README.md
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));
}
```
5 changes: 4 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ pub fn parse_document<'d>(
document_start + separator.len()
};

for (pre_fm_end, _) in original_content.match_indices(separator).skip(1) {
for (pre_fm_end, _) in original_content
.match_indices(separator)
.filter(|(i, _)| *i > fm_start)
{
let post_fm_end = pre_fm_end + separator.len();
let potential_front_matter = if should_parse_separator {
&original_content[fm_start..post_fm_end]
Expand Down

0 comments on commit 515cdfe

Please sign in to comment.