wip: date implementation
Some checks are pending
ci/woodpecker/push/publish Pipeline is pending

This commit is contained in:
Mylloon 2023-04-20 11:57:06 +02:00
parent b5ae9deaf9
commit fa3bdca37e
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 36 additions and 12 deletions

View file

@ -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::<Vec<std::fs::DirEntry>>();
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::<Vec<Post>>();

View file

@ -63,6 +63,7 @@ impl FrontMatter<'_> {
pub struct FileMetadata {
pub title: Option<String>,
pub link: Option<String>,
pub date: Option<String>,
}
#[derive(Content)]