Toml frontmatter
This commit is contained in:
@@ -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 }
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
---
|
||||
title: Mark's Handouts
|
||||
author: Mark
|
||||
slug: handouts
|
||||
---
|
||||
+++
|
||||
title = "Mark's Handouts"
|
||||
author = "Mark"
|
||||
slug = "handouts"
|
||||
+++
|
||||
|
||||
# Mark's Handouts
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
---
|
||||
title: Links
|
||||
author: Mark
|
||||
slug: links
|
||||
---
|
||||
+++
|
||||
title = "Links"
|
||||
author = "Mark"
|
||||
slug = "links"
|
||||
+++
|
||||
|
||||
|
||||
# Bookmarks
|
||||
|
||||
Reference in New Issue
Block a user