<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  {% for post in posts %}
  <url>
    <loc>{{ post.permalink }}</loc>
    {% if post.updated %}
    <lastmod>{{ post.updated | formatDate }}</lastmod>
    {% elif post.date %}
    <lastmod>{{ post.date | formatDate }}</lastmod>
    {% endif %}
  </url>
  {% endfor %}

  {# home page #}
  <url>
    <loc>{{ config.root | fullUrlFor }}</loc>
    <lastmod>{% if posts[0].updated %}{{ posts[0].updated | formatDate }}{% else %}{{ posts[0].date | formatDate }}{% endif %}</lastmod>
  </url>

  {# archive page #}
  <url>
    <loc>{{ config.archive_dir | fullUrlFor + '/' }}</loc>
    <lastmod>{% if posts[0].updated %}{{ posts[0].updated | formatDate }}{% else %}{{ posts[0].date | formatDate }}{% endif %}</lastmod>
   </url>
  
  {% for tag in tags %}
  <url>
    <loc>{{ tag.permalink }}</loc>
    <lastmod>{% if tag.posts.first().updated %}{{ tag.posts.first().updated | formatDate }}{% else %}{{ tag.posts.first().date | formatDate }}{% endif %}</lastmod>
  </url>
  {% endfor %}
</urlset>