Fix current_url in taxonomy term (#2000)
* Fix current_url in taxonomy term * add tests
This commit is contained in:
parent
442d6222a6
commit
a0f7dfed97
@ -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,
|
||||
@ -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);
|
||||
@ -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,
|
||||
@ -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()));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user