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/text/
!src/routes/
!src/routes/blog/
!src/routes/index/
!.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"
checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f"
[[package]]
name = "either"
version = "1.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07"
[[package]]
name = "embed"
version = "0.1.0"
@ -674,6 +680,15 @@ dependencies = [
"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]]
name = "itoa"
version = "1.0.10"
@ -1201,6 +1216,8 @@ dependencies = [
"clap",
"embed",
"env_logger",
"indexmap",
"itertools",
"log",
"maud",
"mime_guess",

View file

@ -18,6 +18,8 @@ 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"
indexmap = "2.1.0"
itertools = "0.12.0"
log = { version = "0.4.20", features = [ "serde" ] }
maud = { git = "https://github.com/lambda-fairy/maud", features = [ "axum" ] }
mime_guess = "2.0.4"

View file

@ -1,10 +1,17 @@
use std::sync::LazyLock;
use axum::extract::Path;
use indexmap::IndexMap;
use itertools::Itertools;
use maud::{
html,
Markup,
};
use super::markdown::PAGES;
use super::markdown::{
Metadata,
PAGES,
};
use crate::{
errors::not_found,
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 {
text::create(
Some("blog"),
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)
}
@for (path, (metadata, ..)) in ENTRIES.iter() {
li {
(metadata.date.unwrap().format("%d/%m/%Y"))
" - "
a href=(format!("/blog/{path}")) {
(metadata.title)
}
}
}
@ -39,22 +71,9 @@ pub async fn index_handler() -> Markup {
)
}
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(", "))
}
}
},
)
pub async fn entry_handler(Path(entry): Path<String>) -> Markup {
if let Some((metadata, body)) = ENTRIES.get(entry.as_str()) {
text::create(Some(&metadata.title), Page::Other, &body)
} else {
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()
.unwrap();
let mut pages = HashMap::new();
for file in embed::dir!(".").flatten() {
HashMap::from_iter(embed::dir!(".").flatten().iter().filter_map(|file| {
let path = path::Path::new(file.path().as_ref())
.strip_prefix(&routes_path)
.unwrap()
@ -42,7 +40,7 @@ pub static PAGES: LazyLock<HashMap<String, (Metadata, Markup)>> = LazyLock::new(
.unwrap();
if !path.ends_with(".md") {
continue;
return None;
}
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();
log::info!("Adding page {path}");
pages.insert(
Some((
path.to_string().strip_suffix(".md").unwrap().to_string(),
(metadata, markdown::parse(content)),
);
}
pages
))
}))
});
pub async fn handler(Path(path): Path<String>) -> Markup {