# Create a sitemap.xml with Harpjs

> Short post on how to generate a sitemap.xml for your site thanks to a "new" public variable

URL: https://kevin.saliou.name/blog/2013-10-28-harpjs-adding-a-sitemap/
Published: 2013-10-28
Tags: harp.js, javascript, jade, mixins, seo

[Last week](https://kevin.saliou.name/blog/2013-10-21-harpjs-one-week-later/) I wrote about how I was missing some way to generate a sitemap files using Harpjs. I even [filed an issue on github](https://github.com/sintaxi/harp/issues/131), and I got a [first solution](https://github.com/sintaxi/harp/issues/131#issuecomment-27181797) 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` :

```javascript
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](https://kevin.saliou.name/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](https://github.com/kennethormandy).

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