Create a sitemap.xml with Harpjs

Mon Oct 28 2013

Last week I wrote about how I was missing some way to generate a sitemap files using Harpjs. I even filed an issue on github, and I got a first solution as promised! Here it is :

  mixin tree(head, tail)
    for val, key in head
      if key !== '.git' && key !== 'data'
        if key == 'contents'
          each file in val
            p= tail + file
        else
          mixin tree(val, tail + key + "/")

  h1 Sitemap
  mixin tree(public, "/")

It introduces a new global variable public which holds all the files stored in your public directory. This example throws the results in html format, so here how I create my sitemap.xml :

mixin tree(head, tail)
  for val, key in head
    if key !== '.git' && key !== 'data'
      if key == 'contents'
        each file in val
          if /(.html$)/.test(file)
            prio = .4
            if /about/.test(tail + file)
              prio = .9
            if /posts/.test(tail + file)
              prio = .7
            if /index/.test(tail + file)
              prio = .6
            url
              loc= tail + file
              priority= prio
      else
        mixin tree(val, tail + key + "/")

doctype xml
urlset(xmlns="http://www.sitemaps.org/schemas/sitemap/0.9")
  mixin tree(public, "/")

Nice, I now have my shiny new sitemap.xml file setup for google, bing and else to consume! Note how I’m setting the priority value in a rather ugly way using regex, this is my 1st take; I bet more interesting solutions will come up soon! They mentioned they would document all this soon! Thanks @kennethormandy.

I will need to find a solution for not listing the posts that are yet to be published.