-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.mjs
53 lines (49 loc) · 2.25 KB
/
index.mjs
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
import {getName, getParent, getStyle, setClass, setElement, setHTML, setStyles, W} from '@taufik-nurrohman/document';
import {isDefined} from '@taufik-nurrohman/is';
function getColorFrom(node) {
let color = getStyle(node, 'color') || "", c;
// <https://www.regular-expressions.info/numericranges.html>
if (c = color.match(/^rgba\s*\(\s*([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\s*[, ]\s*([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\s*[, ]\s*([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\s*[, ]\s*([01]|0?\.\d+)\s*\)$/i)) {
return [+c[1], +c[2], +c[3], +c[4]];
}
if (c = color.match(/^rgb\s*\(\s*([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\s*[, ]\s*([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\s*[, ]\s*([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\s*\)$/i)) {
return [+c[1], +c[2], +c[3], 1];
}
return [0, 0, 0, 1];
}
const LineNumberPlugin = {
'after:highlightElement': function ({el, result, text}) {
let theCode = el,
theCodeLines = setElement(getName(el)),
theCodeNumbers = [],
theCodeParent = getParent(el),
theCodeParentIsValid = theCodeParent && 'pre' === getName(theCodeParent);
if (theCodeParentIsValid) {
for (let i = 0, j = text.split(/\n/).length; i < j; ++i) {
theCodeNumbers.push('<span>' + (i + 1) + '</span>');
}
theCodeParent.insertBefore(theCodeLines, theCode);
setStyles(theCode, {
'flex': '1'
});
setStyles(theCodeParent, {
'direction': 'ltr',
'display': 'flex'
});
setHTML(theCodeLines, theCodeNumbers.join('\n'));
setClass(theCodeLines, 'hljs'); // Inherit `background` and `padding` value(s) from the style sheet
let [r, g, b, a] = getColorFrom(theCodeLines);
setStyles(theCodeLines, {
'border-right': '2px solid rgba(' + r + ',' + g + ',' + b + ',' + (a / 10) + ')',
'text-align': 'right',
'user-select': 'none' // Disable selection on number(s)
});
}
return {el, result, text};
}
}
// Is a web browser
if (isDefined(W) && isDefined(W.hljs)) {
W.hljs.addPlugin(LineNumberPlugin);
}
export default LineNumberPlugin;