From 3a2dab59743e8d193a80ddb1aa1932e1b5462322 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Sat, 17 Mar 2018 14:34:01 +0100 Subject: [PATCH] Sort sitemap elements by permalink Closes #257 --- CHANGELOG.md | 1 + components/site/src/lib.rs | 39 ++++++++++--------- .../documentation/templates/sitemap.md | 2 + 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7cd9714..b85c41b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## 0.3.3 (unreleased) - Fixed config flag in CLI +- Sitemap entries are now sorted by permalinks to avoid random ordering ## 0.3.2 (2018-03-05) diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index 661adb8..109f7d2 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -632,27 +632,26 @@ impl Site { let mut context = Context::new(); - context.add( - "pages", - &self.pages - .values() - .filter(|p| !p.is_draft()) - .map(|p| { - let date = match p.meta.date { - Some(ref d) => Some(d.to_string()), - None => None, - }; - SitemapEntry::new(p.permalink.clone(), date) - }) - .collect::>() - ); - context.add( - "sections", - &self.sections + let mut pages = self.pages + .values() + .filter(|p| !p.is_draft()) + .map(|p| { + let date = match p.meta.date { + Some(ref d) => Some(d.to_string()), + None => None, + }; + SitemapEntry::new(p.permalink.clone(), date) + }) + .collect::>(); + pages.sort_by(|a, b| a.permalink.cmp(&b.permalink)); + context.add("pages", &pages); + + let mut sections = self.sections .values() .map(|s| SitemapEntry::new(s.permalink.clone(), None)) - .collect::>() - ); + .collect::>(); + sections.sort_by(|a, b| a.permalink.cmp(&b.permalink)); + context.add("sections", §ions); let mut categories = vec![]; if let Some(ref c) = self.categories { @@ -664,6 +663,7 @@ impl Site { ); } } + categories.sort_by(|a, b| a.permalink.cmp(&b.permalink)); context.add("categories", &categories); let mut tags = vec![]; @@ -676,6 +676,7 @@ impl Site { ); } } + tags.sort_by(|a, b| a.permalink.cmp(&b.permalink)); context.add("tags", &tags); context.add("config", &self.config); diff --git a/docs/content/documentation/templates/sitemap.md b/docs/content/documentation/templates/sitemap.md index a63f3f6..641c1ff 100644 --- a/docs/content/documentation/templates/sitemap.md +++ b/docs/content/documentation/templates/sitemap.md @@ -21,3 +21,5 @@ all the variables above are arrays of `SitemapEntry` with the following type: permalink: String; date: String?; ``` + +All `SitemapEntry` are sorted in each variable by their permalink.