This commit is contained in:
parent
a310955757
commit
e42c5492e8
4 changed files with 52 additions and 22 deletions
|
@ -16,6 +16,7 @@ pub struct FileMetadata {
|
|||
pub description: Option<String>,
|
||||
pub publish: Option<bool>,
|
||||
pub tags: Option<Vec<Tag>>,
|
||||
pub toc: Option<bool>,
|
||||
}
|
||||
|
||||
#[derive(Content, Debug, Clone)]
|
||||
|
|
|
@ -173,6 +173,7 @@ fn get_posts(location: &str) -> Vec<Post> {
|
|||
#[derive(Content, Debug)]
|
||||
struct BlogPostTemplate {
|
||||
post: Option<File>,
|
||||
toc: String,
|
||||
}
|
||||
|
||||
#[get("/blog/p/{id}")]
|
||||
|
@ -190,34 +191,45 @@ 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<File>, filename: String, name: String, url: String) -> Infos {
|
||||
fn get_post(
|
||||
post: &mut Option<File>,
|
||||
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}")),
|
||||
|
@ -230,7 +242,9 @@ fn get_post(post: &mut Option<File>, filename: String, name: String, url: String
|
|||
.join(", "),
|
||||
),
|
||||
url,
|
||||
}
|
||||
},
|
||||
toc,
|
||||
)
|
||||
}
|
||||
|
||||
#[get("/blog/rss")]
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
<main>
|
||||
{{^post}}
|
||||
<p>This post doesn't exist... sorry</p>
|
||||
{{/post}} {{#post}}
|
||||
{{/post}} {{#post}} {{&toc}}
|
||||
<article>{{&content}}</article>
|
||||
{{/post}}
|
||||
</main>
|
||||
|
|
Loading…
Reference in a new issue