Browse Source

Add sections to sitemap

index-subcmd
Vincent Prouillet 7 years ago
parent
commit
d8995c156c
3 changed files with 12 additions and 2 deletions
  1. +2
    -0
      src/site.rs
  2. +6
    -1
      src/templates/sitemap.xml
  3. +4
    -1
      tests/site.rs

+ 2
- 0
src/site.rs View File

@@ -335,6 +335,8 @@ impl Site {
fn render_sitemap(&self) -> Result<()> {
let mut context = Context::new();
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)?;

create_file(self.output_path.join("sitemap.xml"), &sitemap)?;


+ 6
- 1
src/templates/sitemap.xml View File

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

+ 4
- 1
tests/site.rs View File

@@ -78,7 +78,6 @@ macro_rules! file_contains {
let mut file = File::open(&path).unwrap();
let mut s = String::new();
file.read_to_string(&mut s).unwrap();

s.contains($text)
}
}
@@ -117,6 +116,10 @@ fn test_can_build_site_without_live_reload() {

// no live reload code
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]


Loading…
Cancel
Save