34 lines
849 B
Rust
34 lines
849 B
Rust
|
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>,
|
||
|
}
|
||
|
|
||
|
pub fn get_tags<'a>(posts: impl IntoIterator<Item=&'a Post>) -> HashMap<String,Tag> {
|
||
|
let mut tags: HashMap<String,Tag> = HashMap::new();
|
||
|
|
||
|
for post in posts.into_iter().filter(|p| p.is_published()) {
|
||
|
for key in &post.tags {
|
||
|
if let Some(tag) = tags.get_mut(key) {
|
||
|
tag.posts.push(post.slug.clone());
|
||
|
}
|
||
|
else {
|
||
|
tags.insert(key.clone(), Tag {
|
||
|
slug: key.clone(),
|
||
|
absolute_path: format!("/tags/{key}/"),
|
||
|
posts: vec![post.slug.clone()]
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
tags
|
||
|
}
|