From 2ac1d79f8fed04e2b2b48c606fea76efa306a44a Mon Sep 17 00:00:00 2001 From: Sam Vente Date: Wed, 2 Sep 2020 11:40:06 +0200 Subject: [PATCH] fix `get_taxonomy_url` to handle merged taxonomies properly (#1155) --- components/site/src/tpls.rs | 6 +++++- components/templates/src/global_fns/mod.rs | 25 +++++++++++++++++----- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/components/site/src/tpls.rs b/components/site/src/tpls.rs index 5d3a9027..6a2c9a11 100644 --- a/components/site/src/tpls.rs +++ b/components/site/src/tpls.rs @@ -68,7 +68,11 @@ pub fn register_early_global_fns(site: &mut Site) { site.tera.register_function("trans", global_fns::Trans::new(site.config.clone())); site.tera.register_function( "get_taxonomy_url", - global_fns::GetTaxonomyUrl::new(&site.config.default_language, &site.taxonomies), + global_fns::GetTaxonomyUrl::new( + &site.config.default_language, + &site.taxonomies, + site.config.slugify.taxonomies, + ), ); site.tera.register_function( "get_file_hash", diff --git a/components/templates/src/global_fns/mod.rs b/components/templates/src/global_fns/mod.rs index 15df1ed0..0469612a 100644 --- a/components/templates/src/global_fns/mod.rs +++ b/components/templates/src/global_fns/mod.rs @@ -12,6 +12,7 @@ use config::Config; use image::GenericImageView; use library::{Library, Taxonomy}; use utils::site::resolve_internal_link; +use utils::slugs::{slugify_paths, SlugifyStrategy}; #[macro_use] mod macros; @@ -317,18 +318,20 @@ fn image_dimensions(path: &PathBuf) -> Result<(u32, u32)> { pub struct GetTaxonomyUrl { taxonomies: HashMap>, default_lang: String, + slugify: SlugifyStrategy, } + impl GetTaxonomyUrl { - pub fn new(default_lang: &str, all_taxonomies: &[Taxonomy]) -> Self { + pub fn new(default_lang: &str, all_taxonomies: &[Taxonomy], slugify: SlugifyStrategy) -> Self { let mut taxonomies = HashMap::new(); for taxo in all_taxonomies { let mut items = HashMap::new(); for item in &taxo.items { - items.insert(item.name.clone(), item.permalink.clone()); + items.insert(slugify_paths(&item.name.clone(), slugify), item.permalink.clone()); } taxonomies.insert(format!("{}-{}", taxo.kind.name, taxo.kind.lang), items); } - Self { taxonomies, default_lang: default_lang.to_string() } + Self { taxonomies, default_lang: default_lang.to_string(), slugify: slugify } } } impl TeraFn for GetTaxonomyUrl { @@ -358,7 +361,7 @@ impl TeraFn for GetTaxonomyUrl { } }; - if let Some(permalink) = container.get(&name) { + if let Some(permalink) = container.get(&slugify_paths(&name, self.slugify)) { return Ok(to_value(permalink).unwrap()); } @@ -650,7 +653,9 @@ mod tests { let tags_fr = Taxonomy { kind: taxo_config_fr, items: vec![tag_fr] }; let taxonomies = vec![tags.clone(), tags_fr.clone()]; - let static_fn = GetTaxonomyUrl::new(&config.default_language, &taxonomies); + let static_fn = + GetTaxonomyUrl::new(&config.default_language, &taxonomies, config.slugify.taxonomies); + // can find it correctly let mut args = HashMap::new(); args.insert("kind".to_string(), to_value("tags").unwrap()); @@ -659,6 +664,16 @@ mod tests { static_fn.call(&args).unwrap(), to_value("http://a-website.com/tags/programming/").unwrap() ); + + // can find it correctly with inconsistent capitalisation + let mut args = HashMap::new(); + args.insert("kind".to_string(), to_value("tags").unwrap()); + args.insert("name".to_string(), to_value("programming").unwrap()); + assert_eq!( + static_fn.call(&args).unwrap(), + to_value("http://a-website.com/tags/programming/").unwrap() + ); + // works with other languages let mut args = HashMap::new(); args.insert("kind".to_string(), to_value("tags").unwrap());