Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mrabbitt/4582903 to your computer and use it in GitHub Desktop.
Save mrabbitt/4582903 to your computer and use it in GitHub Desktop.
(function() {
function addSectionNumber (elem, sectionNumber, parentSection, level) {
var sectionNumberStr = '';
var sectionSpan;
if (level > 1) {
sectionNumberStr = parentSection + '.' + sectionNumber;
} else if (level > 0) {
sectionNumberStr = sectionNumber.toString();
}
if (level > 0 && jQuery(elem).find('.section-number').length === 0 ) {
sectionSpan = jQuery('<span class="section-number">').text(sectionNumberStr + ' ');
jQuery(elem).prepend(sectionSpan);
}
if ( level === 0 ) {
jQuery(elem).find('h1').each( function(index, element) {
addSectionNumber(element, (index + 1), sectionNumberStr, (level + 1));
});
} else if ( level < 6 ) {
jQuery(elem).nextUntil('h' + level, 'h' + level + ',h' + (level + 1)).each( function(index, element) {
addSectionNumber(element, (index + 1), sectionNumberStr, (level + 1));
});
}
return;
}
addSectionNumber(jQuery('#main-content').first(), 0, '', 0);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment