Your Middleman blog should create a sitemap.xml

If your website runs on Middleman like this website (at time of writing) you might want to create a nice sitemap.xml for search engines to know their way on your site. This article shows how.

Create a source file source/sitemap.xml.builder.

xml.instruct!
xml.urlset 'xmlns' => "http://www.sitemaps.org/schemas/sitemap/0.9" do
  sitemap.resources.select { |page| page.destination_path =~ /\.html/ && page.data.noindex != true }.each do |page|
    xml.url do
      xml.loc URI.join(settings.casper[:blog][:url], page.destination_path)
      last_mod = if page.path.start_with?('articles/')
        File.mtime(page.source_file).to_time
      else
        Time.now
      end
      xml.lastmod last_mod.iso8601
      xml.changefreq page.data.changefreq || "monthly"
      xml.priority page.data.priority || "0.5"
    end
  end
end

This is a file using builder markup to generate XML. It traverses the Middleman Sitemap (that contains all your compiled files) to include all files in your final sitemap.xml.

If you want to exclude certain files from your sitemap. Use the following frontmatter:

---
noindex: true
---
<html>...