Skip to content
Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
42 lines (39 sloc) 1.46 KB
<script>// Add table of contents! <![CDATA[
function $get(selector) { return document.querySelector(selector); };
function $all(selector) {
return Array.prototype.slice.call(document.querySelectorAll(selector));
}
function buildTOC_ul(selector) {
var levels=[document.createElement("ul"),null,null];
levels[0].style
var lvl=0, c=0;
if (!selector) selector = "h2, h3, h4";
$all(selector).forEach(function(el) {
if (!el.id) el.id='section_'+ ++c;
var newLvl=(el.tagName=="H2"?0:el.tagName=="H3"?1:2);
for (;lvl<newLvl;lvl++)
levels[lvl].appendChild(levels[lvl+1]=document.createElement("ul"));
lvl=newLvl;
var li=document.createElement('li');
li.innerHTML="<a href='#"+el.id+"'></a>";
li.firstChild.innerHTML=el.innerHTML;
levels[lvl].appendChild(li);
});
return levels[0];
}
function addTOC(contentElement, before, tocClass) {
if (before===undefined) before=contentElement.firstChild;
var prefix="";
var cec=contentElement.className;
if (cec && cec.length>0) prefix="."+cec.split(" ")[0]+" ";
var selector = prefix+"h2, "+prefix+"h3, "+prefix+"h4";
var toc=document.createElement("div");
toc.className=tocClass||"sidebox";
toc.appendChild(document.createTextNode("Contents"));
toc.appendChild(buildTOC_ul(selector));
contentElement.insertBefore(toc, before);
}
var _post_ = $get("#post")||$get("#content")||$get("main")||$get("section");
addTOC(_post_, _post_.firstChild.nextSibling.nextSibling);
//]]>
</script>
You can’t perform that action at this time.