-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeblockLineNumbers.js
More file actions
270 lines (208 loc) · 7.67 KB
/
CodeblockLineNumbers.js
File metadata and controls
270 lines (208 loc) · 7.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
* Adds numbers to each line of code in a codeblock.
* @name CodeblockLineNumbers
* @author Arashiryuu0
* @version 1.2.5
* Last modified: 1768745144296
*/
/*
jshint
undef: true,
noarg: true,
devel: true,
jquery: true,
strict: true,
eqeqeq: true,
freeze: true,
newcap: true,
esnext: true,
browser: true,
latedef: true,
shadow: outer,
varstmt: false,
laxbreak: true,
quotmark: single,
singleGroups: true,
futurehostile: true
*/
/*
globals
Symbol
*/
;((mw) => {
'use strict';
if (window.dev && window.dev.CodeblockLineNumbers) return;
const classes = [
'lineNumbers',
'obs'
];
const codeblocks = [
['de1', 'hljs'],
['theme-solarized-light', 'theme-solarized-dark'],
['.mw-highlight pre', 'code pre', '.pi-data-value pre']
];
const ourblocks = [...document.querySelectorAll('pre')];
if (!ourblocks.length) return;
window.importArticles({
type: 'style',
articles: ['u:dev:MediaWiki:CodeblockLineNumbers.css']
});
const noOl = (codeblock) => !codeblock.querySelector('ol');
const mapLine = (line) => {
if (!!line) return '<li>' + line + '</li>';
};
const addLines = (codeblock) => {
codeblock.innerHTML = codeblock.innerHTML
.split('\n')
.map(mapLine)
.join('');
return codeblock;
};
const ourFilter = (codeblock) => {
const hasId = () => codeblocks[1].includes(codeblock.getAttribute('id'));
const hasClass = () => codeblocks[0].some((name) => codeblock.classList && codeblock.classList.contains(name));
const ucpBlock = () => codeblocks[2].some((selector) => codeblock.matches(selector));
if (codeblock.tagName === 'PRE' && codeblock.parentElement.tagName !== 'PRE') {
return hasId() || hasClass() || ucpBlock();
}
return false;
};
const combineFilters = (...filters) => (codeblock) => filters.every((filter) => filter(codeblock));
const wrapInner = (parent, wrapper) => {
if (typeof wrapper === 'string') wrapper = document.createElement(wrapper);
parent.appendChild(wrapper);
while (parent.firstChild !== wrapper) wrapper.appendChild(parent.firstChild);
};
const isCommentStart = (child) => child.textContent.trim().startsWith('/*');
const isInlineComment = (child) => {
const blockClasses = ['coMULTI', 'hljs-comment'];
const hasClass = () => {
const a = Boolean(child.className) && blockClasses.includes(child.className);
const b = Boolean(child.firstElementChild) && blockClasses.includes(child.firstElementChild.className);
return a || b;
};
const starts = child.textContent.trim().startsWith('/*');
const ends = child.textContent.trim().endsWith('*/');
return (hasClass() && starts && ends) || starts && ends; // jshint ignore: line
};
const getEndIndex = (i) => (child, ind) => {
const greater = ind > i;
const trimmed = child.textContent.trim();
const ends = trimmed[0] === '*' && trimmed.endsWith('*/');
return Boolean(greater && ends);
};
const mapSlicedChildren = (child) => child.cloneNode(true);
const createUL = () => {
var ul = document.createElement('ul');
ul.classList.add('no-list');
return ul;
};
const remove = (t) => void t.parentElement.removeChild(t);
const filterSliced = (group) => (children, index) => {
const a = index >= group.start || index <= group.end;
const b = children.tagName !== 'UL';
return a && b;
};
const addCommentGroup = (codeblock, group) => {
const pe = codeblock.parentElement;
const cn = pe.className === 'de1' || pe.getAttribute('id');
group.parent.classList.add(cn ? 'coMULTI' : 'hljs-comment');
for (const child of group.children) {
group.parent.appendChild(child);
}
const end = codeblock.children[group.end];
if (codeblock.children.length - 1 === group.end) {
codeblock.appendChild(group.parent);
} else if (end) {
end.insertAdjacentElement('afterend', group.parent);
}
const fn = filterSliced(group);
const children = [...codeblock.children];
const filtered = children.slice(group.start, group.end + 1).filter(fn);
for (const fil of filtered) {
remove(fil);
}
};
const parseComments = (codeblock) => {
const groups = [];
const children = [...codeblock.children];
for (let i = 0, len = children.length; i < len; i++) {
let start = 0;
let end = 0;
const child = children[i];
if (!isCommentStart(child) || isInlineComment(child)) continue;
start = i;
end = children.findIndex(getEndIndex(start));
const sliced = children.slice(start, end + 1).map(mapSlicedChildren);
groups.push({
start: start,
end: end,
parent: createUL(),
children: sliced
});
}
if (!groups.length) return;
for (let i = groups.length - 1, len = 0; i >= len; i--) {
addCommentGroup(codeblock, groups[i]);
}
};
const match = (block, selector) => block.matches(selector);
const processCodeblock = (block) => {
if (block.querySelector('ol.lineNumbers')) return;
const ol = document.createElement('ol');
const matched = match(block, codeblocks[2][1]);
const ignore = match(block, codeblocks[2][2]);
if (ignore) return;
ol.classList.add(...classes);
if (!block.parentElement.classList.contains('mw-highlight-lines')) ol.classList.remove('obs');
/**
* weird edge-case, <syntaxhighlight line> element but doesn't render any linenos?
* see Luxon for reference {@link https://sky-children-of-the-light.fandom.com/wiki/MediaWiki:Luxon.js}
*/
if (block.matches('.mw-highlight-lines pre') && !block.querySelector('.linenos')) ol.classList.remove('obs');
wrapInner(addLines(block), ol);
/** remove empty trailing <li>s */
if (!ol.lastElementChild.childNodes.length) ol.removeChild(ol.lastElementChild);
if (matched || !mw.user.getRights) parseComments(ol);
};
const processCodeblocks = () => {
const filtered = ourblocks.filter(combineFilters(noOl, ourFilter));
const len = filtered.length;
if (!len) return;
for (const f of filtered) processCodeblock(f);
};
const defaults = {
ready: true
};
const obj = {
processBlock: processCodeblock,
process: processCodeblocks,
settings: Object.assign({}, defaults, window.CodeblockLinesConfig)
};
Object.defineProperty(obj, Symbol.toStringTag, {
configurable: false,
writable: false,
value: 'CodeblockLineNumbers'
});
Object.freeze(obj);
window.dev = window.dev || {};
window.dev.CodeblockLineNumbers = obj;
const fire = () => {
const lines = window.dev.CodeblockLineNumbers;
const settings = lines.settings;
if (typeof settings.delay === 'number' && !Number.isNaN(settings.delay) && settings.delay >= 0) {
setTimeout(lines.process, settings.delay);
} else if (typeof settings.loadAction === 'function') {
settings.loadAction(lines);
} else if (settings.ready) {
lines.process();
}
mw.hook('dev.CodeblockLineNumbers').fire(lines);
};
const ready = () => {
if (document.readyState !== 'complete') return setTimeout(ready, 1000);
Promise.resolve().then(fire, console.error);
};
window.requestIdleCallback(ready);
})(window.mediaWiki);
/*@end@*/