https://github.com/the-alchemists-of-arland/gray-matter-rs
A tool for easily extracting front matter out of a string. It is a fast Rust implementation of gray-matter. Parses YAML, JSON, TOML and support for custom parsers. Use it and let me know by giving it a star!
https://github.com/the-alchemists-of-arland/gray-matter-rs
data front-matter front-matter-parsers frontmatter gray-matter gray-matter-rs gray-matter-rust markdown matter parse rust rust-crate yaml
Last synced: 20 days ago
JSON representation
A tool for easily extracting front matter out of a string. It is a fast Rust implementation of gray-matter. Parses YAML, JSON, TOML and support for custom parsers. Use it and let me know by giving it a star!
- Host: GitHub
- URL: https://github.com/the-alchemists-of-arland/gray-matter-rs
- Owner: the-alchemists-of-arland
- License: mit
- Created: 2021-02-02T03:10:36.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-07-21T18:06:25.000Z (9 months ago)
- Last Synced: 2025-04-10T23:51:52.216Z (20 days ago)
- Topics: data, front-matter, front-matter-parsers, frontmatter, gray-matter, gray-matter-rs, gray-matter-rust, markdown, matter, parse, rust, rust-crate, yaml
- Language: Rust
- Homepage: https://docs.rs/gray_matter
- Size: 159 KB
- Stars: 42
- Watchers: 1
- Forks: 4
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: changelog.md
- License: LICENSE
Awesome Lists containing this project
README

[](https://crates.io/crates/gray_matter)
[](https://docs.rs/gray_matter)gray_matter is a tool for easily extracting front matter out of a string. It is a fast Rust implementation of [gray-matter](https://github.com/jonschlinkert/gray-matter). It supports the following front matter formats:
- TOML
- YAML
- JSONIt also has an `Engine` trait interface for implementing your own parsers that work with gray_matter.
## Usage
### Add `gray_matter` as a dependency
Append this crate to the `Cargo.toml`:
```toml
[dependencies]
# other dependencies...
gray_matter = "0.2"
```### Basic parsing
```rust
use gray_matter::Matter;
use gray_matter::engine::YAML;
use serde::Deserialize;const INPUT: &str = r#"---
title: gray-matter-rs
tags:
- gray-matter
- rust
---
Some excerpt
---
Other stuff
"#;fn main() {
// Select one parser engine, such as YAML, and parse it
// into gray_matter's custom data type: `Pod`
let matter = Matter::::new();
let result = matter.parse(INPUT);// You can now inspect the data from gray_matter.
assert_eq!(result.content, "Some excerpt\n---\nOther stuff");
assert_eq!(result.excerpt, Some("Some excerpt".to_owned()));
assert_eq!(result.data.as_ref().unwrap()["title"].as_string(), Ok("gray-matter-rs".to_string()));
assert_eq!(result.data.as_ref().unwrap()["tags"][0].as_string(), Ok("gray-matter".to_string()));
assert_eq!(result.data.as_ref().unwrap()["tags"][1].as_string(), Ok("rust".to_string()));// The `Pod` data type can be a bit unwieldy, so
// you can also deserialize it into a custom struct
#[derive(Deserialize, Debug)]
struct FrontMatter {
title: String,
tags: Vec
}// Deserialize `result` manually:
let front_matter: FrontMatter = result.data.unwrap().deserialize().unwrap();
println!("{:?}", front_matter);
// FrontMatter { title: "gray-matter-rs", tags: ["gray-matter", "rust"] }// ...or skip a step, by using `parse_with_struct`.
let result_with_struct = matter.parse_with_struct::(INPUT).unwrap();
println!("{:?}", result_with_struct.data)
// FrontMatter { title: "gray-matter-rs", tags: ["gray-matter", "rust"] }
}
```### Custom Delimiters
The default delimiter is `---`, both for front matter and excerpts. You can change this by modifiying the `Matter` struct.
```rust
use gray_matter::{Matter, ParsedEntityStruct};
use gray_matter::engine::YAML;
use serde::Deserialize;fn main() {
let mut matter: Matter = Matter::new();
matter.delimiter = "~~~".to_owned();
matter.excerpt_delimiter = Some("".to_owned());#[derive(Deserialize, Debug)]
struct FrontMatter {
abc: String,
}let result: ParsedEntityStruct = matter.parse_with_struct(
"~~~\nabc: xyz\n~~~\nfoo\nbar\nbaz\n\ncontent",
).unwrap();
}
```#### Custom close delimiter
The open and close delimiter are the same by default (`---`). You can change this by modifiying `close_delimiter` property of `Matter` struct
```rust
use gray_matter::{Matter, ParsedEntityStruct};
use gray_matter::engine::YAML;
use serde::Deserialize;fn main() {
let mut matter: Matter = Matter::new();
matter.delimiter = "".to_owned());
matter.excerpt_delimiter = Some("".to_owned());#[derive(Deserialize, Debug)]
struct FrontMatter {
abc: String,
}let result: ParsedEntityStruct = matter.parse_with_struct(
"\nfoo\nbar\nbaz\n\ncontent",
).unwrap();
}
```## Contribution
If you need more parser engines, feel free to create a **PR** to help me complete this crate.