Git Product home page Git Product logo

yaml-front-matter's Introduction

yaml-front-matter

YAML Front Matter (YFM) parser for Markdown files

Crates.io Documentation Build Clippy Fmt Release Tests

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:

---
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

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<String>,
    similar_posts: Vec<String>,
    date: String,
    favorite_numbers: Vec<f64>,
}

let result = YamlFrontMatter::parse::<Metadata>(&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.]);

yaml-front-matter's People

Contributors

estebanborai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.