From 57705aa82eaf10df72ad9495dcb56f3e6fbb8890 Mon Sep 17 00:00:00 2001 From: Bert JW Regeer Date: Fri, 9 Jul 2021 23:43:22 -0700 Subject: [PATCH] Add permalink to Taxonomy (#1516) * Add permalink to Taxonomy * Document permalink addition to TaxonomyConfig --- components/library/src/pagination/mod.rs | 2 ++ components/library/src/taxonomies/mod.rs | 18 ++++++++++++++++-- components/templates/src/global_fns/content.rs | 4 ++++ .../documentation/templates/taxonomies.md | 1 + 4 files changed, 23 insertions(+), 2 deletions(-) diff --git a/components/library/src/pagination/mod.rs b/components/library/src/pagination/mod.rs index e1bf525d..16f017a2 100644 --- a/components/library/src/pagination/mod.rs +++ b/components/library/src/pagination/mod.rs @@ -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); diff --git a/components/library/src/taxonomies/mod.rs b/components/library/src/taxonomies/mod.rs index 3f34e934..c919bc3d 100644 --- a/components/library/src/taxonomies/mod.rs +++ b/components/library/src/taxonomies/mod.rs @@ -108,6 +108,7 @@ impl PartialEq for TaxonomyItem { pub struct SerializedTaxonomy<'a> { kind: &'a TaxonomyConfig, lang: &'a str, + permalink: &'a str, items: Vec>, } @@ -115,7 +116,12 @@ impl<'a> SerializedTaxonomy<'a> { pub fn from_taxonomy(taxonomy: &'a Taxonomy, library: &'a Library) -> Self { let items: Vec = 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, } @@ -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 { diff --git a/components/templates/src/global_fns/content.rs b/components/templates/src/global_fns/content.rs index 688465a0..5ae1f3ca 100644 --- a/components/templates/src/global_fns/content.rs +++ b/components/templates/src/global_fns/content.rs @@ -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], }; diff --git a/docs/content/documentation/templates/taxonomies.md b/docs/content/documentation/templates/taxonomies.md index e8e41536..c8302877 100644 --- a/docs/content/documentation/templates/taxonomies.md +++ b/docs/content/documentation/templates/taxonomies.md @@ -26,6 +26,7 @@ paginate_by: Number?; paginate_path: String?; feed: Bool; lang: String; +permalink: String; ```