Created
January 14, 2016 16:49
-
-
Save Data-ptr/531cb9e149f882241dbb to your computer and use it in GitHub Desktop.
For use on GitLab circa 14-01-2016. Injects a "Create ToC?" link under the project's name if Markdown style anchors are found on the page. Click the link and a Markdown compatable table of contents will appear in the browser's console! Copy-and-paste it into your Markdown (like README.md).
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
| // ==UserScript== | |
| // @name GitLab Markdown TOC Generator | |
| // @namespace GLMdToCg | |
| // @description For use on GitLab circa 14-01-2016. Injects a "Create ToC?" link under the project's name if Markdown style anchors are found on the page. Click the link and a Markdown compatable table of contents will appear in the browser's console! Copy-and-paste it into your Markdown (like README.md). | |
| // @include *gitlab.com* | |
| // @version 1 | |
| // @grant none | |
| // ==/UserScript== | |
| window.addEventListener( | |
| 'load', | |
| function pageLoadListener() { | |
| var anchors = document.querySelectorAll('a.anchor'); | |
| if (anchors && | |
| anchors.length) | |
| { | |
| var title = document.querySelector('h1.title'); | |
| if(title) { | |
| var newLink = document.createElement('A'); | |
| newLink.setAttribute('id', 'GLMdToCg-run'); | |
| newLink.textContent = 'Generate ToC?'; | |
| newLink.addEventListener( | |
| 'click', | |
| function harvestAnchors() { | |
| var tocString = ''; | |
| for (var i in anchors) { | |
| var anchor = anchors[i]; | |
| if (anchor && | |
| anchor.attributes) | |
| { | |
| var text = /\s*(.*)\s*/m.exec(anchor.parentNode.textContent)[1]; | |
| var link = anchor.attributes['href'].value; | |
| var level = /H(\d)/.exec(anchor.parentNode.nodeName) [1]; | |
| var contentString = '- [' + text + '](' + link + ')'; | |
| tocString += '\n' + ' '.repeat(level - 1) + contentString; | |
| } | |
| } | |
| console.log(tocString); | |
| }, | |
| false | |
| ); | |
| title.parentNode.insertBefore(newLink, null); | |
| } | |
| } | |
| }, | |
| false | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment