Skip to content

Instantly share code, notes, and snippets.

@kolypto
Last active July 21, 2020 16:51
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 kolypto/7634692 to your computer and use it in GitHub Desktop.
Save kolypto/7634692 to your computer and use it in GitHub Desktop.
GitHub: generate TOC table of contents for README.md
// Execute me in Console on the repository page
Array.prototype.map.call(document.querySelectorAll('a[class=anchor]'), el => {
var href = el.href,
title = el.parentElement.textContent.replace(/^\s+|\s+$/g, ''),
level = parseInt(el.parentElement.tagName.substr(1)),
indent = (new Array(level)).join(' ');
return indent + '* <a href="'+href.replace(/^user-content-/, '')+'">'+title+'</a>';
}).join("\n ");
// Detect links with missing targets
$('#readme a[href^="#"]').filter('[class!=anchor]').map(function(){
var $this = $(this),
href=$this.attr('href'),
name=href.substr(1),
$targets = $('a[id="user-content-'+name+'"]');
if (!$targets.length)
console.log('Wrong link target: ', $this.get(0)); }
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment