1
Fork 0
mirror of https://github.com/RGBCube/Site synced 2025-08-01 13:37:49 +00:00

Sort blog entries

This commit is contained in:
RGBCube 2024-01-08 15:12:25 +03:00
parent 3c2a119e4c
commit 5d2c5197cd
No known key found for this signature in database
9 changed files with 96 additions and 36 deletions

1
.gitignore vendored
View file

@ -6,6 +6,7 @@
!src/page/cube/ !src/page/cube/
!src/page/text/ !src/page/text/
!src/routes/ !src/routes/
!src/routes/blog/
!src/routes/index/ !src/routes/index/
!.gitignore !.gitignore

17
Cargo.lock generated
View file

@ -367,6 +367,12 @@ version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f"
[[package]]
name = "either"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
[[package]] [[package]]
name = "embed" name = "embed"
version = "0.1.0" version = "0.1.0"
@ -674,6 +680,15 @@ dependencies = [
"windows-sys 0.52.0", "windows-sys 0.52.0",
] ]
[[package]]
name = "itertools"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25db6b064527c5d482d0423354fcd07a89a2dfe07b67892e62411946db7f07b0"
dependencies = [
"either",
]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.10" version = "1.0.10"
@ -1201,6 +1216,8 @@ dependencies = [
"clap", "clap",
"embed", "embed",
"env_logger", "env_logger",
"indexmap",
"itertools",
"log", "log",
"maud", "maud",
"mime_guess", "mime_guess",

View file

@ -18,6 +18,8 @@ chrono = { version = "0.4.31", features = [ "serde" ] }
clap = { version = "4.4.12", features = [ "derive" ] } clap = { version = "4.4.12", features = [ "derive" ] }
embed = { git = "https://github.com/RGBCube/embed-rs" } embed = { git = "https://github.com/RGBCube/embed-rs" }
env_logger = "0.10.1" env_logger = "0.10.1"
indexmap = "2.1.0"
itertools = "0.12.0"
log = { version = "0.4.20", features = [ "serde" ] } log = { version = "0.4.20", features = [ "serde" ] }
maud = { git = "https://github.com/lambda-fairy/maud", features = [ "axum" ] } maud = { git = "https://github.com/lambda-fairy/maud", features = [ "axum" ] }
mime_guess = "2.0.4" mime_guess = "2.0.4"

View file

@ -1,10 +1,17 @@
use std::sync::LazyLock;
use axum::extract::Path; use axum::extract::Path;
use indexmap::IndexMap;
use itertools::Itertools;
use maud::{ use maud::{
html, html,
Markup, Markup,
}; };
use super::markdown::PAGES; use super::markdown::{
Metadata,
PAGES,
};
use crate::{ use crate::{
errors::not_found, errors::not_found,
page::{ page::{
@ -13,24 +20,49 @@ use crate::{
}, },
}; };
static ENTRIES: LazyLock<IndexMap<&'static str, (&'static Metadata, Markup)>> =
LazyLock::new(|| {
IndexMap::from_iter(
PAGES
.iter()
.sorted_by_key(|(_, value)| value.0.date)
.rev()
.filter_map(|(path, (metadata, body))| {
if let Some(name) = path.strip_prefix("blog/") {
let body = html! {
(body)
@if let Some(tags) = &metadata.tags {
p {
"Tags: "
(tags.join(", "))
}
}
};
Some((name, (metadata, body)))
} else {
None
}
}),
)
});
pub async fn index_handler() -> Markup { pub async fn index_handler() -> Markup {
text::create( text::create(
Some("blog"), Some("Blog"),
Page::Blog, Page::Blog,
&html! { &html! {
h1 { "Blog Articles" } h1 { "Blog Articles" }
p { "RSS feed coming soon, probably :)" } p { "RSS feed coming soon, probably :)" }
ul { ul {
@let pages = &*PAGES; @for (path, (metadata, ..)) in ENTRIES.iter() {
@for (path, (metadata, ..)) in pages.iter() { li {
@if path.starts_with("blog") { (metadata.date.unwrap().format("%d/%m/%Y"))
li { " - "
(metadata.date.unwrap().format("%d/%m/%Y")) a href=(format!("/blog/{path}")) {
" - " (metadata.title)
a href=(format!("/{path}")) {
(metadata.title)
}
} }
} }
} }
@ -39,22 +71,9 @@ pub async fn index_handler() -> Markup {
) )
} }
pub async fn entry_handler(Path(path): Path<String>) -> Markup { pub async fn entry_handler(Path(entry): Path<String>) -> Markup {
if let Some((metadata, body)) = PAGES.get(&path) { if let Some((metadata, body)) = ENTRIES.get(entry.as_str()) {
text::create( text::create(Some(&metadata.title), Page::Other, &body)
Some(&metadata.title),
Page::Other,
&html! {
(body)
@if let Some(tags) = &metadata.tags {
p {
"Tags: "
(tags.join(", "))
}
}
},
)
} else { } else {
not_found::handler().await not_found::handler().await
} }

6
src/routes/blog/test.md Normal file
View file

@ -0,0 +1,6 @@
title: Oldest entry
date: 2022-01-01
tags:
- foo
- bar
---

View file

@ -0,0 +1,6 @@
title: Second oldest
date: 2022-01-05
tags:
- foo
- bar
---

View file

@ -0,0 +1,6 @@
title: Newest
date: 2024-01-08
tags:
- foo
- bar
---

View file

@ -0,0 +1,6 @@
title: 3rd oldest
date: 2023-01-08
tags:
- foo
- bar
---

View file

@ -32,9 +32,7 @@ pub static PAGES: LazyLock<HashMap<String, (Metadata, Markup)>> = LazyLock::new(
.canonicalize() .canonicalize()
.unwrap(); .unwrap();
let mut pages = HashMap::new(); HashMap::from_iter(embed::dir!(".").flatten().iter().filter_map(|file| {
for file in embed::dir!(".").flatten() {
let path = path::Path::new(file.path().as_ref()) let path = path::Path::new(file.path().as_ref())
.strip_prefix(&routes_path) .strip_prefix(&routes_path)
.unwrap() .unwrap()
@ -42,7 +40,7 @@ pub static PAGES: LazyLock<HashMap<String, (Metadata, Markup)>> = LazyLock::new(
.unwrap(); .unwrap();
if !path.ends_with(".md") { if !path.ends_with(".md") {
continue; return None;
} }
let content = String::from_utf8(file.content().to_vec()).unwrap(); let content = String::from_utf8(file.content().to_vec()).unwrap();
@ -52,13 +50,12 @@ pub static PAGES: LazyLock<HashMap<String, (Metadata, Markup)>> = LazyLock::new(
let metadata: Metadata = serde_yaml::from_str(metadata).unwrap(); let metadata: Metadata = serde_yaml::from_str(metadata).unwrap();
log::info!("Adding page {path}"); log::info!("Adding page {path}");
pages.insert(
Some((
path.to_string().strip_suffix(".md").unwrap().to_string(), path.to_string().strip_suffix(".md").unwrap().to_string(),
(metadata, markdown::parse(content)), (metadata, markdown::parse(content)),
); ))
} }))
pages
}); });
pub async fn handler(Path(path): Path<String>) -> Markup { pub async fn handler(Path(path): Path<String>) -> Markup {