Skip to content

Commit

Permalink
Fix taxonomy current path (#1882)
Browse files Browse the repository at this point in the history
* Fix current_path and current_url for taxonomies

* Fix language duplication in get_url fn
  • Loading branch information
bemyak authored Jun 9, 2022
1 parent 6989eb7 commit 8029cf8
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 13 deletions.
3 changes: 2 additions & 1 deletion components/content/src/pagination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,8 @@ mod tests {
kind: taxonomy_def,
lang: "en".to_owned(),
slug: "some-tags".to_string(),
permalink: "/some-tags/".to_string(),
path: "/some-tags/".to_string(),
permalink: "https://vincent.is/some-tags/".to_string(),
items: vec![taxonomy_item.clone()],
};
let paginator = Paginator::from_taxonomy(&taxonomy, &taxonomy_item, &library, &tera, &None);
Expand Down
13 changes: 6 additions & 7 deletions components/content/src/taxonomies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ pub struct Taxonomy {
pub kind: TaxonomyConfig,
pub lang: String,
pub slug: String,
pub path: String,
pub permalink: String,
// this vec is sorted by the count of item
pub items: Vec<TaxonomyTerm>,
Expand Down Expand Up @@ -159,6 +160,7 @@ impl Taxonomy {
slug,
lang: tax_found.lang.to_owned(),
kind: tax_found.config.clone(),
path,
permalink,
items: sorted_items,
}
Expand All @@ -176,11 +178,8 @@ impl Taxonomy {
context.insert("lang", &self.lang);
context.insert("term", &SerializedTaxonomyTerm::from_item(item, library));
context.insert("taxonomy", &self.kind);
context.insert(
"current_url",
&config.make_permalink(&format!("{}/{}", self.kind.name, item.slug)),
);
context.insert("current_path", &format!("/{}/{}/", self.kind.name, item.slug));
context.insert("current_url", &self.permalink);
context.insert("current_path", &self.path);

// Check for taxon-specific template, or use generic as fallback.
let specific_template = format!("{}/single.html", self.kind.name);
Expand All @@ -204,8 +203,8 @@ impl Taxonomy {
context.insert("terms", &terms);
context.insert("lang", &self.lang);
context.insert("taxonomy", &self.kind);
context.insert("current_url", &config.make_permalink(&self.kind.name));
context.insert("current_path", &format!("/{}/", self.kind.name));
context.insert("current_url", &self.permalink);
context.insert("current_path", &self.path);

// Check for taxon-specific template, or use generic as fallback.
let specific_template = format!("{}/list.html", self.kind.name);
Expand Down
12 changes: 8 additions & 4 deletions components/templates/src/global_fns/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,16 @@ mod tests {
kind: taxo_config,
lang: config.default_language.clone(),
slug: "tags".to_string(),
permalink: "/tags/".to_string(),
path: "/tags/".to_string(),
permalink: "https://vincent.is/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(),
path: "/fr/tags/".to_string(),
permalink: "https://vincent.is/fr/tags/".to_string(),
items: vec![tag_fr],
};

Expand Down Expand Up @@ -272,14 +274,16 @@ mod tests {
kind: taxo_config,
lang: config.default_language.clone(),
slug: "tags".to_string(),
permalink: "/tags/".to_string(),
path: "/tags/".to_string(),
permalink: "https://vincent.is/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(),
path: "/fr/tags/".to_string(),
permalink: "https://vincent.is/fr/tags/".to_string(),
items: vec![tag_fr],
};

Expand Down
25 changes: 24 additions & 1 deletion components/templates/src/global_fns/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ impl TeraFn for GetUrl {
// anything else
let mut segments = vec![];

if lang != self.config.default_language {
if lang != self.config.default_language && !path[1..].starts_with(&lang) {
segments.push(lang);
};

Expand Down Expand Up @@ -399,6 +399,29 @@ title = "A title"
);
}

#[test]
fn does_not_duplicate_lang() {
let config = Config::parse(CONFIG_DATA).unwrap();
let mut permalinks = HashMap::new();
permalinks.insert(
"a_section/a_page.md".to_string(),
"https://remplace-par-ton-url.fr/a_section/a_page/".to_string(),
);
permalinks.insert(
"a_section/a_page.en.md".to_string(),
"https://remplace-par-ton-url.fr/en/a_section/a_page/".to_string(),
);
let dir = create_temp_dir();
let static_fn = GetUrl::new(dir.path().to_path_buf(), config, permalinks, PathBuf::new());
let mut args = HashMap::new();
args.insert("path".to_string(), to_value("/en/a_section/a_page/").unwrap());
args.insert("lang".to_string(), to_value("en").unwrap());
assert_eq!(
static_fn.call(&args).unwrap(),
"https://remplace-par-ton-url.fr/en/a_section/a_page"
);
}

#[test]
fn can_get_feed_url_with_default_language() {
let config = Config::parse(CONFIG_DATA).unwrap();
Expand Down

0 comments on commit 8029cf8

Please sign in to comment.