In [ ]:
%%javascript
// Code to dynamically generate table of contents at the top of the HTML file
var tocEntries = ['<ul>'];
var anchors = $('a.anchor-link');
var headingTypes = $(anchors).parent().map(function() { return $(this).prop('tagName')});
var headingTexts = $(anchors).parent().map(function() { return $(this).text()});
var subList = false;
$.each(anchors, function(i, anch) {
var hType = headingTypes[i];
var hText = headingTexts[i];
hText = hText.substr(0, hText.length - 1);
if (hType == 'H2') {
if (subList) {
tocEntries.push('</ul>')
subList = false;
}
tocEntries.push('<li><a href="' + anch + '"</a>' + hText + '</li>')
}
else if (hType == 'H3') {
if (!subList) {
subList = true;
tocEntries.push('<ul>')
}
tocEntries.push('<li><a href="' + anch + '"</a>' + hText + '</li>')
}
});
tocEntries.push('</ul>')
$('#toc').html(tocEntries.join(' '))