(function () { const toc = document.querySelector('#page-nav') // Select each header const sections = document.querySelectorAll('main h1') sections.forEach((section, id) => { // Create the header link toc.innerHTML += '' // Create the sub links container const children = document.createElement('nav') children.className = 'md-nav' children.innerHTML = '' // Select all the sub headers and add them to the sub links container let contenders = section.nextElementSibling while (contenders && contenders.tagName !== 'H1') { if (contenders.tagName === 'H2' || contenders.tagName === 'H3') { const content = '
  • ' + contenders.textContent + '
  • ' children.firstElementChild.innerHTML += content } contenders = contenders.nextElementSibling } document.querySelector('#link_' + section.id).appendChild(children) }) })()