MediaWiki:CodeblockLineNumbers/code.js

/** * @name CodeblockLineNumbers * @desc Adds numbers to each line of code in a codeblock. * @author Arashiryuu0 */ (function {   var codeblocks = ['de1', 'theme-solarized-light', 'theme-solarized-dark'],        ourblocks = Array.prototype.slice.call(document.querySelectorAll('pre'));    if (!ourblocks.length) return;    function noOl(codeblock) {        return !codeblock.querySelector('ol');    }    function addLines(codeblock) {        codeblock.innerHTML = codeblock.innerHTML.split('\n').map(function(line) { if (line.trim.indexOf('*') === 0 || line.trim.indexOf('/*') === 0) { return '' + line + ''; } else if (!!line) { return '' + line + ''; }       })        .join('');        return codeblock;    }    function ourFilter(codeblock) {        if (codeblock.tagName === 'PRE') {            return codeblocks.includes(codeblock.id) || (codeblock.classList && codeblocks.includes(codeblock.classList[0]));        }    }    function wrapInner(parent, wrapper) {        if (typeof wrapper === 'string') wrapper = document.createElement(wrapper);        parent.appendChild(wrapper);        while (parent.firstChild !== wrapper) wrapper.appendChild(parent.firstChild);    }    function processCodeblocks {        var filtered = ourblocks.filter(noOl).filter(ourFilter),            len = filtered.length, ol, i;        if (!len) return;        importArticle({ type: 'style', article: 'u:dev:MediaWiki:CodeblockLineNumbers/code.css' });       i = 0;        for (i; i < len; i++) {            ol = document.createElement('ol');            ol.setAttribute('class', 'lineNumbers');            wrapInner(addLines(filtered[i]), ol);        }    }    processCodeblocks; });