Skip to main content

Crate flatpage

Crate flatpage 

Source
Expand description

§flatpage

A simple filesystem-based Markdown page loader.

§Reading a page

Reading a page with an optional DefaultFrontmatter is as simple as:

let root_folder = "./pages";
if let Some(home) = flatpage::by_url(root_folder, "/").unwrap() {
    println!("frontmatter: {:?}", home.frontmatter);
    println!("markdown body: {}", home.body());
    println!("html body: {}", home.html());
} else {
    println!("No home page");
}

§Custom frontmatter

You can define your own frontmatter type when you need a custom schema. Any type that implements serde::Deserialize can be parsed.

#[derive(Debug, serde::Deserialize)]
struct Frontmatter {
    slug: String,
}

if let Some(page) = flatpage::FlatPage::<Frontmatter>::by_url("./pages", "/").unwrap() {
    println!("slug: {}", page.frontmatter.slug);
}

§Page title

To extract title from pages with different frontmatter structure or without frontmatter at all, implement TitleFrontmatter trait. It makes available FlatPage::title which looks into TitleFrontmatter::title first and then parse the title from the markdown body.

#[derive(Debug, serde::Deserialize)]
struct Frontmatter {
    title: String,
}

impl flatpage::TitleFrontmatter for Frontmatter {
    fn title(&self) -> Option<&str> {
        Some(&self.title)
    }
}

if let Some(page) = flatpage::FlatPage::<Frontmatter>::by_url("./pages", "/").unwrap() {
    println!("title: {}", page.title());
}

§Folder structure

The only characters allowed in URL segments are ASCII letters, numbers, hyphens, underscores, and dots. URLs map to nested Markdown files, and index.md is used for / and folder index pages. Trailing slashes are significant, so /foo and /foo/ map to different files. Empty path segments plus . and .. are rejected.

UrlFile name
/index.md
/foofoo.md
/foo/foo/index.md
/foo/barfoo/bar.md

§Features

  • yaml: enable YAML frontmatter support
  • toml: enable TOML frontmatter support
  • json: enable JSON frontmatter support
  • full: enable all formats (json, toml, yaml) - enabled by default

Structs§

DefaultFrontmatter
Default frontmatter fields used by crate::FlatPage.
FlatPage
Flat page with a fully typed frontmatter schema.

Enums§

Error
The crate’s error type

Traits§

TitleFrontmatter
Frontmatter hooks for crate::FlatPage.

Functions§

by_url
Returns a page with the default frontmatter schema by URL.

Type Aliases§

Result
The crate’s result type