diff --git a/src/misc/markdown.rs b/src/misc/markdown.rs index 60ae341..aa670f1 100644 --- a/src/misc/markdown.rs +++ b/src/misc/markdown.rs @@ -16,6 +16,7 @@ pub struct FileMetadata { pub description: Option, pub publish: Option, pub tags: Option>, + pub toc: Option, } #[derive(Content, Debug, Clone)] diff --git a/src/routes/blog.rs b/src/routes/blog.rs index 91a06fd..053989b 100644 --- a/src/routes/blog.rs +++ b/src/routes/blog.rs @@ -173,6 +173,7 @@ fn get_posts(location: &str) -> Vec { #[derive(Content, Debug)] struct BlogPostTemplate { post: Option, + toc: String, } #[get("/blog/p/{id}")] @@ -190,47 +191,60 @@ pub async fn page( fn build_post(file: String, config: Config, url: String) -> String { let mut post = None; - let infos = get_post(&mut post, file, config.fc.name.unwrap_or_default(), url); + let (infos, toc) = get_post(&mut post, file, config.fc.name.unwrap_or_default(), url); config .tmpl - .render("blog/post.html", BlogPostTemplate { post }, infos) + .render("blog/post.html", BlogPostTemplate { post, toc }, infos) } -fn get_post(post: &mut Option, filename: String, name: String, url: String) -> Infos { +fn get_post( + post: &mut Option, + filename: String, + name: String, + url: String, +) -> (Infos, String) { let blog_dir = "data/blog"; let ext = ".md"; *post = read_file(&format!("{blog_dir}/{filename}{ext}")); - let (title, tags) = match post { + let default = (&filename, Vec::new(), String::new()); + let (title, tags, toc) = match post { Some(data) => ( match &data.metadata.info.title { Some(text) => text, - None => &filename, + None => default.0, }, match &data.metadata.info.tags { Some(tags) => tags.clone(), - None => Vec::new(), + None => default.1, + }, + match &data.metadata.info.toc { + // TODO: Generate TOC + Some(true) => String::new(), + _ => default.2, }, ), - - None => (&filename, Vec::new()), + None => default, }; - Infos { - page_title: Some(format!("Post: {}", title)), - page_desc: Some(format!("Blog d'{name}")), - page_kw: Some( - vec!["blog", "blogging", "write", "writing"] - .iter() - .map(|&tag| tag.to_owned()) - .chain(tags.into_iter().map(|t| t.name)) - .collect::>() - .join(", "), - ), - url, - } + ( + Infos { + page_title: Some(format!("Post: {}", title)), + page_desc: Some(format!("Blog d'{name}")), + page_kw: Some( + vec!["blog", "blogging", "write", "writing"] + .iter() + .map(|&tag| tag.to_owned()) + .chain(tags.into_iter().map(|t| t.name)) + .collect::>() + .join(", "), + ), + url, + }, + toc, + ) } #[get("/blog/rss")] diff --git a/static/css/blog/post.css b/static/css/blog/post.css index e17cfd4..76eada7 100644 --- a/static/css/blog/post.css +++ b/static/css/blog/post.css @@ -261,3 +261,18 @@ pre:has(code.language-mermaid) { margin: 20px; } } + +/* Table of content */ +nav#toc { + position: fixed; + top: 0; + left: 25px; + margin-left: 50px; +} + +@media only screen and (max-width: 1500px) { + /* Visible only on large screens */ + nav#toc { + visibility: hidden; + } +} diff --git a/templates/blog/post.html b/templates/blog/post.html index ba653ee..fb4723d 100644 --- a/templates/blog/post.html +++ b/templates/blog/post.html @@ -22,7 +22,7 @@
{{^post}}

This post doesn't exist... sorry

- {{/post}} {{#post}} + {{/post}} {{#post}} {{&toc}}
{{&content}}
{{/post}}