From 7dcff0b8103b3b9039120ee81d61616278486763 Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 2 May 2023 14:37:32 +0200 Subject: [PATCH] WIP: tags support --- src/misc/markdown.rs | 1 + src/routes/blog.rs | 34 +++++++++++++++++++++++++++------- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/misc/markdown.rs b/src/misc/markdown.rs index f96dea7..5ca6d3d 100644 --- a/src/misc/markdown.rs +++ b/src/misc/markdown.rs @@ -15,6 +15,7 @@ pub struct FileMetadata { pub date: Option, pub description: Option, pub publish: Option, + pub tags: Option>, } #[derive(Content, Debug)] diff --git a/src/routes/blog.rs b/src/routes/blog.rs index e40a4d1..6bc99b0 100644 --- a/src/routes/blog.rs +++ b/src/routes/blog.rs @@ -73,6 +73,7 @@ struct Post { url: String, desc: Option, content: Option, + tags: Option>, } impl Post { @@ -122,11 +123,15 @@ fn get_posts(location: &str) -> Vec { let root = parse_document(&arena, &text, &options); let mut metadata = get_metadata(root); + // Always have a title metadata.title = match metadata.title { Some(title) => Some(title), None => Some(file_without_ext.into()), }; + // No tags if the vec is empty + metadata.tags = metadata.tags.filter(|tags| !tags.is_empty()); + metadata } Err(_) => FileMetadata { @@ -154,6 +159,7 @@ fn get_posts(location: &str) -> Vec { }), desc: file_metadata.description, content: None, + tags: file_metadata.tags, }) } else { None @@ -195,18 +201,32 @@ fn get_post(post: &mut Option, filename: String, name: String, url: String *post = read_file(&format!("{blog_dir}/{filename}{ext}")); - let title = match post { - Some(data) => match &data.metadata.info.title { - Some(text) => text, - None => &filename, - }, - None => &filename, + let (title, tags) = match post { + Some(data) => ( + match &data.metadata.info.title { + Some(text) => text, + None => &filename, + }, + match &data.metadata.info.tags { + Some(tags) => tags.clone(), + None => Vec::new(), + }, + ), + + None => (&filename, Vec::new()), }; Infos { page_title: Some(format!("Post: {}", title)), page_desc: Some(format!("Blog d'{name}")), - page_kw: Some(["blog", "blogging", "write", "writing"].join(", ")), + page_kw: Some( + vec!["blog", "blogging", "write", "writing"] + .iter() + .map(|&tag| tag.to_owned()) + .chain(tags.into_iter()) + .collect::>() + .join(", "), + ), url, } }