Last active
July 21, 2020 16:51
-
-
Save kolypto/7634692 to your computer and use it in GitHub Desktop.
GitHub: generate TOC table of contents for README.md
This file contains 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
// 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