Skip to content

Instantly share code, notes, and snippets.

@eviltester
Created September 1, 2021 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eviltester/7d8dc6e3665e038772ea8be1e5c3af98 to your computer and use it in GitHub Desktop.
Save eviltester/7d8dc6e3665e038772ea8be1e5c3af98 to your computer and use it in GitHub Desktop.
Automatically Create a Markdown TOC for current page
var output="";
document.querySelectorAll("h2,h3,h4,h5").forEach((element) => {
var headingNum = element.tagName.toLowerCase().replace('h','');
var indent = (parseInt(headingNum)-2)*3;
var spaces= indent>0 ? Array(indent).fill(' ').join('') : '';
var href= element.id ? element.id : element.innerText.toLowerCase().replaceAll(" ","-")
output+=spaces + "- [" + element.innerText +"](#" + href + ")\n" })
console.log(output);
@eviltester
Copy link
Author

  • on web page
  • inspect to open console
  • paste gist into console
  • copy the generated markdown
  • script would need amending to add h1 if you want that in the list

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment