Sitemap

Menu

The following Ruby code generates the XML sitemap of my website. It is composed of two parts:

Org Mode is instructed to generate a sitemap.xml file upon execution of this source block, thus generating the file.

Warning! Executing this piece of code is not enough to publish the sitemap, since the file sitemap.xml is created in the current directory. We need to ensure sitemap.xml is also copied into the _site directory. This is guaranteed by the order in which we execute the site generation commands. More in details, if we first process the org-mode files and then manage the “random bits in home”, we should be pretty sure the sitemap is copied.

sitemap_template = <<EOS
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <%- pages.each do |page| -%>
  <url>
    <loc><%= page[:url] %></loc>
    <lastmod><%= page[:updated_at] %></lastmod>
  </url>
  <%- end -%>
</urlset> 
EOS

load '_scripts/metadata.rb'
require 'erb'

pages = Metadata::collect(".").select { |x| not x[:filename].start_with?("_") }
renderer = ERB.new(sitemap_template, nil, "-")
content = renderer.result(binding)

Author: Adolfo Villafiorita

Last modified: 2020-03-29 Sun 16:52 (created on: 2020-03-11 Wed 00:00)

Published: 2020-06-05 Fri 18:24