Last active
January 4, 2019 05:21
-
-
Save nikushi/c0ffae8b61f7dbf3cb06b5839e6e6620 to your computer and use it in GitHub Desktop.
Bookmarklet to generate TOC foro github wiki on cursor point in text area
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
javascript: (() => { | |
const e = document.querySelector('#gollum-editor-body'); | |
const re = /^(#+)\s*(.+)$/; | |
const beginToc = '<!-- BEGIN TOC -->'; | |
const endToc = '<!-- END TOC -->'; | |
const lists = e.textContent.split("\n").filter(line => re.test(line)).map((line) => { | |
const m = line.match(re); | |
const tag = m[1].replace(/#$/, '*').replace(/#/g, ' '); | |
const desc = m[2]; | |
return `${tag} [${desc}](#${desc.replace(/\s/g, '-')})`; | |
}); | |
const toc = beginToc + "\n" + lists.join("\n") + "\n" + endToc + "\n"; | |
e.focus(); | |
document.execCommand('insertText', false, toc); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment