Created
July 25, 2012 13:42
-
-
Save redonkulus/3176259 to your computer and use it in GitHub Desktop.
TOC example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Used with Styledocco | |
* Dynamically create table of contents from H1's on page | |
*/ | |
(function () { | |
'use strict'; | |
var x, header, article, link, | |
container = document.querySelector('section.container'), | |
sections = container.getElementsByTagName('article'), | |
length = sections.length, | |
items = [], | |
tocEl = document.getElementById('toc'); | |
// parse each section, extract H1 title and push to 'toc' array | |
if (sections) { | |
for (x = 0; x < length; x+=1) { | |
article = sections[x]; | |
if (article) { | |
header = article.getElementsByTagName('h1')[0].innerText; | |
link = article.querySelector('a.permalink').href; | |
items.push('<li><a href="' + link + '">' + header + '</a></li>'); | |
} | |
} | |
} | |
// generate table of contents markup | |
tocEl.innerHTML = '<ul>' + items.join('') + '</ul>'; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ok!
Thanks for your time mate.