Last active
November 12, 2016 09:23
-
-
Save sebkoller/d474ed74ac6d1b1959ba81041c3f2691 to your computer and use it in GitHub Desktop.
Generates table of content markup for OpenProject wikis
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
/* | |
* Script to generate table of content markup for OpenProject wikis | |
* OpenProject: https://github.com/opf/openproject | |
* Author: Sebastian Koller | |
* | |
* Usage: Insert into the console (Ctrl-Shift-I) on a wiki page | |
* Output: [[DocumentName#1-Header|1. Header]] | |
*/ | |
var bold_h1 = true; | |
var markup = []; | |
var content = document.querySelector('.wiki-content') | |
var title = content.querySelectorAll('.wiki-title')[0].innerText | |
var headers = content.querySelectorAll('h1:not(.wiki-title), h2, h3') | |
Array.prototype.forEach.call(headers, function(el, i){ | |
let header = el.innerText | |
let header_ref = header.replace(/(\.| )+/g, '-') | |
let reference = `[[${title}#${header_ref}|${header}]]` | |
if (bold_h1 && el.tagName == 'H1') { | |
reference = `*${reference}*` | |
} | |
markup.push(reference) | |
}) | |
console.log(markup.join('\n')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment