-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
254 lines (253 loc) · 8.43 KB
/
index.js
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
(function (g, f) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = f() : typeof define === 'function' && define.amd ? define(f) : (g = typeof globalThis !== 'undefined' ? globalThis : g || self, (g.hljs = g.hljs || {}, g.hljs.LineNumberPlugin = f()));
})(this, (function () {
'use strict';
var isArray = function isArray(x) {
return Array.isArray(x);
};
var isDefined = function isDefined(x) {
return 'undefined' !== typeof x;
};
var isInstance = function isInstance(x, of) {
return x && isSet(of) && x instanceof of ;
};
var isNull = function isNull(x) {
return null === x;
};
var isNumber = function isNumber(x) {
return 'number' === typeof x;
};
var isNumeric = function isNumeric(x) {
return /^-?(?:\d*.)?\d+$/.test(x + "");
};
var isObject = function isObject(x, isPlain) {
if (isPlain === void 0) {
isPlain = true;
}
if ('object' !== typeof x) {
return false;
}
return isPlain ? isInstance(x, Object) : true;
};
var isSet = function isSet(x) {
return isDefined(x) && !isNull(x);
};
var isString = function isString(x) {
return 'string' === typeof x;
};
var toCaseCamel = function toCaseCamel(x) {
return x.replace(/[-_.](\w)/g, function (m0, m1) {
return toCaseUpper(m1);
});
};
var toCaseLower = function toCaseLower(x) {
return x.toLowerCase();
};
var toCaseUpper = function toCaseUpper(x) {
return x.toUpperCase();
};
var toNumber = function toNumber(x, base) {
if (base === void 0) {
base = 10;
}
return base ? parseInt(x, base) : parseFloat(x);
};
var toValue = function toValue(x) {
if (isArray(x)) {
return x.map(function (v) {
return toValue(v);
});
}
if (isNumeric(x)) {
return toNumber(x);
}
if (isObject(x)) {
for (var k in x) {
x[k] = toValue(x[k]);
}
return x;
}
if ('false' === x) {
return false;
}
if ('null' === x) {
return null;
}
if ('true' === x) {
return true;
}
return x;
};
var fromValue = function fromValue(x) {
if (isArray(x)) {
return x.map(function (v) {
return fromValue(x);
});
}
if (isObject(x)) {
for (var k in x) {
x[k] = fromValue(x[k]);
}
return x;
}
if (false === x) {
return 'false';
}
if (null === x) {
return 'null';
}
if (true === x) {
return 'true';
}
return "" + x;
};
var D = document;
var W = window;
var getName = function getName(node) {
return toCaseLower(node && node.nodeName || "") || null;
};
var getParent = function getParent(node, query) {
return node.parentNode || null;
};
var getStyle = function getStyle(node, style, parseValue) {
if (parseValue === void 0) {
parseValue = true;
}
var value = W.getComputedStyle(node).getPropertyValue(style);
if (parseValue) {
value = toValue(value);
}
return value || "" === value || 0 === value ? value : null;
};
var hasState = function hasState(node, state) {
return state in node;
};
var letAttribute = function letAttribute(node, attribute) {
return node.removeAttribute(attribute), node;
};
var letStyle = function letStyle(node, style) {
return node.style[toCaseCamel(style)] = null, node;
};
var setAttribute = function setAttribute(node, attribute, value) {
if (true === value) {
value = attribute;
}
return node.setAttribute(attribute, fromValue(value)), node;
};
var setAttributes = function setAttributes(node, attributes) {
var value;
for (var attribute in attributes) {
value = attributes[attribute];
if (value || "" === value || 0 === value) {
setAttribute(node, attribute, value);
} else {
letAttribute(node, attribute);
}
}
return node;
};
var setClass = function setClass(node, value) {
return node.classList.add(value), node;
};
var setElement = function setElement(node, content, attributes) {
node = isString(node) ? D.createElement(node) : node;
if (isObject(content)) {
attributes = content;
content = false;
}
if (isString(content)) {
setHTML(node, content);
}
if (isObject(attributes)) {
setAttributes(node, attributes);
}
return node;
};
var setHTML = function setHTML(node, content, trim) {
if (trim === void 0) {
trim = true;
}
if (null === content) {
return node;
}
var state = 'innerHTML';
return hasState(node, state) && (node[state] = trim ? content.trim() : content), node;
};
var setStyle = function setStyle(node, style, value) {
if (isNumber(value)) {
value += 'px';
}
return node.style[toCaseCamel(style)] = fromValue(value), node;
};
var setStyles = function setStyles(node, styles) {
var value;
for (var style in styles) {
value = styles[style];
if (value || "" === value || 0 === value) {
setStyle(node, style, value);
} else {
letStyle(node, style);
}
}
return node;
};
function getColorFrom(node) {
var 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];
}
var LineNumberPlugin = {
'after:highlightElement': function afterHighlightElement(_ref) {
var el = _ref.el,
result = _ref.result,
text = _ref.text;
var theCode = el,
theCodeLines = setElement(getName(el)),
theCodeNumbers = [],
theCodeParent = getParent(el),
theCodeParentIsValid = theCodeParent && 'pre' === getName(theCodeParent);
if (theCodeParentIsValid) {
for (var 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
var _getColorFrom = getColorFrom(theCodeLines),
r = _getColorFrom[0],
g = _getColorFrom[1],
b = _getColorFrom[2],
a = _getColorFrom[3];
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: el,
result: result,
text: text
};
}
};
// Is a web browser
if (isDefined(W) && isDefined(W.hljs)) {
W.hljs.addPlugin(LineNumberPlugin);
}
return LineNumberPlugin;
}));