Skip to content

Instantly share code, notes, and snippets.

@redonkulus
Created July 25, 2012 13:42
Show Gist options
  • Save redonkulus/3176259 to your computer and use it in GitHub Desktop.
Save redonkulus/3176259 to your computer and use it in GitHub Desktop.
TOC example
/**
* 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>';
}());
@redonkulus
Copy link
Author

I think you will have to do it manually unless Gist Markup allows for TOC generation. Something I'm not aware of today.

@raimonizard
Copy link

Ok!
Thanks for your time mate.

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