Created
September 1, 2021 12:19
-
-
Save eviltester/7d8dc6e3665e038772ea8be1e5c3af98 to your computer and use it in GitHub Desktop.
Automatically Create a Markdown TOC for current page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Author
eviltester
commented
Sep 1, 2021
- 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