tags in post
Some checks are pending
ci/woodpecker/push/publish Pipeline is pending

This commit is contained in:
Mylloon 2023-05-02 16:02:50 +02:00
parent 3e9bbffd58
commit 67feca4fc2
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
4 changed files with 51 additions and 22 deletions

View file

@ -5,7 +5,7 @@ use comrak::{
ComrakRenderOptions, ListStyleType,
};
use ramhorns::Content;
use serde::Deserialize;
use serde::{Deserialize, Deserializer};
use std::fs;
#[derive(Default, Deserialize, Content, Debug)]
@ -15,7 +15,27 @@ pub struct FileMetadata {
pub date: Option<Date>,
pub description: Option<String>,
pub publish: Option<bool>,
pub tags: Option<Vec<String>>,
pub tags: Option<Vec<Tag>>,
}
#[derive(Content, Debug, Clone)]
pub struct Tag {
pub name: String,
}
impl<'de> Deserialize<'de> for Tag {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
match <&str>::deserialize(deserializer) {
Ok(s) => match serde_yaml::from_str(s) {
Ok(tags) => Ok(Self { name: tags }),
Err(e) => Err(serde::de::Error::custom(e)),
},
Err(e) => Err(e),
}
}
}
#[derive(Content, Debug)]

View file

@ -66,6 +66,11 @@ pub fn build_index(config: Config, url: String) -> String {
)
}
#[derive(Content, Debug)]
pub struct Tag {
pub name: String,
}
#[derive(Content, Debug)]
struct Post {
title: String,
@ -73,7 +78,7 @@ struct Post {
url: String,
desc: Option<String>,
content: Option<String>,
tags: Option<Vec<String>>,
tags: Vec<String>,
}
impl Post {
@ -129,9 +134,6 @@ fn get_posts(location: &str) -> Vec<Post> {
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 {
@ -159,7 +161,12 @@ fn get_posts(location: &str) -> Vec<Post> {
}),
desc: file_metadata.description,
content: None,
tags: file_metadata.tags,
tags: file_metadata
.tags
.unwrap_or_default()
.iter()
.map(|t| t.name.to_owned())
.collect(),
})
} else {
None
@ -223,7 +230,7 @@ fn get_post(post: &mut Option<File>, filename: String, name: String, url: String
vec!["blog", "blogging", "write", "writing"]
.iter()
.map(|&tag| tag.to_owned())
.chain(tags.into_iter())
.chain(tags.into_iter().map(|t| t.name))
.collect::<Vec<String>>()
.join(", "),
),
@ -297,16 +304,14 @@ fn build_rss(config: Config, info: ConnectionInfo) -> String {
title: Some(p.title.to_owned()),
link: Some(format!("{}/blog/p/{}", link_to_site, p.url)),
description: p.content.to_owned(),
categories: match &p.tags {
Some(tags) => tags
categories: p
.tags
.iter()
.map(|c| Category {
name: c.into(),
name: c.to_owned(),
..Category::default()
})
.collect(),
None => vec![],
},
guid: Some(Guid {
value: format!("urn:hash:{}", {
let mut hasher = DefaultHasher::new();

View file

@ -63,7 +63,7 @@ header > h1 {
}
/* Date's title */
header > p {
header > span {
font-style: italic;
}

View file

@ -11,9 +11,13 @@
<header>
{{#info}}
<h1>{{title}}</h1>
{{#date}}
<p>{{>blog/date.html}}</p>
{{/date}} {{/info}} {{/metadata}} {{/post}}
{{#date}} {{>blog/date.html}} {{/date}}
<ul>
{{#tags}}
<li>{{name}}</li>
{{/tags}}
</ul>
{{/info}} {{/metadata}} {{/post}}
</header>
<main>
{{^post}}