Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix current_url in taxonomy term #2000

Merged
merged 2 commits into from
Oct 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()));
}
}