A simple filesystem-based Markdown page loader.
Reading a page
FlatPage::by_url returns Ok(None) for invalid URLs and missing pages. It
returns Err only for I/O failures and frontmatter parsing errors.
let root_folder = "./pages";
if let Some = by_url.unwrap else
Extra frontmatter fields
You can define extra statically typed frontmatter fields.
if let Some = by_url.unwrap
Cached metadata
You can cache page metadata (titles and descriptions) using
[FlatPageStore]. This lets you check if page exists or search for sub-pages
without filesystem calls.
let root_folder = "./pages";
let store = read_dir.unwrap;
// Reading metadata
if let Some = store.meta_by_url else
// Reading pages
if let Some = store..unwrap else
// Checking if a page exists without filesystem calls
if store.contains_url
// Searching for sub-pages without filesystem calls
let prefix = "/foo/";
let sub_page_metas = store
.iter
.filter;
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.
| Url | File name |
|---|---|
/ |
index.md |
/foo |
foo.md |
/foo/ |
foo/index.md |
/foo/bar |
foo/bar.md |
Page format
A file can provide a title and description in frontmatter. flatpage proxies
markdown-frontmatter features, so you can parse YAML (---), TOML (+++),
and JSON ({ ... }) depending on the enabled features. If there's no
frontmatter, the first non-empty line is considered the title. For ATX headings,
flatpage strips the heading markers but preserves the remaining Markdown.
| File content | title |
description |
body |
html() |
|---|---|---|---|---|
# FooBar |
"Foo" |
None |
"# Foo\nBar" |
"<h1>Foo</h1>\n<p>Bar</p>\n" |
+++description = "Bar"+++# Foo |
"Foo" |
Some("Bar") |
"# Foo" |
"<h1>Foo</h1>\n" |
---title: Foodescription: Bar--- |
"Foo" |
Some("Bar") |
"" |
"" |
Features
yaml: enable YAML frontmatter supporttoml: enable TOML frontmatter supportjson: enable JSON frontmatter supportfull: enable all formats (json,toml,yaml) - enabled by default
Contributing
Please run .pre-commit.sh before sending a PR, it will check everything.
License
This project is licensed under the MIT license.