Skip to content

Instantly share code, notes, and snippets.

@nikcorg
Created March 6, 2013 14:00
Show Gist options
  • Save nikcorg/5099479 to your computer and use it in GitHub Desktop.
Save nikcorg/5099479 to your computer and use it in GitHub Desktop.
Array.prototype.splice.call(document.querySelectorAll("h2,h3,h4,h5,h6"), 0).
map(function (el, n) {
var txt, id;
id = el.getAttribute("id");
txt = el.innerText;
if (!id) {
el.setAttribute("id", encodeURIComponent(txt));
}
return el;
}).
forEach(function (el) {
var a = document.createElement("a");
a.setAttribute("href", "#" + el.getAttribute("id"));
a.appendChild(el.firstChild);
el.appendChild(a);
});
var navlist = Array.prototype.splice.call(document.querySelectorAll("h2,h3,h4,h5,h6"), 0).
map(function (el, n) {
var txt, id;
id = el.getAttribute("id");
txt = el.innerText;
if (!id) {
el.setAttribute("id", encodeURIComponent(txt));
}
return el;
}).
map(function (el) {
var a = document.createElement("a");
a.setAttribute("href", "#" + el.getAttribute("id"));
a.appendChild(document.createTextNode(el.innerText));
return a;
}).
map(function (a) {
var li = document.createElement("li");
li.appendChild(a);
return li;
}).
reduce(function (ul, li) {
ul.appendChild(li);
return ul;
}, document.createElement("ul"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment