From 2215f3e4e855420e13e14087074bc3ffdf4f6927 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Mon, 10 Apr 2023 19:16:25 +0200 Subject: [PATCH] add yaml frontmatter --- Cargo.lock | 20 ++++++++++++++++++++ Cargo.toml | 1 + src/template.rs | 28 +++++++++++++++++----------- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fe6508e..6fe07ab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -468,6 +468,7 @@ dependencies = [ "minify-html", "ramhorns", "serde", + "serde_yaml", "toml", ] @@ -1143,6 +1144,19 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_yaml" +version = "0.9.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9d684e3ec7de3bf5466b32bd75303ac16f0736426e5a4e0d6e489559ce1249c" +dependencies = [ + "indexmap", + "itoa", + "ryu", + "serde", + "unsafe-libyaml", +] + [[package]] name = "sha1" version = "0.10.5" @@ -1386,6 +1400,12 @@ version = "1.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36" +[[package]] +name = "unsafe-libyaml" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1865806a559042e51ab5414598446a5871b561d21b6764f2eabb0dd481d880a6" + [[package]] name = "url" version = "2.3.1" diff --git a/Cargo.toml b/Cargo.toml index 8632d66..f8e35c5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ actix-files = "0.6" ramhorns = "0.14.0" toml = "0.7.3" serde = { version = "1.0.159", features = ["derive"] } +serde_yaml = "0.9" minify-html = "0.10.8" glob = "0.3.1" markdown = "1.0.0-alpha.7" diff --git a/src/template.rs b/src/template.rs index 9f30c98..229cd59 100644 --- a/src/template.rs +++ b/src/template.rs @@ -1,4 +1,5 @@ use ramhorns::{Content, Ramhorns}; +use serde::Deserialize; #[derive(Clone)] pub struct Template { @@ -21,16 +22,21 @@ enum FrontMatter<'a> { } impl FrontMatter<'_> { - fn parse(&self) -> String { + fn parse(&self) -> Option { match self { - Self::Yaml(_val) => todo!(), + Self::Yaml(val) => serde_yaml::from_str(val).unwrap_or_default(), Self::Toml(_val) => todo!(), Self::Json(_val) => todo!(), } } } -pub fn read_md(filename: &str) -> String { +#[derive(Deserialize)] +pub struct Metadata { + pub title: Option, +} + +pub fn read_md(filename: &str) -> (Option, String) { // Read markdown file let mut text = std::fs::read_to_string(filename).unwrap(); @@ -53,22 +59,22 @@ pub fn read_md(filename: &str) -> String { }; let md_tree = markdown::to_mdast(&text, &parse_option).unwrap(); - let frontmatter = match &md_tree.children().unwrap()[0] { - markdown::mdast::Node::Yaml(v) => Some(FrontMatter::Yaml(&v.value).parse()), - markdown::mdast::Node::Toml(v) => Some(FrontMatter::Toml(&v.value).parse()), - markdown::mdast::Node::MdxjsEsm(v) => Some(FrontMatter::Json(&v.value).parse()), + let metadata = match &md_tree.children().unwrap()[0] { + markdown::mdast::Node::Yaml(v) => FrontMatter::Yaml(&v.value).parse(), + markdown::mdast::Node::Toml(v) => FrontMatter::Toml(&v.value).parse(), + markdown::mdast::Node::MdxjsEsm(v) => FrontMatter::Json(&v.value).parse(), _ => None, }; - println!("{:#?}\nVS", &md_tree.children().unwrap()[0]); - println!("{:#?}", frontmatter); // Convert to HTML - markdown::to_html_with_options( + let html = markdown::to_html_with_options( &text, &markdown::Options { parse: parse_option, compile: compile_option, }, ) - .unwrap() + .unwrap(); + + (metadata, html) }