2023-03-29 21:48:27 +02:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
use serde_derive::Serialize;
|
|
|
|
|
|
|
|
use crate::post::Post;
|
|
|
|
|
|
|
|
#[derive(Serialize, Debug)]
|
|
|
|
pub struct Tag {
|
|
|
|
pub slug: String,
|
|
|
|
pub absolute_path: String,
|
|
|
|
pub posts: Vec<String>,
|
|
|
|
}
|
|
|
|
|
2023-04-02 15:26:20 +02:00
|
|
|
pub fn get_tags<'a>(posts: impl IntoIterator<Item = &'a Post>) -> HashMap<String, Tag> {
|
|
|
|
let mut tags: HashMap<String, Tag> = HashMap::new();
|
2023-03-29 21:48:27 +02:00
|
|
|
|
2023-03-30 00:06:01 +02:00
|
|
|
for post in posts.into_iter() {
|
2023-03-29 21:48:27 +02:00
|
|
|
for key in &post.tags {
|
|
|
|
if let Some(tag) = tags.get_mut(key) {
|
|
|
|
tag.posts.push(post.slug.clone());
|
2023-04-02 15:26:20 +02:00
|
|
|
} else {
|
|
|
|
tags.insert(
|
|
|
|
key.clone(),
|
|
|
|
Tag {
|
|
|
|
slug: key.clone(),
|
|
|
|
absolute_path: format!("/tags/{key}/"),
|
|
|
|
posts: vec![post.slug.clone()],
|
|
|
|
},
|
|
|
|
);
|
2023-03-29 21:48:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tags
|
|
|
|
}
|