Alex Pearwin

Jekyll sitemaps

This post has been archived. It's pretty old and likely concerns topics that are avoidable by using more modern tools and techniques. The original text is preserved below for posterity but it may no longer be relevant or correct.

For indexing websites, Google et al. have a standardised protocol for creating a sitemap. A sitemap is an XML file containing a list of all pages to be indexed. The sitemaps Wikipedia article is more informative than I am on the matter.

Anyway, I wanted to create a sitemap for this site to submit to Google. This site is very simple as it consists of only three types of pages: a post page, a tag index page, and a category index page. One post page per post, and one tag/category index page per tag/category. (For how I implemented tag/category index pages, see my post on simple Jekyll searching.

Listing all the pages is easy.

<% for post in site.posts %>
...
<% endfor %>

Listing the index pages was slightly harder, as the template data sites.tags is supposed to be used like sites.tags.TAGNAME, returning a list of all posts with the given tag name. The solution is embedded somewhere here, specifically these lines:

<% for category in site.categories %>
  <%= category | first %>
<% endfor %>

We just need to use the liquid filter first to get the category name, and equally for tags.

Consolidating it all into a sitemap.xml might yield something like mine. If you have any extra pages you can add these in a similar way to posts. Nice!