From 0cf8e8ca1c0f23a6907d854decf04150bc8f2ec3 Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Thu, 29 Nov 2018 21:48:02 +0100 Subject: [PATCH] Fix pagers not being in sitemap Closes #521 --- CHANGELOG.md | 1 + components/config/src/config.rs | 8 ++++++ components/library/src/pagination/mod.rs | 6 ++--- components/site/src/lib.rs | 16 ++++++++++++ components/site/tests/site.rs | 33 +++++++++++++++++++----- 5 files changed, 55 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5fe0d727..2f827df4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Fix deleting markdown file in `zola serve` - Fix pagination for taxonomies being broken and add missing documentation for it +- Add missing pager pages from the sitemap ## 0.5.0 (2018-11-17) diff --git a/components/config/src/config.rs b/components/config/src/config.rs index 7687c360..c80c34b7 100644 --- a/components/config/src/config.rs +++ b/components/config/src/config.rs @@ -37,6 +37,14 @@ impl Taxonomy { false } } + + pub fn paginate_path(&self) -> &str { + if let Some(ref path) = self.paginate_path { + path + } else { + "page" + } + } } impl Default for Taxonomy { diff --git a/components/library/src/pagination/mod.rs b/components/library/src/pagination/mod.rs index b1648667..9566189b 100644 --- a/components/library/src/pagination/mod.rs +++ b/components/library/src/pagination/mod.rs @@ -100,7 +100,7 @@ impl<'a> Paginator<'a> { .kind .paginate_path .clone() - .unwrap_or_else(|| "pages".to_string()), + .unwrap_or_else(|| "page".to_string()), is_index: false, template: format!("{}/single.html", taxonomy.kind.name), }; @@ -350,7 +350,7 @@ mod tests { assert_eq!(paginator.pagers[1].index, 2); assert_eq!(paginator.pagers[1].pages.len(), 1); - assert_eq!(paginator.pagers[1].permalink, "https://vincent.is/tags/something/pages/2/"); - assert_eq!(paginator.pagers[1].path, "tags/something/pages/2/"); + assert_eq!(paginator.pagers[1].permalink, "https://vincent.is/tags/something/page/2/"); + assert_eq!(paginator.pagers[1].path, "tags/something/page/2/"); } } diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index 1b37a3e1..76240229 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -721,6 +721,13 @@ impl Site { .iter() .map(|s| SitemapEntry::new(s.permalink.clone(), None)) .collect::>(); + for section in self.library.sections_values().iter().filter(|s| s.meta.paginate_by.is_some()) { + let number_pagers = (section.pages.len() as f64 / section.meta.paginate_by.unwrap() as f64).ceil() as isize; + for i in 1..number_pagers+1 { + let permalink = format!("{}{}/{}/", section.permalink, section.meta.paginate_path, i); + sections.push(SitemapEntry::new(permalink, None)) + } + } sections.sort_by(|a, b| a.permalink.cmp(&b.permalink)); context.insert("sections", §ions); @@ -734,7 +741,16 @@ impl Site { self.config.make_permalink(&format!("{}/{}", &name, item.slug)), None, )); + + if taxonomy.kind.is_paginated() { + let number_pagers = (item.pages.len() as f64 / taxonomy.kind.paginate_by.unwrap() as f64).ceil() as isize; + for i in 1..number_pagers+1 { + let permalink = self.config.make_permalink(&format!("{}/{}/{}/{}", name, item.slug, taxonomy.kind.paginate_path(), i)); + terms.push(SitemapEntry::new(permalink, None)) + } + } } + terms.sort_by(|a, b| a.permalink.cmp(&b.permalink)); taxonomies.push(terms); } diff --git a/components/site/tests/site.rs b/components/site/tests/site.rs index 4031444c..9d813ddd 100644 --- a/components/site/tests/site.rs +++ b/components/site/tests/site.rs @@ -467,6 +467,13 @@ fn can_build_site_with_pagination_for_section() { "posts/page/4/index.html", "Last: https://replace-this-with-your-url.com/posts/page/5/" )); + + // sitemap contains the pager pages + assert!(file_contains!( + public, + "sitemap.xml", + "https://replace-this-with-your-url.com/posts/page/4/" + )); } #[test] @@ -512,6 +519,13 @@ fn can_build_site_with_pagination_for_index() { assert!(file_contains!(public, "index.html", "Last: https://replace-this-with-your-url.com/")); assert_eq!(file_contains!(public, "index.html", "has_prev"), false); assert_eq!(file_contains!(public, "index.html", "has_next"), false); + + // sitemap contains the pager pages + assert!(file_contains!( + public, + "sitemap.xml", + "https://replace-this-with-your-url.com/page/1/" + )) } #[test] @@ -558,15 +572,15 @@ fn can_build_site_with_pagination_for_taxonomy() { assert!(file_exists!(public, "tags/a/rss.xml")); assert!(file_exists!(public, "tags/b/rss.xml")); // And pagination! - assert!(file_exists!(public, "tags/a/pages/1/index.html")); - assert!(file_exists!(public, "tags/b/pages/1/index.html")); - assert!(file_exists!(public, "tags/a/pages/2/index.html")); - assert!(file_exists!(public, "tags/b/pages/2/index.html")); + assert!(file_exists!(public, "tags/a/page/1/index.html")); + assert!(file_exists!(public, "tags/b/page/1/index.html")); + assert!(file_exists!(public, "tags/a/page/2/index.html")); + assert!(file_exists!(public, "tags/b/page/2/index.html")); // should redirect to posts/ assert!(file_contains!( public, - "tags/a/pages/1/index.html", + "tags/a/page/1/index.html", "http-equiv=\"refresh\" content=\"0;url=https://replace-this-with-your-url.com/tags/a/\"" )); assert!(file_contains!(public, "tags/a/index.html", "Num pagers: 6")); @@ -582,9 +596,16 @@ fn can_build_site_with_pagination_for_taxonomy() { assert!(file_contains!( public, "tags/a/index.html", - "Last: https://replace-this-with-your-url.com/tags/a/pages/6/" + "Last: https://replace-this-with-your-url.com/tags/a/page/6/" )); assert_eq!(file_contains!(public, "tags/a/index.html", "has_prev"), false); + + // sitemap contains the pager pages + assert!(file_contains!( + public, + "sitemap.xml", + "https://replace-this-with-your-url.com/tags/a/page/6/" + )) } #[test]