Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@nikushi
Last active January 4, 2019 05:21
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 nikushi/c0ffae8b61f7dbf3cb06b5839e6e6620 to your computer and use it in GitHub Desktop.
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
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