From 2eefdafaba4f0a62c89f8cb72a2ebe3f0ca82860 Mon Sep 17 00:00:00 2001 From: Freddie Ridell Date: Tue, 17 Mar 2026 01:29:25 +0000 Subject: [PATCH] Replace custom frontmatter parsing with markdown-frontmatter crate --- Cargo.lock | 53 +++++++++++++++++++ crates/bough-docs-cli/Cargo.toml | 2 + crates/bough-docs-cli/src/build.rs | 81 +++++++++++++----------------- docs/index.md | 7 +-- docs/installation.md | 10 ++++ 5 files changed, 105 insertions(+), 48 deletions(-) create mode 100644 docs/installation.md diff --git a/Cargo.lock b/Cargo.lock index 02689630..3de0395f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -175,6 +175,8 @@ dependencies = [ "comrak", "facet", "figue", + "markdown-frontmatter", + "serde", "tiny_http", ] @@ -960,6 +962,17 @@ version = "0.4.29" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e5032e24019045c762d3c0f28f5b6b8bbf38563a65908389bf7978758920897" +[[package]] +name = "markdown-frontmatter" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02a46ec238a43080a53dc296b015d91fa033030d25bc05863f899915265afd5d" +dependencies = [ + "serde", + "thiserror", + "toml", +] + [[package]] name = "memchr" version = "2.8.0" @@ -1268,6 +1281,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" dependencies = [ "serde_core", + "serde_derive", ] [[package]] @@ -1304,6 +1318,15 @@ dependencies = [ "zmij", ] +[[package]] +name = "serde_spanned" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8bbf91e5a4d6315eee45e704372590b30e260ee83af6639d64557f51b067776" +dependencies = [ + "serde_core", +] + [[package]] name = "serial_test" version = "3.4.0" @@ -1536,6 +1559,30 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" +[[package]] +name = "toml" +version = "1.0.6+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "399b1124a3c9e16766831c6bba21e50192572cdd98706ea114f9502509686ffc" +dependencies = [ + "indexmap", + "serde_core", + "serde_spanned", + "toml_datetime", + "toml_parser", + "toml_writer", + "winnow", +] + +[[package]] +name = "toml_datetime" +version = "1.0.0+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32c2555c699578a4f59f0cc68e5116c8d7cabbd45e1409b989d4be085b53f13e" +dependencies = [ + "serde_core", +] + [[package]] name = "toml_parser" version = "1.0.9+spec-1.1.0" @@ -1545,6 +1592,12 @@ dependencies = [ "winnow", ] +[[package]] +name = "toml_writer" +version = "1.0.6+spec-1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab16f14aed21ee8bfd8ec22513f7287cd4a91aa92e44edfe2c17ddd004e92607" + [[package]] name = "tracing" version = "0.1.44" diff --git a/crates/bough-docs-cli/Cargo.toml b/crates/bough-docs-cli/Cargo.toml index 53446091..5312197a 100644 --- a/crates/bough-docs-cli/Cargo.toml +++ b/crates/bough-docs-cli/Cargo.toml @@ -16,3 +16,5 @@ facet = "0.44" figue = "2" comrak = { version = "0.36", default-features = false } tiny_http = "0.12" +markdown-frontmatter = { version = "0.5", default-features = false, features = ["toml"] } +serde = { version = "1.0.228", features = ["derive"] } diff --git a/crates/bough-docs-cli/src/build.rs b/crates/bough-docs-cli/src/build.rs index 2c3d44ae..cf7f58ea 100644 --- a/crates/bough-docs-cli/src/build.rs +++ b/crates/bough-docs-cli/src/build.rs @@ -2,35 +2,23 @@ use std::fs; use std::path::{Path, PathBuf}; use comrak::{Options, markdown_to_html}; +use serde::Deserialize; const DOCS_DIR: &str = "docs"; const OUT_DIR: &str = "target/bough-docs-site"; +#[derive(Deserialize, Default)] +struct Frontmatter { + #[serde(default)] + title: Option, +} + struct Page { title: String, slug: String, body_html: String, } -fn parse_frontmatter(content: &str) -> (&str, Vec<(&str, &str)>) { - let Some(rest) = content.strip_prefix("---\n") else { - return (content, Vec::new()); - }; - let Some(end) = rest.find("\n---\n") else { - return (content, Vec::new()); - }; - let fm_block = &rest[..end]; - let body = &rest[end + 5..]; - let fields = fm_block - .lines() - .filter_map(|line| { - let (key, val) = line.split_once(':')?; - Some((key.trim(), val.trim())) - }) - .collect(); - (body, fields) -} - fn collect_md_files(dir: &Path) -> Vec { let mut files = Vec::new(); let Ok(entries) = fs::read_dir(dir) else { @@ -52,20 +40,23 @@ fn render_page(page: &Page) -> String { format!( r#" - - - - - - + + + + + - - -{title} - - -{body} - + + + {title} + + + +
+

{title}

+ {body} +
+ "#, title = page.title, body = page.body_html, @@ -97,18 +88,18 @@ pub fn build() { .iter() .map(|path| { let content = fs::read_to_string(path).expect("failed to read md file"); - let (body, fields) = parse_frontmatter(&content); - - let title = fields - .iter() - .find(|(k, _)| *k == "title") - .map(|(_, v)| v.to_string()) - .unwrap_or_else(|| { - path.file_stem() - .unwrap_or_default() - .to_string_lossy() - .into_owned() - }); + + let (fm, body) = match markdown_frontmatter::parse::(&content) { + Ok((fm, body)) => (fm, body.to_owned()), + Err(_) => (Frontmatter::default(), content.clone()), + }; + + let title = fm.title.unwrap_or_else(|| { + path.file_stem() + .unwrap_or_default() + .to_string_lossy() + .into_owned() + }); let rel = path.strip_prefix(docs_dir).unwrap(); let slug = if rel.file_stem().is_some_and(|s| s == "index") { @@ -119,7 +110,7 @@ pub fn build() { rel.with_extension("").to_string_lossy().into_owned() }; - let body_html = markdown_to_html(body, &opts); + let body_html = markdown_to_html(&body, &opts); Page { title, diff --git a/docs/index.md b/docs/index.md index 473972b9..b78f64f5 100644 --- a/docs/index.md +++ b/docs/index.md @@ -1,6 +1,7 @@ ---- -title: Bough ---- ++++ +title = "Bough" +idx = 0 ++++ Hello World diff --git a/docs/installation.md b/docs/installation.md new file mode 100644 index 00000000..50204df9 --- /dev/null +++ b/docs/installation.md @@ -0,0 +1,10 @@ ++++ +title = "Installing Bough" +idx = 1 ++++ + +While `bough` is still in alpha, the recommended way to install it is directly via `cargo`: + +```bash +cargo install --locked --git https://github.com/CodeEnPlace/bough --bin bough +``` -- knotmirror 0.0.1