diff --git a/src/routes/blog.rs b/src/routes/blog.rs index 23ca589..2c3f53f 100644 --- a/src/routes/blog.rs +++ b/src/routes/blog.rs @@ -1,11 +1,10 @@ use actix_web::{get, web, HttpResponse, Responder}; use cached::proc_macro::once; -use glob::glob; use ramhorns::Content; use crate::{ config::Config, - template::{get_md_asm, get_md_metadata, read_md_file, File, Infos}, + template::{get_md_asm, get_md_metadata, read_md_file, File, FileMetadata, Infos}, }; #[get("/blog")] @@ -21,35 +20,59 @@ struct BlogIndexTemplate { #[derive(Content, Debug)] struct Post { title: String, + date: String, url: String, } #[once(time = 60)] pub fn get_index(config: Config) -> String { let location = "data/blog"; - let paths = glob(&format!("{location}/*.md")) + + let mut entries = std::fs::read_dir(location) .unwrap() + .flatten() + .filter(|f| f.path().extension().unwrap() == "md") + .collect::>(); + + entries.sort_by_cached_key(|f| { + f.metadata() + .unwrap() + .modified() + .unwrap_or(f.metadata().unwrap().created().unwrap()) + }); + entries.reverse(); + + let paths = entries + .iter() .map(|f| { - let filename = f - .unwrap() - .file_name() - .unwrap() - .to_string_lossy() - .to_string(); + let _filename = f.file_name(); + let filename = _filename.to_string_lossy(); let file_without_ext = filename.split_at(filename.len() - 3).0; let file_metadata = match std::fs::read_to_string(format!("{location}/{filename}")) { Ok(text) => { let md_tree = get_md_asm(&text); let md_nodes = md_tree.children().unwrap(); - get_md_metadata(md_nodes).title + let mut metadata = get_md_metadata(md_nodes); + + metadata.title = match metadata.title { + Some(t) => Some(t), + None => Some(file_without_ext.to_string()), + }; + + metadata } - _ => None, + Err(_) => FileMetadata { + title: Some(file_without_ext.to_string()), + link: None, + date: None, + }, }; Post { url: file_without_ext.to_string(), - title: file_metadata.unwrap_or(file_without_ext.to_string()), + title: file_metadata.title.unwrap(), + date: file_metadata.date.unwrap_or_default(), } }) .collect::>(); diff --git a/src/template.rs b/src/template.rs index d0c8503..a7b4998 100644 --- a/src/template.rs +++ b/src/template.rs @@ -63,6 +63,7 @@ impl FrontMatter<'_> { pub struct FileMetadata { pub title: Option, pub link: Option, + pub date: Option, } #[derive(Content)]