This commit is contained in:
parent
0fd74dd449
commit
d9c9ff0828
2 changed files with 25 additions and 19 deletions
|
@ -1,5 +1,6 @@
|
|||
use actix_web::{get, web, HttpResponse, Responder};
|
||||
use cached::proc_macro::once;
|
||||
use chrono::{DateTime, Datelike, Utc};
|
||||
use ramhorns::Content;
|
||||
|
||||
use crate::{
|
||||
|
@ -28,22 +29,13 @@ struct Post {
|
|||
pub fn get_index(config: Config) -> String {
|
||||
let location = "data/blog";
|
||||
|
||||
let mut entries = std::fs::read_dir(location)
|
||||
let entries = std::fs::read_dir(location)
|
||||
.unwrap()
|
||||
.flatten()
|
||||
.filter(|f| f.path().extension().unwrap() == "md")
|
||||
.collect::<Vec<std::fs::DirEntry>>();
|
||||
|
||||
// Sort by latest modification/ date of creation
|
||||
entries.sort_by_cached_key(|f| {
|
||||
f.metadata()
|
||||
.unwrap()
|
||||
.modified()
|
||||
.unwrap_or(f.metadata().unwrap().created().unwrap())
|
||||
});
|
||||
entries.reverse();
|
||||
|
||||
let paths = entries
|
||||
let mut posts = entries
|
||||
.iter()
|
||||
.map(|f| {
|
||||
let _filename = f.file_name();
|
||||
|
@ -73,17 +65,31 @@ pub fn get_index(config: Config) -> String {
|
|||
Post {
|
||||
url: file_without_ext.to_string(),
|
||||
title: file_metadata.title.unwrap(),
|
||||
date: file_metadata.date.unwrap_or_default(),
|
||||
date: file_metadata.date.unwrap_or({
|
||||
let m = f.metadata().unwrap();
|
||||
let date = std::convert::Into::<DateTime<Utc>>::into(
|
||||
m.modified().unwrap_or(m.created().unwrap()),
|
||||
)
|
||||
.date_naive();
|
||||
|
||||
Date {
|
||||
day: date.day(),
|
||||
month: date.month(),
|
||||
year: date.year(),
|
||||
}
|
||||
}),
|
||||
}
|
||||
})
|
||||
.collect::<Vec<Post>>();
|
||||
|
||||
// TODO sort vec by post date metadata, removing the sort above
|
||||
// Sort from newest to oldest
|
||||
posts.sort_by_cached_key(|p| p.date.clone());
|
||||
posts.reverse();
|
||||
|
||||
config.tmpl.render(
|
||||
"blog/index.html",
|
||||
BlogIndexTemplate {
|
||||
posts: if paths.is_empty() { None } else { Some(paths) },
|
||||
posts: if posts.is_empty() { None } else { Some(posts) },
|
||||
},
|
||||
Infos {
|
||||
page_title: Some("Blog".to_string()),
|
||||
|
|
|
@ -59,11 +59,11 @@ impl FrontMatter<'_> {
|
|||
}
|
||||
}
|
||||
}
|
||||
#[derive(Content, Default, Debug)]
|
||||
#[derive(Content, Default, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct Date {
|
||||
day: u32,
|
||||
month: u32,
|
||||
year: i32,
|
||||
pub day: u32,
|
||||
pub month: u32,
|
||||
pub year: i32,
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for Date {
|
||||
|
@ -85,7 +85,7 @@ impl<'de> Deserialize<'de> for Date {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Deserialize, Content, Debug)]
|
||||
#[derive(Default, Deserialize, Content)]
|
||||
pub struct FileMetadata {
|
||||
pub title: Option<String>,
|
||||
pub link: Option<String>,
|
||||
|
|
Loading…
Reference in a new issue