Skip to content

Instantly share code, notes, and snippets.

@bernardodiasc
Created January 14, 2015 18:32
Show Gist options
  • Save bernardodiasc/0278c6b346c25eafdf95 to your computer and use it in GitHub Desktop.
Save bernardodiasc/0278c6b346c25eafdf95 to your computer and use it in GitHub Desktop.
var data = [],
html = '',
slice = Array.prototype.slice,
navigation = document.querySelectorAll('.navigation')[0];
// store useful values in the data array
[].forEach.call(document.querySelectorAll('section'), function(section){
data.push({
'filename': section.dataset.filename,
'h1': section.querySelectorAll('h1'),
'h2': section.querySelectorAll('h2')
});
});
html += '<ul>';
// loop trought data array and add up string in the html string
data.forEach(function(obj){
var h1 = slice.call(obj.h1),
h2 = slice.call(obj.h2);
h1.forEach(function(node) {
html += '<li><a href="#' + node.id + '">' + node.innerHTML + '</a>';
});
if (h2.length > 0) {
html += '<ul>';
h2.forEach(function(node) {
html += '<li><a href="#' + node.id + '">' + node.innerHTML + '</a></li>';
});
html += '</ul>';
}
html += '</li>';
});
html += '</ul>';
navigation.innerHTML = html;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment