From d9122b105eef3173aa200175f193ce87c44df929 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konrad=20G=C4=85dek?= Date: Sat, 13 Apr 2019 18:47:17 +0200 Subject: [PATCH] Sort sitemap elements by permalink This feature was originally reported in #257 and got fixed in 3a2dab59743e8d193a80ddb1aa1932e1b5462322, however it seems this got lost during development. Closes #661 Ref #257 --- components/site/src/lib.rs | 14 +++++++++----- components/site/src/sitemap.rs | 13 +++++++++++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index 57ba91d1..7f0e326c 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -777,11 +777,15 @@ impl Site { ensure_directory_exists(&self.output_path)?; let library = self.library.read().unwrap(); - let all_sitemap_entries = sitemap::find_entries( - &library, - &self.taxonomies[..], - &self.config, - ); + let all_sitemap_entries = { + let mut all_sitemap_entries = sitemap::find_entries( + &library, + &self.taxonomies[..], + &self.config, + ); + all_sitemap_entries.sort(); + all_sitemap_entries + }; let sitemap_limit = 30000; if all_sitemap_entries.len() < sitemap_limit { diff --git a/components/site/src/sitemap.rs b/components/site/src/sitemap.rs index c38ed96a..83978bef 100644 --- a/components/site/src/sitemap.rs +++ b/components/site/src/sitemap.rs @@ -5,6 +5,7 @@ use std::collections::{HashSet}; use tera::{Map, Value}; use config::{Config}; use library::{Library, Taxonomy}; +use std::cmp::Ordering; /// The sitemap only needs links, potentially date and extra for pages in case of updates /// for examples so we trim down all entries to only that @@ -39,6 +40,18 @@ impl<'a> SitemapEntry<'a> { } } +impl<'a> PartialOrd for SitemapEntry<'a> { + fn partial_cmp(&self, other: &SitemapEntry) -> Option { + Some(self.permalink.as_ref().cmp(other.permalink.as_ref())) + } +} + +impl<'a> Ord for SitemapEntry<'a> { + fn cmp(&self, other: &SitemapEntry) -> Ordering { + self.permalink.as_ref().cmp(other.permalink.as_ref()) + } +} + /// Finds out all the links to put in a sitemap from the pages/sections/taxonomies /// There are no duplicate permalinks in the output vec pub fn find_entries<'a>(library: &'a Library, taxonomies: &'a [Taxonomy], config: &'a Config) -> Vec> {