This commit is contained in:
parent
b5ae9deaf9
commit
fa3bdca37e
2 changed files with 36 additions and 12 deletions
|
@ -1,11 +1,10 @@
|
||||||
use actix_web::{get, web, HttpResponse, Responder};
|
use actix_web::{get, web, HttpResponse, Responder};
|
||||||
use cached::proc_macro::once;
|
use cached::proc_macro::once;
|
||||||
use glob::glob;
|
|
||||||
use ramhorns::Content;
|
use ramhorns::Content;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
config::Config,
|
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")]
|
#[get("/blog")]
|
||||||
|
@ -21,35 +20,59 @@ struct BlogIndexTemplate {
|
||||||
#[derive(Content, Debug)]
|
#[derive(Content, Debug)]
|
||||||
struct Post {
|
struct Post {
|
||||||
title: String,
|
title: String,
|
||||||
|
date: String,
|
||||||
url: String,
|
url: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[once(time = 60)]
|
#[once(time = 60)]
|
||||||
pub fn get_index(config: Config) -> String {
|
pub fn get_index(config: Config) -> String {
|
||||||
let location = "data/blog";
|
let location = "data/blog";
|
||||||
let paths = glob(&format!("{location}/*.md"))
|
|
||||||
|
let mut entries = std::fs::read_dir(location)
|
||||||
.unwrap()
|
.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| {
|
.map(|f| {
|
||||||
let filename = f
|
let _filename = f.file_name();
|
||||||
.unwrap()
|
let filename = _filename.to_string_lossy();
|
||||||
.file_name()
|
|
||||||
.unwrap()
|
|
||||||
.to_string_lossy()
|
|
||||||
.to_string();
|
|
||||||
let file_without_ext = filename.split_at(filename.len() - 3).0;
|
let file_without_ext = filename.split_at(filename.len() - 3).0;
|
||||||
|
|
||||||
let file_metadata = match std::fs::read_to_string(format!("{location}/{filename}")) {
|
let file_metadata = match std::fs::read_to_string(format!("{location}/{filename}")) {
|
||||||
Ok(text) => {
|
Ok(text) => {
|
||||||
let md_tree = get_md_asm(&text);
|
let md_tree = get_md_asm(&text);
|
||||||
let md_nodes = md_tree.children().unwrap();
|
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 {
|
Post {
|
||||||
url: file_without_ext.to_string(),
|
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>>();
|
.collect::<Vec<Post>>();
|
||||||
|
|
|
@ -63,6 +63,7 @@ impl FrontMatter<'_> {
|
||||||
pub struct FileMetadata {
|
pub struct FileMetadata {
|
||||||
pub title: Option<String>,
|
pub title: Option<String>,
|
||||||
pub link: Option<String>,
|
pub link: Option<String>,
|
||||||
|
pub date: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Content)]
|
#[derive(Content)]
|
||||||
|
|
Loading…
Reference in a new issue