Installation & Guide

Getting Started with Frontmatter Gen

Install the library crate or standalone CLI, extract YAML 1.2, TOML 1.1, and JSON headers, and synthesize HTML meta tags.

Frontmatter Gen (frontmatter-gen) is an open-source, pure-Rust library and CLI for extracting and parsing YAML 1.2, TOML 1.1, and JSON frontmatter from Markdown and text documents. Follow this guide to add the crate, run the fmg binary, or enforce DoS limits on untrusted inputs.

Core Library

Crate v0.0.15

Zero-copy extractor, YAML 1.2 / TOML 1.1 / JSON parsers, and HTML meta tag generator with minimal dependencies.

cargo add frontmatter-gen

Standalone CLI

Binary (fmg)

Install the standalone fmg command-line binary with clap and tokio for terminal and CI workflows.

cargo install frontmatter-gen --features cli

SSG Engine

Feature Flag

Enable the static site generation engine powered by tera templates and comrak Markdown rendering.

cargo add frontmatter-gen --features ssg

Rust Toolchain

MSRV 1.85.0+

Requires Rust 1.85.0 or newer (Edition 2024) and compiles cleanly across Linux, macOS, and Windows with zero unsafe code.

rustup update stable
01

Extract & Parse Frontmatter in One Call

The top-level frontmatter_gen::extract function detects the frontmatter format automatically, parses the header into a Frontmatter map, and returns the remaining Markdown body as a borrowed &str slice:

rust — src/main.rs
use frontmatter_gen::{extract, Value};
 
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let doc = "---\ntitle: Zero-Copy Rust\npublished: true\n---\n# Welcome\n";
    let (fm, body) = extract(doc)?;
 
    assert_eq!(fm.get("title"), Some(&Value::String("Zero-Copy Rust".into())));
    assert_eq!(fm.get("published"), Some(&Value::Bool(true)));
    assert!(body.starts_with("# Welcome"));
    Ok(())
}
02

Zero-Copy Raw Slicing & DoS Budget Guardrails

When processing untrusted Markdown uploads or inspecting raw slices before allocation, pair extractor::extract_raw_frontmatter with parser::parse_with_options:

rust — zero-copy & ParseOptions
use frontmatter_gen::extractor::{detect_format, extract_raw_frontmatter};
use frontmatter_gen::parser::{parse_with_options, ParseOptions};
 
# 1. Borrow (raw_header, body) without heap allocation
let (raw_header, body) = extract_raw_frontmatter(input)?;
let format = detect_format(raw_header);
 
# 2. Enforce structural depth and key-count budgets
let options = ParseOptions {
    validate: true,
    max_depth: Some(8),
    max_keys: Some(64),
};
let fm = parse_with_options(raw_header, format, &options)?;
03

Generate Escaped HTML Meta Tags & Use the CLI

Convert parsed frontmatter into HTML <meta>, Open Graph, and Twitter Card tags, or invoke the fmg binary directly from your shell:

bash — fmg cli quickstart
# Extract frontmatter from a Markdown file and print as formatted JSON
$fmg extract content/post.md --format json
 
# Write extracted frontmatter to a YAML file
$fmg extract content/post.md --format yaml --output build/meta.yaml
 
# Build a static site from content/ and templates/ into public/
$fmg build --content-dir content --template-dir templates --output-dir public
04

Memory-Safe Execution Guarantees

Every extraction and parsing pass executes strictly in local memory with #![forbid(unsafe_code)], verified under Miri Tree Borrows and continuous fuzzing:

Deterministic Rust document metadata extraction and parsing architecture
Zero-copy architecture: frontmatter boundaries are sliced in a single forward scan and parsed into strongly-typed Rust values with zero unsafe code.