diff --git a/src/misc/mod.rs b/src/misc/mod.rs index 0c8ab90..683ac39 100644 --- a/src/misc/mod.rs +++ b/src/misc/mod.rs @@ -1,2 +1,2 @@ pub mod github; -mod utils; +pub mod utils; diff --git a/src/misc/utils.rs b/src/misc/utils.rs index b740952..2ecbea4 100644 --- a/src/misc/utils.rs +++ b/src/misc/utils.rs @@ -1,6 +1,8 @@ use cached::proc_macro::cached; use reqwest::Client; +use crate::template::{read_md_file, File, Infos}; + #[cached] pub fn get_reqwest_client() -> Client { Client::builder() @@ -8,3 +10,24 @@ pub fn get_reqwest_client() -> Client { .build() .unwrap() } + +pub fn get_post(post: &mut Option, file: String) -> Infos { + let blog_dir = "data/blog"; + let ext = ".md"; + + *post = read_md_file(&format!("{blog_dir}/{file}{ext}")); + + let title = match post { + Some(data) => match &data.metadata.info.title { + Some(text) => text, + None => &file, + }, + None => &file, + }; + + Infos { + page_title: Some(format!("Post: {}", title)), + page_desc: Some("Blog d'Anri".to_string()), + page_kw: Some(["blog", "blogging", "write", "writing"].join(", ")), + } +} diff --git a/src/routes/blog.rs b/src/routes/blog.rs index a327635..2bae2ec 100644 --- a/src/routes/blog.rs +++ b/src/routes/blog.rs @@ -2,10 +2,7 @@ use actix_web::{get, web, HttpResponse, Responder}; use cached::proc_macro::once; use ramhorns::Content; -use crate::{ - config::Config, - template::{read_md_file, File, Infos}, -}; +use crate::{config::Config, misc::utils::get_post, template::File}; #[get("/blog")] pub async fn index(config: web::Data) -> impl Responder { @@ -28,15 +25,8 @@ pub async fn page(path: web::Path<(String,)>, config: web::Data) -> impl } pub fn get_page(file: String, config: Config) -> String { - let blog_dir = "data/blog"; - let ext = ".md"; - - let post = read_md_file(&format!("{blog_dir}/{file}{ext}")); - - let infos = Infos { - page_title: Some(format!("Post: {file}")), - page_desc: Some("Blog d'Anri".to_string()), - }; + let mut post = None; + let infos = get_post(&mut post, file); config .tmpl