Skip to content

Instantly share code, notes, and snippets.

@jorgepedret
Created October 25, 2013 19:02
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jorgepedret/7160052 to your computer and use it in GitHub Desktop.
Save jorgepedret/7160052 to your computer and use it in GitHub Desktop.
Recursive function to create dynamic sitemap of your website in Harp using Jade
- var paths = [], files = []; function recursiveTree(obj) { if (typeof obj == "object" && !obj.length) { for (item in obj) { if (item === "contents") { recursiveTree(obj[item]); } else if (item != "data") { paths.push(item); recursiveTree(obj[item]); paths.splice(paths.length-1, 1); } } } else if (typeof obj == "object") { for (i in obj) { var file = obj[i]; if (/(\.html$)/.test(file)) { paths.push(file); files.push("/" + paths.join("/")); paths.splice(paths.length-1, 1); } } } }; recursiveTree(public);
xml
urlset(xmlns="http://www.sitemaps.org/schemas/sitemap/0.9")
- for(i in files) {
url
loc= files[i]
priority 0.5
- }
@sintaxi
Copy link

sintaxi commented Oct 25, 2013

call from any template...

!= partial("_tree", { head: public, tail: "/" })

_tree.jade

for val, key in head
  if key !== '.git' && key !== 'data'
    if key == 'contents'
      each file in val
        p= tail + file
    else
      != partial("_tree", { head: val, tail: tail + key + "/" })

@sintaxi
Copy link

sintaxi commented Oct 25, 2013

much better...

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, "/")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment