|
|
|
|
@@ -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))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|