Toml frontmatter

This commit is contained in:
2025-11-05 09:55:03 -08:00
parent 2f2f7c68f1
commit 726bf3cd36
6 changed files with 137 additions and 46 deletions

76
Cargo.lock generated
View File

@@ -2194,6 +2194,15 @@ dependencies = [
"serde_core",
]
[[package]]
name = "serde_spanned"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e24345aa0fe688594e73770a5f6d1b216508b4f93484c0026d521acd30134392"
dependencies = [
"serde_core",
]
[[package]]
name = "serde_urlencoded"
version = "0.7.1"
@@ -2206,19 +2215,6 @@ dependencies = [
"serde",
]
[[package]]
name = "serde_yaml"
version = "0.9.34+deprecated"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47"
dependencies = [
"indexmap",
"itoa",
"ryu",
"serde",
"unsafe-libyaml",
]
[[package]]
name = "service-webpage"
version = "0.0.1"
@@ -2237,9 +2233,10 @@ dependencies = [
"parking_lot",
"reqwest",
"serde",
"serde_yaml",
"strum",
"tokio",
"toml",
"toolbox",
"tower-http",
"tracing",
]
@@ -2587,6 +2584,45 @@ dependencies = [
"tokio",
]
[[package]]
name = "toml"
version = "0.9.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0dc8b1fb61449e27716ec0e1bdf0f6b8f3e8f6b05391e8497b8b6d7804ea6d8"
dependencies = [
"indexmap",
"serde_core",
"serde_spanned",
"toml_datetime",
"toml_parser",
"toml_writer",
"winnow",
]
[[package]]
name = "toml_datetime"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2cdb639ebbc97961c51720f858597f7f24c4fc295327923af55b74c3c724533"
dependencies = [
"serde_core",
]
[[package]]
name = "toml_parser"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0cbe268d35bdb4bb5a56a2de88d0ad0eb70af5384a99d648cd4b3d04039800e"
dependencies = [
"winnow",
]
[[package]]
name = "toml_writer"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df8b2b54733674ad286d16267dcfc7a71ed5c776e4ac7aa3c3e2561f7c637bf2"
[[package]]
name = "toolbox"
version = "0.0.1"
@@ -2802,12 +2838,6 @@ version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ebc1c04c71510c7f702b52b7c350734c9ff1295c464a03335b00bb84fc54f853"
[[package]]
name = "unsafe-libyaml"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861"
[[package]]
name = "untrusted"
version = "0.9.0"
@@ -3285,6 +3315,12 @@ version = "0.53.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6bbff5f0aada427a1e5a6da5f1f98158182f26556f345ac9e04d36d0ebed650"
[[package]]
name = "winnow"
version = "0.7.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21a0236b59786fed61e2a80582dd500fe61f18b5dca67a4a067d0bc9039339cf"
[[package]]
name = "wit-bindgen"
version = "0.46.0"

View File

@@ -12,6 +12,7 @@ libservice = { workspace = true }
macro-assets = { workspace = true }
macro-sass = { workspace = true }
assetserver = { workspace = true }
toolbox = { workspace = true }
page = { workspace = true }
axum = { workspace = true }
@@ -23,7 +24,7 @@ strum = { workspace = true }
chrono = { workspace = true }
parking_lot = { workspace = true }
lazy_static = { workspace = true }
serde_yaml = { workspace = true }
toml = { workspace = true }
serde = { workspace = true }
reqwest = { workspace = true }
tower-http = { workspace = true }

View File

@@ -16,10 +16,14 @@ lazy_static! {
let mut md = markdown_it::MarkdownIt::new();
markdown_it::plugins::cmark::add(&mut md);
markdown_it::plugins::html::add(&mut md);
md.block.add_rule::<YamlFrontMatter>().before_all();
md.block.add_rule::<TomlFrontMatter>().before_all();
md.inline.add_rule::<InlineEmote>();
md.inline.add_rule::<InlineEmote>();
md.inline.add_rule::<InlineMdx>();
md.block.add_rule::<FrontMatter>().before_all();
md
};
}
@@ -47,12 +51,12 @@ impl Markdown<'_> {
/// Try to read page metadata from a markdown file's frontmatter.
/// - returns `none` if there is no frontmatter
/// - returns an error if we fail to parse frontmatter
pub fn meta_from_markdown(root_node: &Node) -> Result<Option<PageMetadata>, serde_yaml::Error> {
pub fn meta_from_markdown(root_node: &Node) -> Result<Option<PageMetadata>, toml::de::Error> {
root_node
.children
.first()
.and_then(|x| x.cast::<FrontMatter>())
.map(|x| serde_yaml::from_str::<PageMetadata>(&x.content))
.and_then(|x| x.cast::<TomlFrontMatter>())
.map(|x| toml::from_str::<PageMetadata>(&x.content))
.map_or(Ok(None), |v| v.map(Some))
}
@@ -323,20 +327,20 @@ fn mdx_include(mdx: &str, _node: &Node, fmt: &mut dyn Renderer) -> bool {
}
//
// MARK: frontmatter
// MARK: yaml frontmatter
//
#[derive(Debug)]
/// AST node for front-matter
pub struct FrontMatter {
pub struct YamlFrontMatter {
#[expect(dead_code)]
pub content: String,
}
impl NodeValue for FrontMatter {
impl NodeValue for YamlFrontMatter {
fn render(&self, _node: &Node, _fmt: &mut dyn Renderer) {}
}
impl BlockRule for FrontMatter {
impl BlockRule for YamlFrontMatter {
fn run(state: &mut BlockState<'_, '_>) -> Option<(Node, usize)> {
// check the parent is the document Root
if !state.node.is::<Root>() {
@@ -373,6 +377,56 @@ impl BlockRule for FrontMatter {
}
let (content, _) = state.get_lines(state.line + 1, next_line, 0, true);
Some((Node::new(FrontMatter { content }), next_line + 1))
Some((Node::new(YamlFrontMatter { content }), next_line + 1))
}
}
//
// MARK: toml frontmatter
//
#[derive(Debug)]
pub struct TomlFrontMatter {
pub content: String,
}
impl NodeValue for TomlFrontMatter {
fn render(&self, _node: &Node, _fmt: &mut dyn Renderer) {}
}
impl BlockRule for TomlFrontMatter {
fn run(state: &mut BlockState<'_, '_>) -> Option<(Node, usize)> {
if !state.node.is::<Root>() {
return None;
}
if state.line != 0 {
return None;
}
let opening = state
.get_line(state.line)
.chars()
.take_while(|c| *c == '+')
.collect::<String>();
if !opening.starts_with("+++") {
return None;
}
let mut next_line = state.line;
loop {
next_line += 1;
if next_line >= state.line_max {
return None;
}
let line = state.get_line(next_line);
if line.starts_with(&opening) {
break;
}
}
let (content, _) = state.get_lines(state.line + 1, next_line, 0, true);
Some((Node::new(TomlFrontMatter { content }), next_line + 1))
}
}

View File

@@ -1,8 +1,8 @@
---
title: "What's a \"betalupi?\""
author: "Mark"
slug: whats-a-betalupi
---
+++
title = "What's a \"betalupi?\""
author = "Mark"
slug = "whats-a-betalupi"
+++
[es]: https://github.com/endless-sky/endless-sky
[*Stellaris*]: https://www.paradoxinteractive.com/games/stellaris/about

View File

@@ -1,8 +1,8 @@
---
title: Mark's Handouts
author: Mark
slug: handouts
---
+++
title = "Mark's Handouts"
author = "Mark"
slug = "handouts"
+++
# Mark's Handouts

View File

@@ -1,8 +1,8 @@
---
title: Links
author: Mark
slug: links
---
+++
title = "Links"
author = "Mark"
slug = "links"
+++
# Bookmarks