Skip to content

Instantly share code, notes, and snippets.

@NightScript370
Created May 13, 2020 22:15
Show Gist options
  • Save NightScript370/62e08b71e22dd3f5175f773ae8ac406f to your computer and use it in GitHub Desktop.
Save NightScript370/62e08b71e22dd3f5175f773ae8ac406f to your computer and use it in GitHub Desktop.
pageElement.innerHTML = pageElement.innerHTML.replace(/<h([\d]).*>([^<]+)<\/h([\d])>/gi, (str, openLevel, titleText, closeLevel) => {
if (openLevel != closeLevel)
return str;
if (openLevel > level)
toc += (new Array(openLevel - level + 1)).join('<ul>');
else if (openLevel < level)
toc += (new Array(level - openLevel + 1)).join('</li></ul>');
else
toc += (new Array(level+ 1)).join('</li>');
level = parseInt(openLevel);
var anchor = titleText.toLowerCase().replace(/ /g, "_");
toc += '<li><a href="#' + anchor + '">' + titleText + '</a>';
return str;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment