mirror of
https://github.com/RGBCube/Site
synced 2025-08-01 13:37:49 +00:00
Add blog stuff
This commit is contained in:
parent
1c85d7e720
commit
9a292fce5b
3 changed files with 74 additions and 22 deletions
61
src/routes/blog.rs
Normal file
61
src/routes/blog.rs
Normal file
|
@ -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<String>) -> 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
|
||||
}
|
||||
}
|
|
@ -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<NaiveDate>,
|
||||
#[allow(dead_code)]
|
||||
tags: Option<Vec<String>>,
|
||||
#[derive(Deserialize, Debug)]
|
||||
pub struct Metadata {
|
||||
pub title: String,
|
||||
pub date: Option<NaiveDate>,
|
||||
pub tags: Option<Vec<String>>,
|
||||
}
|
||||
|
||||
static PAGES: LazyLock<HashMap<String, (Metadata, Markup)>> = LazyLock::new(|| {
|
||||
pub static PAGES: LazyLock<HashMap<String, (Metadata, Markup)>> = LazyLock::new(|| {
|
||||
let routes_path = path::Path::new(file!())
|
||||
.parent()
|
||||
.unwrap()
|
||||
|
@ -72,11 +61,10 @@ static PAGES: LazyLock<HashMap<String, (Metadata, Markup)>> = LazyLock::new(|| {
|
|||
pages
|
||||
});
|
||||
|
||||
pub async fn handler(Path(path): Path<String>) -> Response<Body> {
|
||||
pub async fn handler(Path(path): Path<String>) -> 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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue