Add sections to sitemap

This commit is contained in:
Vincent Prouillet 2017-03-19 19:40:31 +09:00
parent cd70aac065
commit d8995c156c
3 changed files with 12 additions and 2 deletions

View file

@ -335,6 +335,8 @@ impl Site {
fn render_sitemap(&self) -> Result<()> { fn render_sitemap(&self) -> Result<()> {
let mut context = Context::new(); let mut context = Context::new();
context.add("pages", &self.pages.values().collect::<Vec<&Page>>()); context.add("pages", &self.pages.values().collect::<Vec<&Page>>());
context.add("sections", &self.sections.values().collect::<Vec<&Section>>());
// TODO: add categories and tags pages
let sitemap = self.templates.render("sitemap.xml", &context)?; let sitemap = self.templates.render("sitemap.xml", &context)?;
create_file(self.output_path.join("sitemap.xml"), &sitemap)?; create_file(self.output_path.join("sitemap.xml"), &sitemap)?;

View file

@ -1,10 +1,15 @@
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{% for page in pages %} {% for page in pages %}
<url> <url>
<loc>{{ page.permalink }}</loc> <loc>{{ page.permalink | safe }}</loc>
{% if page.date %} {% if page.date %}
<lastmod>{{ page.date }}</lastmod> <lastmod>{{ page.date }}</lastmod>
{% endif %} {% endif %}
</url> </url>
{% endfor %} {% endfor %}
{% for section in sections %}
<url>
<loc>{{ section.permalink | safe }}</loc>
</url>
{% endfor %}
</urlset> </urlset>

View file

@ -78,7 +78,6 @@ macro_rules! file_contains {
let mut file = File::open(&path).unwrap(); let mut file = File::open(&path).unwrap();
let mut s = String::new(); let mut s = String::new();
file.read_to_string(&mut s).unwrap(); file.read_to_string(&mut s).unwrap();
s.contains($text) s.contains($text)
} }
} }
@ -117,6 +116,10 @@ fn test_can_build_site_without_live_reload() {
// no live reload code // no live reload code
assert_eq!(file_contains!(public, "index.html", "/livereload.js?port=1112&mindelay=10"), false); assert_eq!(file_contains!(public, "index.html", "/livereload.js?port=1112&mindelay=10"), false);
// Both pages and sections are in the sitemap
assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/posts/simple</loc>"));
assert!(file_contains!(public, "sitemap.xml", "<loc>https://replace-this-with-your-url.com/posts</loc>"));
} }
#[test] #[test]