From 1c85d7e72071a67a14a3018914bfa66a1336c4fc Mon Sep 17 00:00:00 2001 From: RGBCube Date: Mon, 8 Jan 2024 12:48:37 +0300 Subject: [PATCH] Add metadata --- Cargo.lock | 22 ++++++++++++++++++++++ Cargo.toml | 4 +++- src/routes/about.md | 3 +++ src/routes/markdown.rs | 27 +++++++++++++++++++++++---- 4 files changed, 51 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 451d01e..b6cd07a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -310,6 +310,7 @@ dependencies = [ "iana-time-zone", "js-sys", "num-traits", + "serde", "wasm-bindgen", "windows-targets 0.48.5", ] @@ -1165,6 +1166,19 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_yaml" +version = "0.9.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1bf28c79a99f70ee1f1d83d10c875d2e70618417fda01ad1785e027579d9d38" +dependencies = [ + "indexmap", + "itoa", + "ryu", + "serde", + "unsafe-libyaml", +] + [[package]] name = "signal-hook-registry" version = "1.4.1" @@ -1192,6 +1206,8 @@ dependencies = [ "mime_guess", "minify-js", "pulldown-cmark", + "serde", + "serde_yaml", "tokio", "tower", "tower-http", @@ -1435,6 +1451,12 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" +[[package]] +name = "unsafe-libyaml" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ab4c90930b95a82d00dc9e9ac071b4991924390d46cbd0dfe566148667605e4b" + [[package]] name = "untrusted" version = "0.9.0" diff --git a/Cargo.toml b/Cargo.toml index 1d2cc56..14a7676 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ axum = { version = "0.7.3", features = [ "macros" ] } axum-server = { version = "0.6.0", features = [ "tls-rustls" ] } bytes = "1.5.0" cargo_toml = "0.17.2" -chrono = "0.4.31" +chrono = { version = "0.4.31", features = [ "serde" ] } clap = { version = "4.4.12", features = [ "derive" ] } embed = { git = "https://github.com/RGBCube/embed-rs" } env_logger = "0.10.1" @@ -23,6 +23,8 @@ maud = { git = "https://github.com/lambda-fairy/maud", features = mime_guess = "2.0.4" minify-js = "0.6.0" pulldown-cmark = "0.9.3" +serde = { version = "1.0.195", features = [ "derive" ] } +serde_yaml = "0.9.30" tokio = { version = "1.35.1", features = [ "full" ] } tower = "0.4.13" tower-http = { version = "0.5.0", features = [ "trace" ] } diff --git a/src/routes/about.md b/src/routes/about.md index 106c4c9..82e27bb 100644 --- a/src/routes/about.md +++ b/src/routes/about.md @@ -1,3 +1,6 @@ +title: About +--- + ### Hi. Test 123 diff --git a/src/routes/markdown.rs b/src/routes/markdown.rs index 3067cbd..7b789ea 100644 --- a/src/routes/markdown.rs +++ b/src/routes/markdown.rs @@ -13,7 +13,9 @@ use axum::{ IntoResponse, }, }; +use chrono::NaiveDate; use maud::Markup; +use serde::Deserialize; use crate::{ errors::not_found, @@ -24,7 +26,17 @@ use crate::{ }, }; -static PAGES: LazyLock> = LazyLock::new(|| { +#[derive(Deserialize)] +struct Metadata { + title: String, + // TODO: Use these for blog articles. + #[allow(dead_code)] + date: Option, + #[allow(dead_code)] + tags: Option>, +} + +static PAGES: LazyLock> = LazyLock::new(|| { let routes_path = path::Path::new(file!()) .parent() .unwrap() @@ -44,10 +56,16 @@ static PAGES: LazyLock> = LazyLock::new(|| { continue; } + let content = String::from_utf8(file.content().to_vec()).unwrap(); + + let (metadata, content) = content.split_once("---").unwrap(); + + let metadata: Metadata = serde_yaml::from_str(metadata).unwrap(); + log::info!("Adding page {path}"); pages.insert( path.to_string().strip_suffix(".md").unwrap().to_string(), - markdown::parse(&String::from_utf8(file.content().to_vec()).unwrap()), + (metadata, markdown::parse(&content)), ); } @@ -55,8 +73,9 @@ static PAGES: LazyLock> = LazyLock::new(|| { }); pub async fn handler(Path(path): Path) -> Response { - if let Some(body) = PAGES.get(&path) { - Html(text::create(Some("test"), Page::from_str(&path), &body).into_string()).into_response() + if let Some((metadata, body)) = PAGES.get(&path) { + Html(text::create(Some(&metadata.title), Page::from_str(&path), &body).into_string()) + .into_response() } else { not_found::handler().await.into_response() }