yaml-front-matter

YAML Front Matter (YFM) parser for Markdown files

[![Crates.io](https://img.shields.io/crates/v/yaml-front-matter.svg)](https://crates.io/crates/yaml-front-matter) [![Documentation](https://docs.rs/yaml-front-matter/badge.svg)](https://docs.rs/yaml-front-matter) ![Build](https://github.com/EstebanBorai/yaml-front-matter/workflows/build/badge.svg) ![Clippy](https://github.com/EstebanBorai/yaml-front-matter/workflows/clippy/badge.svg) ![Fmt](https://github.com/EstebanBorai/yaml-front-matter/workflows/fmt/badge.svg) ![Release](https://github.com/EstebanBorai/yaml-front-matter/workflows/release/badge.svg) ![Tests](https://github.com/EstebanBorai/yaml-front-matter/workflows/tests/badge.svg)

# 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'] similarposts: - 'Rendering markdown' - 'Using Rust to render markdown' date: '2021-09-13T03:48:00' favoritenumbers: - 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 yamlfrontmatter::YamlFrontMatter;

const SIMPLEMARKDOWNYFM: &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'] similarposts: - 'Rendering markdown' - 'Using Rust to render markdown' date: '2021-09-13T03:48:00' favoritenumbers: - 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, similarposts: Vec, date: String, favoritenumbers: Vec, }

let result = YamlFrontMatter::parse::(&SIMPLEMARKDOWNYFM).unwrap();

let Metadata { title, description, tags, similarposts, date, favoritenumbers, } = result;

asserteq!(title, "Parsing a Markdown file metadata into a struct"); asserteq!( description, "This tutorial walks you through the practice of parsing markdown files for metadata" ); asserteq!( tags, vec!["markdown", "rust", "files", "parsing", "metadata"] ); asserteq!( similarposts, vec!["Rendering markdown", "Using Rust to render markdown"] ); asserteq!(date, "2021-09-13T03:48:00"); asserteq!(favoritenumbers, vec![3.14, 1970., 12345.]); ```