I think I have it working. I've got this running on my machine and if you want a copy, just reach out. I don't use Nunjucks normally so forgive any dumb mistake. The first mod I did to sitemap.njk was to hide collections.all:
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{%- for page in collections.all %}
{%- if not page.data.hidden %}
{%- if page.url !== "/category/all/" %}
<url>
<loc>{{ site.url }}{{ page.url | url }}</loc>
<lastmod>{{ page.date | htmlDateDisplay }}</lastmod>
</url>
{% endif %}
{%- endif %}
{%- endfor %}
Kinda hacky but worked. Next, I needed a way to get the blog category pages. I looked at tags.njk. Based on what I saw there, I wrote a filter for .eleventy.js named categories. I do not think this is a great name:
eleventyConfig.addFilter("categories", function(collections) {
let categories = Object.keys(collections).filter(c => c !== 'all');
return categories;
});
Back in the sitemap, I then did this:
{%- set cats = collections | categories %}
{%- for cat in cats %}
{% set newestDate = collections[cat] | getLatestDate %}
<url>
<loc>{{ site.url }}/category/{{ cat }}/</loc>
<lastmod>{{ newestDate | htmlDateDisplay }}</lastmod>
</url>
{%- endfor %}
</urlset>
Note the getLatestDate
filter, this is defined as such:
eleventyConfig.addFilter("getLatestDate", function(collection) {
console.log('running getLatestDate');
return collection[0].date;
});
It seemed to work well. Here is my output:
<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://skeleventy.netlify.app/blog/post-1/</loc>
<lastmod>2020-8-26</lastmod>
</url>
<url>
<loc>https://skeleventy.netlify.app/blog/post-2/</loc>
<lastmod>2020-8-26</lastmod>
</url>
<url>
<loc>https://skeleventy.netlify.app/blog/post-3/</loc>
<lastmod>2020-8-26</lastmod>
</url>
<url>
<loc>https://skeleventy.netlify.app/about/</loc>
<lastmod>2020-8-26</lastmod>
</url>
<url>
<loc>https://skeleventy.netlify.app/blog/</loc>
<lastmod>2020-8-26</lastmod>
</url>
<url>
<loc>https://skeleventy.netlify.app/contact/</loc>
<lastmod>2020-8-26</lastmod>
</url>
<url>
<loc>https://skeleventy.netlify.app/</loc>
<lastmod>2020-8-26</lastmod>
</url>
<url>
<loc>https://skeleventy.netlify.app/category/blog/</loc>
<lastmod>2020-8-26</lastmod>
</url>
<url>
<loc>https://skeleventy.netlify.app/category/business/</loc>
<lastmod>2020-8-26</lastmod>
</url>
<url>
<loc>https://skeleventy.netlify.app/category/digital/</loc>
<lastmod>2020-8-26</lastmod>
</url>
<url>
<loc>https://skeleventy.netlify.app/category/health/</loc>
<lastmod>2020-8-26</lastmod>
</url>
</urlset>
If you want a complete copy, just reach out to me.