Skip to content

Instantly share code, notes, and snippets.

@nkgokul
Created January 21, 2019 19:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nkgokul/2690b1c7a11c6a9de5d177ae11bcf293 to your computer and use it in GitHub Desktop.
Save nkgokul/2690b1c7a11c6a9de5d177ae11bcf293 to your computer and use it in GitHub Desktop.
Generate TOC for published medium articles
getTOCHtml();
function getTOCHtml() {
let TOCHtml = ""
$$("h2,h3,h4,h5").forEach(function(ele){
TOCHtml += `<a href='#${ele.id}'>${ele.innerText}</a><br/>` ;
//console.log(ele.innerText);
})
//console.log(TOCHtml);
var parentElement = $('.postArticle-content');
// Get a reference to the first child
var theFirstChild = parentElement.firstChild;
// Create a new element
var newElement = document.createElement("div");
newElement.innerHTML = TOCHtml
// Insert the new element before the first child
parentElement.insertBefore(newElement, theFirstChild);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment