Skip to content

Commit

Permalink
Fix current_url in taxonomy term (#2000)
Browse files Browse the repository at this point in the history
* Fix current_url in taxonomy term

* add tests
  • Loading branch information
iEverX authored and Keats committed Feb 16, 2023
1 parent 442d622 commit a0f7dfe
Showing 1 changed file with 49 additions and 9 deletions.
58 changes: 49 additions & 9 deletions components/content/src/taxonomies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,11 @@ pub struct SerializedTaxonomy<'a> {

impl<'a> SerializedTaxonomy<'a> {
pub fn from_taxonomy(taxonomy: &'a Taxonomy, library: &'a Library) -> Self {
let items: Vec<SerializedTaxonomyTerm> =
taxonomy.items.iter().map(|i| SerializedTaxonomyTerm::from_item(i, library, true)).collect();
let items: Vec<SerializedTaxonomyTerm> = taxonomy
.items
.iter()
.map(|i| SerializedTaxonomyTerm::from_item(i, library, true))
.collect();
SerializedTaxonomy {
kind: &taxonomy.kind,
lang: &taxonomy.lang,
Expand Down Expand Up @@ -184,13 +187,7 @@ impl Taxonomy {
config: &Config,
library: &Library,
) -> Result<String> {
let mut context = Context::new();
context.insert("config", &config.serialize(&self.lang));
context.insert("lang", &self.lang);
context.insert("term", &SerializedTaxonomyTerm::from_item(item, library, true));
context.insert("taxonomy", &self.kind);
context.insert("current_url", &self.permalink);
context.insert("current_path", &self.path);
let context = self.build_term_context(item, config, library);

// Check for taxon-specific template, or use generic as fallback.
let specific_template = format!("{}/single.html", self.kind.name);
Expand All @@ -201,6 +198,22 @@ impl Taxonomy {
.with_context(|| format!("Failed to render single term {} page.", self.kind.name))
}

fn build_term_context(
&self,
item: &TaxonomyTerm,
config: &Config,
library: &Library,
) -> Context {
let mut context = Context::new();
context.insert("config", &config.serialize(&self.lang));
context.insert("lang", &self.lang);
context.insert("term", &SerializedTaxonomyTerm::from_item(item, library, true));
context.insert("taxonomy", &self.kind);
context.insert("current_url", &item.permalink);
context.insert("current_path", &item.path);
context
}

pub fn render_all_terms(
&self,
tera: &Tera,
Expand Down Expand Up @@ -253,3 +266,30 @@ impl<'a> TaxonomyFound<'a> {
Self { slug, lang, config, terms: AHashMap::new() }
}
}

#[cfg(test)]
mod tests {
use config::{Config, TaxonomyConfig};

use crate::{Library, Taxonomy, TaxonomyTerm};

use super::TaxonomyFound;

#[test]
fn can_build_term_context() {
let conf = Config::default_for_test();
let tax_conf = TaxonomyConfig::default();
let tax_found = TaxonomyFound::new("tag".into(), &conf.default_language, &tax_conf);
let tax = Taxonomy::new(tax_found, &conf);
let pages = &[];
let term = TaxonomyTerm::new("rust", &conf.default_language, "tags", pages, &conf);
let lib = Library::default();

let ctx = tax.build_term_context(&term, &conf, &lib);

assert_eq!(ctx.get("current_path").and_then(|x| x.as_str()), Some("/tags/rust/"));

let path = format!("{}{}", conf.base_url, "/tags/rust/");
assert_eq!(ctx.get("current_url").and_then(|x| x.as_str()), Some(path.as_str()));
}
}

0 comments on commit a0f7dfe

Please sign in to comment.