From 6e16dfdc29cdf69eecea7f3902f6e56e8431dca7 Mon Sep 17 00:00:00 2001 From: Sam Vente Date: Wed, 26 Aug 2020 19:36:02 +0200 Subject: [PATCH] A Fix for the permalinks in #1136 (#1142) * add fix for (#1135) Taxonomies with identical slugs now get merged (#1136) * update templates so they propperly render taxonomy names * squash! add fix for (#1135) Taxonomies with identical slugs now get merged (#1136) reimplement taxonomy deduping * revert unwanted changes to templates * add tests for unic in permalinks * add tests for unic in permalinks --- components/config/src/lib.rs | 2 +- components/library/src/taxonomies/mod.rs | 252 ++++++++++++++++-- .../documentation/templates/overview.md | 4 +- test_site_i18n/templates/auteurs/list.html | 2 +- 4 files changed, 228 insertions(+), 32 deletions(-) diff --git a/components/config/src/lib.rs b/components/config/src/lib.rs index ada1bf1c..529a8b4a 100644 --- a/components/config/src/lib.rs +++ b/components/config/src/lib.rs @@ -2,7 +2,7 @@ mod config; pub mod highlighting; mod theme; pub use crate::config::{ - languages::Language, link_checker::LinkChecker, taxonomies::Taxonomy, Config, + languages::Language, link_checker::LinkChecker, slugify::Slugify, taxonomies::Taxonomy, Config, }; use std::path::Path; diff --git a/components/library/src/taxonomies/mod.rs b/components/library/src/taxonomies/mod.rs index a2803b79..62062c30 100644 --- a/components/library/src/taxonomies/mod.rs +++ b/components/library/src/taxonomies/mod.rs @@ -1,3 +1,4 @@ +use std::cmp::Ordering; use std::collections::HashMap; use serde_derive::Serialize; @@ -40,7 +41,7 @@ impl<'a> SerializedTaxonomyItem<'a> { } /// A taxonomy with all its pages -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone)] pub struct TaxonomyItem { pub name: String, pub slug: String, @@ -70,22 +71,33 @@ impl TaxonomyItem { }) .collect(); let (mut pages, ignored_pages) = sort_pages_by_date(data); - let slug = slugify_paths(name, config.slugify.taxonomies); + let item_slug = slugify_paths(name, config.slugify.taxonomies); + let taxo_slug = slugify_paths(&taxonomy.name, config.slugify.taxonomies); let permalink = if taxonomy.lang != config.default_language { - config.make_permalink(&format!("/{}/{}/{}", taxonomy.lang, taxonomy.name, slug)) + config.make_permalink(&format!("/{}/{}/{}", taxonomy.lang, taxo_slug, item_slug)) } else { - config.make_permalink(&format!("/{}/{}", taxonomy.name, slug)) + config.make_permalink(&format!("/{}/{}", taxo_slug, item_slug)) }; // We still append pages without dates at the end pages.extend(ignored_pages); - TaxonomyItem { name: name.to_string(), permalink, slug, pages } + TaxonomyItem { name: name.to_string(), permalink, slug: item_slug, pages } } pub fn serialize<'a>(&'a self, library: &'a Library) -> SerializedTaxonomyItem<'a> { SerializedTaxonomyItem::from_item(self, library) } + + pub fn merge(&mut self, other: Self) { + self.pages.extend(other.pages); + } +} + +impl PartialEq for TaxonomyItem { + fn eq(&self, other: &Self) -> bool { + self.permalink == other.permalink + } } #[derive(Debug, Clone, PartialEq, Serialize)] @@ -121,8 +133,23 @@ impl Taxonomy { for (name, pages) in items { sorted_items.push(TaxonomyItem::new(&name, &kind, config, pages, library)); } - sorted_items.sort_by(|a, b| a.name.cmp(&b.name)); - + //sorted_items.sort_by(|a, b| a.name.cmp(&b.name)); + sorted_items.sort_by(|a, b| match a.slug.cmp(&b.slug) { + Ordering::Less => Ordering::Less, + Ordering::Greater => Ordering::Greater, + Ordering::Equal => a.name.cmp(&b.name), + }); + sorted_items.dedup_by(|a, b| { + // custom Eq impl checks for equal permalinks + // here we make sure all pages from a get coppied to b + // before dedup gets rid of it + if a == b { + b.merge(a.to_owned()); + true + } else { + false + } + }); Taxonomy { kind, items: sorted_items } } @@ -189,27 +216,25 @@ pub fn find_taxonomies(config: &Config, library: &Library) -> Result