Create a sitemap for your middleman


First verify if you have a gem called builder, if you don’t add it to your Gemfile:

gem "builder", "~> 3.0"

Then you will have to create a file in source/sitemap.xml.builder:

xml.urlset "xmlns" => "" do
  sitemap.resources.each do |resource|
    xml.url do
      xml.loc URI.join(config[:host], resource.url)
    end if resource.url !~ /\.(css|js|eot|svg|woff|ttf|png|jpg|jpeg|gif|keep)$/

In my case I have this line in config.rb:

config[:host] = ""

If you want to create a sitemap.txt, create a file source/sitemap.txt.erb with this content:

<% sitemap.resources.each do |page| %><%= "#{URI.join(config[:host], page.url)}\n" if page.url !~ /\.(css|js|eot|svg|woff|ttf|png|jpg|jpeg|gif|keep)$/ %><% end %>

sitemap.txt‘s code must be all in one line.

Protip: add this line to your config.rb:

config[:ignore_sitemap_regex] = %r[\.(css|js|eot|svg|woff|ttf|png|jpg|jpeg|gif|keep)$]

And replace /\.(css|js|eot|svg|woff|ttf|png|jpg|jpeg|gif|keep)$/ with config[:ignore_sitemap_regex].

Posts in this series


Post cover by: