Add permalink to Taxonomy (#1516)

* Add permalink to Taxonomy

* Document permalink addition to TaxonomyConfig
This commit is contained in:
Bert JW Regeer 2021-07-09 23:43:22 -07:00 committed by GitHub
parent 98b110ba14
commit 57705aa82e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 2 deletions

View file

@ -422,6 +422,7 @@ mod tests {
kind: taxonomy_def,
lang: "en".to_owned(),
slug: "tags".to_string(),
permalink: "/tags/".to_string(),
items: vec![taxonomy_item.clone()],
};
let paginator = Paginator::from_taxonomy(&taxonomy, &taxonomy_item, &library);
@ -457,6 +458,7 @@ mod tests {
kind: taxonomy_def,
lang: "en".to_owned(),
slug: "some-tags".to_string(),
permalink: "/some-tags/".to_string(),
items: vec![taxonomy_item.clone()],
};
let paginator = Paginator::from_taxonomy(&taxonomy, &taxonomy_item, &library);

View file

@ -108,6 +108,7 @@ impl PartialEq for TaxonomyItem {
pub struct SerializedTaxonomy<'a> {
kind: &'a TaxonomyConfig,
lang: &'a str,
permalink: &'a str,
items: Vec<SerializedTaxonomyItem<'a>>,
}
@ -115,7 +116,12 @@ impl<'a> SerializedTaxonomy<'a> {
pub fn from_taxonomy(taxonomy: &'a Taxonomy, library: &'a Library) -> Self {
let items: Vec<SerializedTaxonomyItem> =
taxonomy.items.iter().map(|i| SerializedTaxonomyItem::from_item(i, library)).collect();
SerializedTaxonomy { kind: &taxonomy.kind, lang: &taxonomy.lang, items }
SerializedTaxonomy {
kind: &taxonomy.kind,
lang: &taxonomy.lang,
permalink: &taxonomy.permalink,
items,
}
}
}
@ -125,6 +131,7 @@ pub struct Taxonomy {
pub kind: TaxonomyConfig,
pub lang: String,
pub slug: String,
pub permalink: String,
// this vec is sorted by the count of item
pub items: Vec<TaxonomyItem>,
}
@ -159,7 +166,14 @@ impl Taxonomy {
false
}
});
Taxonomy { kind, slug, lang: lang.to_owned(), items: sorted_items }
let path = if lang != config.default_language {
format!("/{}/{}/", lang, slug)
} else {
format!("/{}/", slug)
};
let permalink = config.make_permalink(&path);
Taxonomy { kind, slug, lang: lang.to_owned(), permalink, items: sorted_items }
}
pub fn len(&self) -> usize {

View file

@ -202,12 +202,14 @@ mod tests {
kind: taxo_config,
lang: config.default_language.clone(),
slug: "tags".to_string(),
permalink: "/tags/".to_string(),
items: vec![tag],
};
let tags_fr = Taxonomy {
kind: taxo_config_fr,
lang: "fr".to_owned(),
slug: "tags".to_string(),
permalink: "/fr/tags/".to_string(),
items: vec![tag_fr],
};
@ -278,12 +280,14 @@ mod tests {
kind: taxo_config,
lang: config.default_language.clone(),
slug: "tags".to_string(),
permalink: "/tags/".to_string(),
items: vec![tag],
};
let tags_fr = Taxonomy {
kind: taxo_config_fr,
lang: "fr".to_owned(),
slug: "tags".to_string(),
permalink: "/fr/tags/".to_string(),
items: vec![tag_fr],
};

View file

@ -26,6 +26,7 @@ paginate_by: Number?;
paginate_path: String?;
feed: Bool;
lang: String;
permalink: String;
```