Created
August 7, 2012 17:59
-
-
Save ls-lukebowerman/3287848 to your computer and use it in GitHub Desktop.
Sitemap (sitemaps.org) generator for Middleman
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% 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> |
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)
Incase of directory_indexes using url
instead of destination_path
does the trick
<loc>http://youdomain.com<%=p.url.gsub('/index.html','')%></loc>
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
---
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
For anyone stumbling across this who is using redirects, you might want to update the method which fetches pages to something like: