diff --git a/src/routes/blog.rs b/src/routes/blog.rs new file mode 100644 index 0000000..26cbb26 --- /dev/null +++ b/src/routes/blog.rs @@ -0,0 +1,61 @@ +use axum::extract::Path; +use maud::{ + html, + Markup, +}; + +use super::markdown::PAGES; +use crate::{ + errors::not_found, + page::{ + text, + Page, + }, +}; + +pub async fn index_handler() -> Markup { + text::create( + Some("blog"), + Page::Blog, + &html! { + h1 { "Blog Articles" } + p { "RSS feed coming soon, probably :)" } + + ul { + @let pages = &*PAGES; + @for (path, (metadata, ..)) in pages.iter() { + @if path.starts_with("blog") { + li { + (metadata.date.unwrap().format("%d/%m/%Y")) + " - " + a href=(format!("/{path}")) { + (metadata.title) + } + } + } + } + } + }, + ) +} + +pub async fn entry_handler(Path(path): Path) -> Markup { + if let Some((metadata, body)) = PAGES.get(&path) { + text::create( + Some(&metadata.title), + Page::Other, + &html! { + (body) + + @if let Some(tags) = &metadata.tags { + p { + "Tags: " + (tags.join(", ")) + } + } + }, + ) + } else { + not_found::handler().await + } +} diff --git a/src/routes/markdown.rs b/src/routes/markdown.rs index 7b789ea..8663646 100644 --- a/src/routes/markdown.rs +++ b/src/routes/markdown.rs @@ -4,15 +4,7 @@ use std::{ sync::LazyLock, }; -use axum::{ - body::Body, - extract::Path, - http::Response, - response::{ - Html, - IntoResponse, - }, -}; +use axum::extract::Path; use chrono::NaiveDate; use maud::Markup; use serde::Deserialize; @@ -26,17 +18,14 @@ use crate::{ }, }; -#[derive(Deserialize)] -struct Metadata { - title: String, - // TODO: Use these for blog articles. - #[allow(dead_code)] - date: Option, - #[allow(dead_code)] - tags: Option>, +#[derive(Deserialize, Debug)] +pub struct Metadata { + pub title: String, + pub date: Option, + pub tags: Option>, } -static PAGES: LazyLock> = LazyLock::new(|| { +pub static PAGES: LazyLock> = LazyLock::new(|| { let routes_path = path::Path::new(file!()) .parent() .unwrap() @@ -72,11 +61,10 @@ static PAGES: LazyLock> = LazyLock::new(|| { pages }); -pub async fn handler(Path(path): Path) -> Response { +pub async fn handler(Path(path): Path) -> Markup { if let Some((metadata, body)) = PAGES.get(&path) { - Html(text::create(Some(&metadata.title), Page::from_str(&path), &body).into_string()) - .into_response() + text::create(Some(&metadata.title), Page::from_str(&path), &body) } else { - not_found::handler().await.into_response() + not_found::handler().await } } diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 77c9c99..65aa6ee 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -4,12 +4,15 @@ use axum::{ }; mod assets; +mod blog; mod index; mod markdown; pub fn router() -> Router { Router::new() .route("/", get(index::handler)) + .route("/blog", get(blog::index_handler)) + .route("/blog/:entry", get(blog::entry_handler)) .route("/*page", get(markdown::handler)) .route("/assets/*path", get(assets::handler)) }