Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sitemap (sitemaps.org) generator for Middleman
<% pages = sitemap.resources.find_all{|p| p.source_file.match(/\.html/) } %>
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<% pages.each do |p| %>
<url>
<loc>http://youdomain.com/<%=p.destination_path.gsub('/index.html','')%></loc>
<priority>0.7</priority>
</url>
<% end %>
</urlset>
@benfrain

This comment has been minimized.

Copy link

benfrain commented May 8, 2013

Brilliant, thanks.

@moorbrook

This comment has been minimized.

Copy link

moorbrook commented May 20, 2013

Thank you for this!

Add to the frontmatter


layout: false
directory_index: false


to disable layout

@iparr

This comment has been minimized.

Copy link

iparr commented Dec 11, 2013

Great stuff this. Cheers.
You can also use:

page "/sitemap.xml", :layout => false

in config.rb if for some reason you don't like @moorbrook 's frontmatter!

@karimmtarek

This comment has been minimized.

Copy link

karimmtarek commented Dec 20, 2013

It's just what I was looking for, Thanks @lukebowerman

@SteveBenner

This comment has been minimized.

Copy link

SteveBenner commented Apr 27, 2014

For those who prefer a cleaner, leaner, meaner syntax, here’s the same gist in Slim: https://gist.github.com/SteveBenner/8897566

@doertedev

This comment has been minimized.

Copy link

doertedev commented Oct 1, 2014

Aaaaaaaand here goes HAML:

---
layout: false

---
- pages = sitemap.resources.find_all{ |page| page.source_file.match(/\.haml/) }
!!! XML
%urlset{ :xmlns => "http://www.sitemaps.org/schemas/sitemap/0.9" }
  - pages.each do |page|
    %url
      %loc
        =page.destination_path.gsub("/index.html", "")
      %priority
        0.7

If the !!!xml doesnt generate the corresponding tag, just write it in plain text.

@doertedev

This comment has been minimized.

Copy link

doertedev commented Oct 1, 2014

Alternatively for people working with a navigation built on data files is an approach inspired by a blog post I just found:

---
layout: false

---
<?xml version="1.0" encoding="UTF-8"?>
-today = Time.new.iso8601
%urlset{ :xmlns => "http://www.sitemaps.org/schemas/sitemap/0.9" }
  -sitemap.app.data.navigation.items.each do |page|
    %url
      %loc
        =sitemap.app.config.deployment_url + page.url
      %priority
        0.7
      %changefreq
        "monthly"
      %lastmod
        = today

sitemap.app.config << means I :set deployment_url, "$actualUrl" in my config, another nice way I found worth mentioning.

@rowanhogan

This comment has been minimized.

Copy link

rowanhogan commented Apr 29, 2015

For anyone stumbling across this who is using redirects, you might want to update the method which fetches pages to something like:

<% pages = sitemap.resources.reject{|r| r.is_a? Middleman::Sitemap::Extensions::Redirects::RedirectResource }.find_all{|p| p.source_file.match(/\.html/) } %>
@sathishmanohar

This comment has been minimized.

Copy link

sathishmanohar commented Jan 19, 2016

Works great!

There are three things I would want to achieve further

  • In case of using directory_indexes, the root path still shows index.html in the end
  • How to hide certain pages from sitemap (preferably using frontmatter)
  • How to set page specific priority (preferably using frontmatter)
@sathishmanohar

This comment has been minimized.

Copy link

sathishmanohar commented Jan 19, 2016

Incase of directory_indexes using url instead of destination_path does the trick

<loc>http://youdomain.com<%=p.url.gsub('/index.html','')%></loc>

@sathishmanohar

This comment has been minimized.

Copy link

sathishmanohar commented Jan 19, 2016

I tweaked this gist to the following code for sitemap.xml.erb it works with directory indexes and has ability to hide pages from sitemap and set priorities for each page from frontmatter, priority defaults to 0.5


---
layout: false
directory_index: false

---
<% pages = sitemap.resources.find_all{|p| p.source_file.match(/\.html/) && !p.data.sitemap_noindex == true } %>
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<% pages.each do |p| %>
  <url>
    <loc>http://balcozy.com<%=p.url.gsub('/index.html','')%></loc>
    <changefreq>monthly</changefreq>
    <priority><%= p.data.sitemap_priority || 0.5 %></priority>
  </url>
<% end %>
</urlset>

In page.html.erb use sitemap_noindex: true and sitemap_priority: whenever needed


---
sitemap_noindex: true
sitemap_priority: 0.8

---
@raefa

This comment has been minimized.

Copy link

raefa commented Feb 26, 2018

For anyone coming across this recently, I had to modify the suggestion above to work with redirects. My sitemap.xml.erb file looks like this:

---
layout: false
directory_index: false
---
<% pages = sitemap.resources.reject{|r| r.is_a? Middleman::Sitemap::Extensions::RedirectResource }.find_all{|p| p.source_file.match(/\.html/) && !p.data.sitemap_noindex == true } %>
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<% pages.each do |p| %>
  <url>
    <loc>https://example.com<%=p.url.gsub('/index.html','')%></loc>
    <changefreq>monthly</changefreq>
    <priority><%= p.data.sitemap_priority || 0.5 %></priority>
  </url>
<% end %>
</urlset>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.