https://github.com/leoborai/yaml-front-matter
Parses a valid YAML string into a struct which implements the DeserializeOwned trait from serde
https://github.com/leoborai/yaml-front-matter
front-matter markdown parser rust yaml
Last synced: 7 months ago
JSON representation
Parses a valid YAML string into a struct which implements the DeserializeOwned trait from serde
- Host: GitHub
- URL: https://github.com/leoborai/yaml-front-matter
- Owner: LeoBorai
- License: apache-2.0
- Created: 2021-09-23T02:27:24.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2021-09-26T14:35:39.000Z (almost 5 years ago)
- Last Synced: 2025-04-14T07:09:12.937Z (over 1 year ago)
- Topics: front-matter, markdown, parser, rust, yaml
- Language: Rust
- Homepage: https://crates.io/crates/yaml-front-matter
- Size: 21.5 KB
- Stars: 15
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG
- License: LICENSE-APACHE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
yaml-front-matter
YAML Front Matter (YFM) parser for Markdown files
[](https://crates.io/crates/yaml-front-matter)
[](https://docs.rs/yaml-front-matter)





# YAML Front Matter (YFM) Parser
**yaml-front-matter** parses a valid YAML string into a `struct` which
implements the `DeserializeOwned` trait from serde.
Consider the following YAML content on the top of your markdown file:
```yml
---
title: 'Parsing a Markdown file metadata into a struct'
description: 'This tutorial walks you through the practice of parsing markdown files for metadata'
tags: ['markdown', 'rust', 'files', 'parsing', 'metadata']
similar_posts:
- 'Rendering markdown'
- 'Using Rust to render markdown'
date: '2021-09-13T03:48:00'
favorite_numbers:
- 3.14
- 1970
- 12345
---
```
This crate takes care of extracting this header from your markdown file and
parse extracted data using `serde` and `serde_yaml`.
## Example
```rust
use serde::Deserialize;
use yaml_front_matter::YamlFrontMatter;
const SIMPLE_MARKDOWN_YFM: &str = r#"
---
title: 'Parsing a Markdown file metadata into a struct'
description: 'This tutorial walks you through the practice of parsing markdown files for metadata'
tags: ['markdown', 'rust', 'files', 'parsing', 'metadata']
similar_posts:
- 'Rendering markdown'
- 'Using Rust to render markdown'
date: '2021-09-13T03:48:00'
favorite_numbers:
- 3.14
- 1970
- 12345
---
# Parsing a **Markdown** file metadata into a `struct`
> This tutorial walks you through the practice of parsing markdown files for metadata
"#;
#[derive(Deserialize)]
struct Metadata {
title: String,
description: String,
tags: Vec,
similar_posts: Vec,
date: String,
favorite_numbers: Vec,
}
let result = YamlFrontMatter::parse::(&SIMPLE_MARKDOWN_YFM).unwrap();
let Metadata {
title,
description,
tags,
similar_posts,
date,
favorite_numbers,
} = result;
assert_eq!(title, "Parsing a Markdown file metadata into a struct");
assert_eq!(
description,
"This tutorial walks you through the practice of parsing markdown files for metadata"
);
assert_eq!(
tags,
vec!["markdown", "rust", "files", "parsing", "metadata"]
);
assert_eq!(
similar_posts,
vec!["Rendering markdown", "Using Rust to render markdown"]
);
assert_eq!(date, "2021-09-13T03:48:00");
assert_eq!(favorite_numbers, vec![3.14, 1970., 12345.]);
```