codelight.js 1.1.1 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGES.md +16 -0
- package/README.CDN.md +1 -1
- package/README.md +24 -14
- package/SUPPORTED_LANGUAGES.md +17 -2
- package/build/codelight.js +566 -74
- package/build/codelight.min.js +74 -17
- package/build/styles/dark.css +45 -1
- package/build/styles/dark.min.css +1 -1
- package/build/styles/default.css +62 -6
- package/build/styles/default.min.css +1 -1
- package/build/styles/light.css +46 -2
- package/build/styles/light.min.css +1 -1
- package/es/{chunk-ZYEPC5GO.js → chunk-F46VGBFI.js} +378 -68
- package/es/codelight.d.ts +72 -37
- package/es/codelight.js +193 -8
- package/es/lines-BCdhnSO4.d.ts +8 -0
- package/es/react.d.ts +7 -4
- package/es/react.js +22 -8
- package/lib/{chunk-KY23A6M5.cjs → chunk-HSSBKI63.cjs} +379 -69
- package/lib/codelight.cjs +205 -20
- package/lib/codelight.d.cts +72 -37
- package/lib/lines-BCdhnSO4.d.cts +8 -0
- package/lib/react.cjs +22 -8
- package/lib/react.d.cts +7 -4
- package/package.json +2 -2
|
@@ -1,8 +1,45 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }/*! codelight.js 1.
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }/*! codelight.js 1.3.0 | MIT License | https://github.com/codelightjs/codelight.js */
|
|
2
2
|
var __defProp = Object.defineProperty;
|
|
3
3
|
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
4
4
|
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
5
|
|
|
6
|
+
// src/lib/copy.ts
|
|
7
|
+
async function copyText(text, doc = document) {
|
|
8
|
+
try {
|
|
9
|
+
await doc.defaultView.navigator.clipboard.writeText(text);
|
|
10
|
+
} catch (e) {
|
|
11
|
+
const area = doc.createElement("textarea");
|
|
12
|
+
area.value = text;
|
|
13
|
+
area.setAttribute("readonly", "");
|
|
14
|
+
area.style.cssText = "position:fixed;top:-1000px;opacity:0";
|
|
15
|
+
doc.body.append(area);
|
|
16
|
+
area.select();
|
|
17
|
+
doc.execCommand("copy");
|
|
18
|
+
area.remove();
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
function addCopyButton(code, options = {}) {
|
|
22
|
+
const pre = code.parentElement;
|
|
23
|
+
if (!pre || [...pre.children].some((c) => c.classList.contains("cl-copy"))) return null;
|
|
24
|
+
const doc = code.ownerDocument;
|
|
25
|
+
const label = _nullishCoalesce(options.label, () => ( "Copy"));
|
|
26
|
+
const button = doc.createElement("button");
|
|
27
|
+
button.type = "button";
|
|
28
|
+
button.className = "cl-copy";
|
|
29
|
+
button.textContent = label;
|
|
30
|
+
button.setAttribute("aria-label", "Copy code");
|
|
31
|
+
let timer;
|
|
32
|
+
button.addEventListener("click", async () => {
|
|
33
|
+
await copyText(_nullishCoalesce(code.textContent, () => ( "")), doc);
|
|
34
|
+
button.textContent = _nullishCoalesce(options.copiedLabel, () => ( "Copied"));
|
|
35
|
+
clearTimeout(timer);
|
|
36
|
+
timer = setTimeout(() => button.textContent = label, 1500);
|
|
37
|
+
});
|
|
38
|
+
pre.classList.add("cl-has-copy");
|
|
39
|
+
pre.append(button);
|
|
40
|
+
return button;
|
|
41
|
+
}
|
|
42
|
+
|
|
6
43
|
// src/lib/engine.ts
|
|
7
44
|
var LineBuilder = class {
|
|
8
45
|
constructor() {
|
|
@@ -22,17 +59,9 @@ var IDENT = /[A-Za-z_$][\w$]*/;
|
|
|
22
59
|
var SPACE = /\s+/y;
|
|
23
60
|
var OPERATOR = /[+\-*/%=<>!&|^~?:]+/y;
|
|
24
61
|
var CALL = /\s*\(/y;
|
|
62
|
+
var HEREDOC = /<<(-?)\s*(['"]?)([A-Za-z_]\w*)\2/y;
|
|
25
63
|
var PUNCTUATION = "()[]{},;.";
|
|
26
|
-
|
|
27
|
-
for (let j = from; j < line.length; j++) {
|
|
28
|
-
if (escape && line[j] === "\\") {
|
|
29
|
-
j++;
|
|
30
|
-
continue;
|
|
31
|
-
}
|
|
32
|
-
if (line.startsWith(close, j)) return j + close.length;
|
|
33
|
-
}
|
|
34
|
-
return -1;
|
|
35
|
-
}
|
|
64
|
+
var PAIRS = { "}": "{", ")": "(", "]": "[" };
|
|
36
65
|
function defineLanguage(def) {
|
|
37
66
|
const ci = _nullishCoalesce(def.caseInsensitive, () => ( false));
|
|
38
67
|
const keywords = words(def.keywords, ci);
|
|
@@ -41,9 +70,11 @@ function defineLanguage(def) {
|
|
|
41
70
|
const variables = words(def.variables, ci);
|
|
42
71
|
const builtins = words(def.builtins, ci);
|
|
43
72
|
const rules = (_nullishCoalesce(def.rules, () => ( []))).map((r) => ({ match: sticky(r.match), type: r.type }));
|
|
44
|
-
const strings = [..._nullishCoalesce(def.strings, () => ( []))].sort((a, b) => b.open.length - a.open.length);
|
|
73
|
+
const strings = [..._nullishCoalesce(def.strings, () => ( []))].sort((a, b) => b.open.length - a.open.length).map((s) => ({ open: s.open, close: _nullishCoalesce(s.close, () => ( s.open)), escape: _nullishCoalesce(s.escape, () => ( true)), multiline: _nullishCoalesce(s.multiline, () => ( false)), type: _nullishCoalesce(s.type, () => ( "string")), interpolate: s.interpolate }));
|
|
45
74
|
const ident = sticky(_nullishCoalesce(def.identifier, () => ( IDENT)));
|
|
46
75
|
const calls = _nullishCoalesce(def.calls, () => ( true));
|
|
76
|
+
const numbers = _nullishCoalesce(def.numbers, () => ( true));
|
|
77
|
+
const operators = _nullishCoalesce(def.operators, () => ( true));
|
|
47
78
|
function classify(word, line, end) {
|
|
48
79
|
const k = ci ? word.toLowerCase() : word;
|
|
49
80
|
if (keywords.has(k)) return "keyword";
|
|
@@ -61,38 +92,90 @@ function defineLanguage(def) {
|
|
|
61
92
|
return {
|
|
62
93
|
tokenizeLine(line, state) {
|
|
63
94
|
const out = new LineBuilder();
|
|
64
|
-
|
|
95
|
+
const stack = state ? [...state] : [];
|
|
96
|
+
const top = () => stack[stack.length - 1];
|
|
97
|
+
let heredoc;
|
|
65
98
|
let i = 0;
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
out.add(
|
|
73
|
-
|
|
74
|
-
open = null;
|
|
99
|
+
let m;
|
|
100
|
+
const doc = top();
|
|
101
|
+
if (_optionalChain([doc, 'optionalAccess', _ => _.t]) === "heredoc") {
|
|
102
|
+
if ((doc.dash ? line.replace(/^\t+/, "") : line) === doc.end) {
|
|
103
|
+
out.add("meta", line);
|
|
104
|
+
stack.pop();
|
|
105
|
+
} else out.add("string", line);
|
|
106
|
+
return { tokens: out.tokens, state: stack.length ? stack : null };
|
|
75
107
|
}
|
|
76
108
|
scan: while (i < line.length) {
|
|
109
|
+
const f = top();
|
|
110
|
+
if (_optionalChain([f, 'optionalAccess', _2 => _2.t]) === "comment") {
|
|
111
|
+
const end = line.indexOf(f.close, i);
|
|
112
|
+
if (end < 0) {
|
|
113
|
+
out.add("comment", line.slice(i));
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
out.add("comment", line.slice(i, end + f.close.length));
|
|
117
|
+
i = end + f.close.length;
|
|
118
|
+
stack.pop();
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
if (_optionalChain([f, 'optionalAccess', _3 => _3.t]) === "string") {
|
|
122
|
+
const s = f.s;
|
|
123
|
+
for (let j = i; j < line.length; j++) {
|
|
124
|
+
if (s.interpolate && line.startsWith(s.interpolate[0], j)) {
|
|
125
|
+
if (s.interpolate[0] === "{" && line[j + 1] === "{") {
|
|
126
|
+
j++;
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
out.add(s.type, line.slice(i, j));
|
|
130
|
+
out.add("punctuation", s.interpolate[0]);
|
|
131
|
+
i = j + s.interpolate[0].length;
|
|
132
|
+
const close = s.interpolate[1];
|
|
133
|
+
stack.push({ t: "code", close, open: _nullishCoalesce(PAIRS[close], () => ( "")), depth: 0 });
|
|
134
|
+
continue scan;
|
|
135
|
+
}
|
|
136
|
+
if (s.escape && line[j] === "\\") {
|
|
137
|
+
j++;
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
if (line.startsWith(s.close, j)) {
|
|
141
|
+
out.add(s.type, line.slice(i, j + s.close.length));
|
|
142
|
+
i = j + s.close.length;
|
|
143
|
+
stack.pop();
|
|
144
|
+
continue scan;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
out.add(s.type, line.slice(i));
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
if (_optionalChain([f, 'optionalAccess', _4 => _4.t]) === "code") {
|
|
151
|
+
if (f.depth === 0 && line.startsWith(f.close, i)) {
|
|
152
|
+
out.add("punctuation", f.close);
|
|
153
|
+
i += f.close.length;
|
|
154
|
+
stack.pop();
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
const ch2 = line[i];
|
|
158
|
+
if (ch2 === f.open || f.depth > 0 && ch2 === f.close) {
|
|
159
|
+
stack[stack.length - 1] = { ...f, depth: f.depth + (ch2 === f.open ? 1 : -1) };
|
|
160
|
+
out.add("punctuation", ch2);
|
|
161
|
+
i++;
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
77
165
|
for (const r of rules) {
|
|
78
166
|
r.match.lastIndex = i;
|
|
79
|
-
const
|
|
80
|
-
if (
|
|
81
|
-
out.add(r.type,
|
|
82
|
-
i +=
|
|
167
|
+
const hit = r.match.exec(line);
|
|
168
|
+
if (hit && hit[0]) {
|
|
169
|
+
out.add(r.type, hit[0]);
|
|
170
|
+
i += hit[0].length;
|
|
83
171
|
continue scan;
|
|
84
172
|
}
|
|
85
173
|
}
|
|
86
174
|
for (const [o, c] of _nullishCoalesce(def.blockComment, () => ( []))) {
|
|
87
175
|
if (!line.startsWith(o, i)) continue;
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
open = { close: c, type: "comment", escape: false };
|
|
92
|
-
break scan;
|
|
93
|
-
}
|
|
94
|
-
out.add("comment", line.slice(i, end + c.length));
|
|
95
|
-
i = end + c.length;
|
|
176
|
+
out.add("comment", o);
|
|
177
|
+
i += o.length;
|
|
178
|
+
stack.push({ t: "comment", close: c });
|
|
96
179
|
continue scan;
|
|
97
180
|
}
|
|
98
181
|
for (const p of _nullishCoalesce(def.lineComment, () => ( []))) {
|
|
@@ -103,22 +186,20 @@ function defineLanguage(def) {
|
|
|
103
186
|
}
|
|
104
187
|
for (const s of strings) {
|
|
105
188
|
if (!line.startsWith(s.open, i)) continue;
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
const end = findClose(line, i + s.open.length, close, escape);
|
|
110
|
-
if (end < 0) {
|
|
111
|
-
out.add(type, line.slice(i));
|
|
112
|
-
if (s.multiline) open = { close, type, escape };
|
|
113
|
-
break scan;
|
|
114
|
-
}
|
|
115
|
-
out.add(type, line.slice(i, end));
|
|
116
|
-
i = end;
|
|
189
|
+
out.add(s.type, s.open);
|
|
190
|
+
i += s.open.length;
|
|
191
|
+
stack.push({ t: "string", s });
|
|
117
192
|
continue scan;
|
|
118
193
|
}
|
|
119
|
-
|
|
194
|
+
if (def.heredoc && (m = (HEREDOC.lastIndex = i, HEREDOC.exec(line)))) {
|
|
195
|
+
out.add("operator", `<<${m[1]}`);
|
|
196
|
+
out.add("meta", m[0].slice(2 + m[1].length));
|
|
197
|
+
heredoc = { t: "heredoc", end: m[3], dash: !!m[1] };
|
|
198
|
+
i += m[0].length;
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
120
201
|
NUMBER.lastIndex = i;
|
|
121
|
-
if (m = NUMBER.exec(line)) {
|
|
202
|
+
if (numbers && (m = NUMBER.exec(line))) {
|
|
122
203
|
out.add("number", m[0]);
|
|
123
204
|
i += m[0].length;
|
|
124
205
|
continue;
|
|
@@ -137,7 +218,7 @@ function defineLanguage(def) {
|
|
|
137
218
|
}
|
|
138
219
|
OPERATOR.lastIndex = i;
|
|
139
220
|
if (m = OPERATOR.exec(line)) {
|
|
140
|
-
out.add("operator", m[0]);
|
|
221
|
+
out.add(operators ? "operator" : void 0, m[0]);
|
|
141
222
|
i += m[0].length;
|
|
142
223
|
continue;
|
|
143
224
|
}
|
|
@@ -145,7 +226,10 @@ function defineLanguage(def) {
|
|
|
145
226
|
out.add(PUNCTUATION.includes(ch) ? "punctuation" : void 0, ch);
|
|
146
227
|
i++;
|
|
147
228
|
}
|
|
148
|
-
|
|
229
|
+
const cut = stack.findIndex((fr) => fr.t === "string" && !fr.s.multiline);
|
|
230
|
+
if (cut >= 0) stack.length = cut;
|
|
231
|
+
if (heredoc) stack.push(heredoc);
|
|
232
|
+
return { tokens: out.tokens, state: stack.length ? stack : null };
|
|
149
233
|
}
|
|
150
234
|
};
|
|
151
235
|
}
|
|
@@ -296,7 +380,8 @@ var bash_default = defineLanguage({
|
|
|
296
380
|
{ match: /(?<=\s)--?[A-Za-z][\w-]*/, type: "attr" }
|
|
297
381
|
],
|
|
298
382
|
identifier: /[A-Za-z_][\w-]*/,
|
|
299
|
-
calls: false
|
|
383
|
+
calls: false,
|
|
384
|
+
heredoc: true
|
|
300
385
|
});
|
|
301
386
|
|
|
302
387
|
// src/languages/_common.ts
|
|
@@ -331,12 +416,20 @@ var cpp_default = defineLanguage({
|
|
|
331
416
|
});
|
|
332
417
|
|
|
333
418
|
// src/languages/csharp.ts
|
|
419
|
+
var INTERPOLATE = ["{", "}"];
|
|
334
420
|
var csharp_default = defineLanguage({
|
|
335
421
|
...C_COMMENTS,
|
|
336
|
-
strings: [
|
|
422
|
+
strings: [
|
|
423
|
+
{ open: '$@"', close: '"', escape: false, multiline: true, interpolate: INTERPOLATE },
|
|
424
|
+
{ open: '@$"', close: '"', escape: false, multiline: true, interpolate: INTERPOLATE },
|
|
425
|
+
{ open: '@"', close: '"', escape: false, multiline: true },
|
|
426
|
+
{ open: '$"', close: '"', interpolate: INTERPOLATE },
|
|
427
|
+
...QUOTES
|
|
428
|
+
],
|
|
337
429
|
keywords: "abstract as base break case catch checked class const continue default delegate do else enum event explicit extern finally fixed for foreach goto if implicit in interface internal is lock namespace new operator out override params private protected public readonly ref return sealed sizeof stackalloc static struct switch this throw try typeof unchecked unsafe using virtual volatile while async await var record get set init yield where when",
|
|
338
430
|
types: "bool byte char decimal double float int long object sbyte short string uint ulong ushort void dynamic",
|
|
339
431
|
constants: "true false null",
|
|
432
|
+
rules: [{ match: /(?<=^\s*)\[[A-Z]\w*(?:\([^)]*\))?\]/, type: "meta" }],
|
|
340
433
|
classNames: true
|
|
341
434
|
});
|
|
342
435
|
|
|
@@ -365,6 +458,21 @@ var diff = {
|
|
|
365
458
|
};
|
|
366
459
|
var diff_default = diff;
|
|
367
460
|
|
|
461
|
+
// src/languages/dockerfile.ts
|
|
462
|
+
var INSTRUCTIONS = "FROM|RUN|CMD|LABEL|EXPOSE|ENV|ADD|COPY|ENTRYPOINT|VOLUME|USER|WORKDIR|ARG|ONBUILD|STOPSIGNAL|HEALTHCHECK|SHELL|MAINTAINER";
|
|
463
|
+
var dockerfile_default = defineLanguage({
|
|
464
|
+
strings: [{ open: '"' }, { open: "'", escape: false }],
|
|
465
|
+
rules: [
|
|
466
|
+
{ match: /(?<=^\s*)#.*/, type: "comment" },
|
|
467
|
+
{ match: new RegExp(`(?<=^\\s*)(?:${INSTRUCTIONS})\\b`, "i"), type: "keyword" },
|
|
468
|
+
{ match: /(?<=^\s*FROM\s.*\s)AS(?=\s)/i, type: "keyword" },
|
|
469
|
+
{ match: /\$(?:\{[^}]*\}|[A-Za-z_]\w*)/, type: "variable" },
|
|
470
|
+
{ match: /(?<=\s)--[A-Za-z][\w-]*/, type: "attr" }
|
|
471
|
+
],
|
|
472
|
+
identifier: /[A-Za-z_][\w.-]*/,
|
|
473
|
+
calls: false
|
|
474
|
+
});
|
|
475
|
+
|
|
368
476
|
// src/languages/go.ts
|
|
369
477
|
var go_default = defineLanguage({
|
|
370
478
|
...C_COMMENTS,
|
|
@@ -375,6 +483,38 @@ var go_default = defineLanguage({
|
|
|
375
483
|
builtins: "append cap close copy delete len make new panic print println recover"
|
|
376
484
|
});
|
|
377
485
|
|
|
486
|
+
// src/languages/graphql.ts
|
|
487
|
+
var graphql_default = defineLanguage({
|
|
488
|
+
lineComment: ["#"],
|
|
489
|
+
strings: [{ open: '"""', multiline: true }, { open: '"' }],
|
|
490
|
+
keywords: "query mutation subscription fragment on type interface union enum input scalar schema extend directive implements repeatable",
|
|
491
|
+
constants: "true false null",
|
|
492
|
+
types: "Int Float String Boolean ID",
|
|
493
|
+
rules: [
|
|
494
|
+
{ match: /\$[A-Za-z_]\w*/, type: "variable" },
|
|
495
|
+
{ match: /@[A-Za-z_]\w*/, type: "meta" },
|
|
496
|
+
{ match: /\.\.\./, type: "operator" },
|
|
497
|
+
{ match: /[A-Za-z_]\w*(?=\s*:)/, type: "property" }
|
|
498
|
+
],
|
|
499
|
+
classNames: true
|
|
500
|
+
});
|
|
501
|
+
|
|
502
|
+
// src/languages/hcl.ts
|
|
503
|
+
var hcl_default = defineLanguage({
|
|
504
|
+
lineComment: ["#", "//"],
|
|
505
|
+
blockComment: [["/*", "*/"]],
|
|
506
|
+
strings: [{ open: '"', interpolate: ["${", "}"] }],
|
|
507
|
+
constants: "true false null",
|
|
508
|
+
types: "string number bool list map set object tuple any",
|
|
509
|
+
rules: [
|
|
510
|
+
{ match: /(?<=^\s*)(?:resource|data|variable|output|module|provider|locals|terraform|moved|import|check)\b/, type: "keyword" },
|
|
511
|
+
{ match: /\b(?:for|in|if|for_each|count|depends_on|lifecycle|dynamic|content)\b(?!\s*=)/, type: "keyword" },
|
|
512
|
+
{ match: /(?<=^\s*)[A-Za-z_][\w-]*(?=\s*=(?!=))/, type: "property" }
|
|
513
|
+
],
|
|
514
|
+
identifier: /[A-Za-z_][\w-]*/,
|
|
515
|
+
heredoc: true
|
|
516
|
+
});
|
|
517
|
+
|
|
378
518
|
// src/languages/java.ts
|
|
379
519
|
var java_default = defineLanguage({
|
|
380
520
|
...C_COMMENTS,
|
|
@@ -389,14 +529,19 @@ var java_default = defineLanguage({
|
|
|
389
529
|
// src/languages/javascript.ts
|
|
390
530
|
var JS_KEYWORDS = "break case catch class const continue debugger default delete do else export extends finally for function if import in instanceof let new return super switch this throw try typeof var void while with yield async await of static get set from as";
|
|
391
531
|
var JS_CONSTANTS = "true false null undefined NaN Infinity";
|
|
532
|
+
var TEMPLATE = { open: "`", multiline: true, interpolate: ["${", "}"] };
|
|
392
533
|
var JSX_TAG = { match: /(?<=(?:^|[(=?:,&|{}[>;]|\breturn)\s*)<\/?[A-Za-z][\w.:-]*(?=[\s/>]|$)/, type: "tag" };
|
|
534
|
+
var REGEX = {
|
|
535
|
+
match: /(?<=(?:^|[(,=:[!&|?{;]|\breturn|\btypeof|\bcase)\s*)\/(?![*/\s>])(?:\\.|\[(?:\\.|[^\]\\])*\]|[^\\/[])+\/[dgimsuvy]*/,
|
|
536
|
+
type: "regex"
|
|
537
|
+
};
|
|
393
538
|
var DECORATOR = { match: /@[A-Za-z_$][\w$.]*/, type: "meta" };
|
|
394
539
|
var javascript_default = defineLanguage({
|
|
395
540
|
...C_COMMENTS,
|
|
396
|
-
strings: [...QUOTES,
|
|
541
|
+
strings: [...QUOTES, TEMPLATE],
|
|
397
542
|
keywords: JS_KEYWORDS,
|
|
398
543
|
constants: JS_CONSTANTS,
|
|
399
|
-
rules: [JSX_TAG, DECORATOR],
|
|
544
|
+
rules: [JSX_TAG, REGEX, DECORATOR],
|
|
400
545
|
classNames: true
|
|
401
546
|
});
|
|
402
547
|
|
|
@@ -409,6 +554,44 @@ var json_default = defineLanguage({
|
|
|
409
554
|
calls: false
|
|
410
555
|
});
|
|
411
556
|
|
|
557
|
+
// src/languages/kotlin.ts
|
|
558
|
+
var INTERPOLATE2 = ["${", "}"];
|
|
559
|
+
var kotlin_default = defineLanguage({
|
|
560
|
+
...C_COMMENTS,
|
|
561
|
+
strings: [
|
|
562
|
+
{ open: '"""', multiline: true, escape: false, interpolate: INTERPOLATE2 },
|
|
563
|
+
{ open: '"', interpolate: INTERPOLATE2 },
|
|
564
|
+
{ open: "'" }
|
|
565
|
+
],
|
|
566
|
+
keywords: "package import class interface object fun val var if else when for while do return break continue throw try catch finally is in as typealias this super data sealed enum open abstract override private protected public internal companion lateinit suspend inline reified by init get set where out vararg const operator infix annotation",
|
|
567
|
+
constants: "true false null",
|
|
568
|
+
types: "Int Long Short Byte Double Float Boolean Char String Unit Any Nothing List Map Set",
|
|
569
|
+
// Annotations (`@Serializable`) and labels (`return@forEach`).
|
|
570
|
+
rules: [{ match: /@[A-Za-z_]\w*(?::\w+)?/, type: "meta" }],
|
|
571
|
+
classNames: true
|
|
572
|
+
});
|
|
573
|
+
|
|
574
|
+
// src/languages/markdown.ts
|
|
575
|
+
var markdown_default = defineLanguage({
|
|
576
|
+
blockComment: [["<!--", "-->"]],
|
|
577
|
+
rules: [
|
|
578
|
+
{ match: /^#{1,6}\s.*/, type: "keyword" },
|
|
579
|
+
{ match: /^ {0,3}(?:`{3,}|~{3,}).*/, type: "meta" },
|
|
580
|
+
{ match: /^\s*>.*/, type: "comment" },
|
|
581
|
+
{ match: /^\s*(?:[-*+]|\d+[.)])(?=\s)/, type: "punctuation" },
|
|
582
|
+
{ match: /^\s*(?:-{3,}|\*{3,}|_{3,})\s*$/, type: "punctuation" },
|
|
583
|
+
{ match: /`[^`]+`/, type: "string" },
|
|
584
|
+
{ match: /!?\[[^\]]*\]\([^)\s]*(?:\s+"[^"]*")?\)/, type: "function" },
|
|
585
|
+
{ match: /\*\*[^*]+\*\*|__[^_]+__/, type: "type" },
|
|
586
|
+
{ match: /(?<![\w*])\*[^*\s][^*]*\*(?!\*)|(?<!\w)_[^_\s][^_]*_(?!\w)/, type: "attr" },
|
|
587
|
+
{ match: /<\/?[A-Za-z][^>]*>/, type: "tag" }
|
|
588
|
+
],
|
|
589
|
+
identifier: /[A-Za-z_][\w'-]*/,
|
|
590
|
+
calls: false,
|
|
591
|
+
numbers: false,
|
|
592
|
+
operators: false
|
|
593
|
+
});
|
|
594
|
+
|
|
412
595
|
// src/languages/php.ts
|
|
413
596
|
var php_default = defineLanguage({
|
|
414
597
|
lineComment: ["//", "#"],
|
|
@@ -424,17 +607,51 @@ var php_default = defineLanguage({
|
|
|
424
607
|
classNames: true
|
|
425
608
|
});
|
|
426
609
|
|
|
610
|
+
// src/languages/powershell.ts
|
|
611
|
+
var powershell_default = defineLanguage({
|
|
612
|
+
lineComment: ["#"],
|
|
613
|
+
blockComment: [["<#", "#>"]],
|
|
614
|
+
strings: [
|
|
615
|
+
// Double-quoted strings run the code inside `$(…)`.
|
|
616
|
+
{ open: '@"', close: '"@', escape: false, multiline: true, interpolate: ["$(", ")"] },
|
|
617
|
+
{ open: "@'", close: "'@", escape: false, multiline: true },
|
|
618
|
+
{ open: '"', escape: false, multiline: true, interpolate: ["$(", ")"] },
|
|
619
|
+
{ open: "'", escape: false, multiline: true }
|
|
620
|
+
],
|
|
621
|
+
keywords: "if else elseif switch foreach for while do until break continue return function filter param begin process end try catch finally throw trap in exit class enum using",
|
|
622
|
+
caseInsensitive: true,
|
|
623
|
+
rules: [
|
|
624
|
+
{ match: /\$(?:true|false|null)\b/i, type: "constant" },
|
|
625
|
+
{ match: /\$(?:\{[^}]+\}|[\w:]+)/, type: "variable" },
|
|
626
|
+
{ match: /-(?:eq|ne|gt|ge|lt|le|like|notlike|match|notmatch|contains|notcontains|in|notin|and|or|not|xor|replace|split|join|is|isnot|as)\b/i, type: "operator" },
|
|
627
|
+
{ match: /\[[A-Za-z][\w.]*(?:\[\])?\]/, type: "type" },
|
|
628
|
+
{ match: /\b[A-Z][a-z]+-[A-Z]\w+/, type: "function" },
|
|
629
|
+
{ match: /(?<=\s)-[A-Za-z]\w*/, type: "attr" }
|
|
630
|
+
],
|
|
631
|
+
identifier: /[A-Za-z_][\w-]*/
|
|
632
|
+
});
|
|
633
|
+
|
|
427
634
|
// src/languages/python.ts
|
|
635
|
+
var F_STRINGS = ["f", "F", "rf", "rF", "Rf", "RF", "fr", "fR", "Fr", "FR"].flatMap(
|
|
636
|
+
(prefix) => ['"""', "'''", '"', "'"].map((quote) => ({
|
|
637
|
+
open: prefix + quote,
|
|
638
|
+
close: quote,
|
|
639
|
+
escape: !/r/i.test(prefix),
|
|
640
|
+
multiline: quote.length === 3,
|
|
641
|
+
interpolate: ["{", "}"]
|
|
642
|
+
}))
|
|
643
|
+
);
|
|
428
644
|
var python_default = defineLanguage({
|
|
429
645
|
lineComment: ["#"],
|
|
430
|
-
strings: [{ open: '"""', multiline: true }, { open: "'''", multiline: true }, ...QUOTES],
|
|
646
|
+
strings: [...F_STRINGS, { open: '"""', multiline: true }, { open: "'''", multiline: true }, ...QUOTES],
|
|
431
647
|
keywords: "and as assert async await break class continue def del elif else except finally for from global if import in is lambda nonlocal not or pass raise return try while with yield match case",
|
|
432
648
|
constants: "True False None",
|
|
433
649
|
variables: "self cls",
|
|
434
650
|
types: "int float str bool list dict set tuple bytes object Exception",
|
|
435
651
|
rules: [
|
|
436
652
|
{ match: /(?<=^\s*)@[\w.]+/, type: "meta" },
|
|
437
|
-
|
|
653
|
+
// r"…", b"…", u"…" prefixes (f-strings are handled above).
|
|
654
|
+
{ match: /[rRbBuU]{1,2}(?=["'])/, type: "string" }
|
|
438
655
|
],
|
|
439
656
|
classNames: true
|
|
440
657
|
});
|
|
@@ -484,16 +701,53 @@ var sql_default = defineLanguage({
|
|
|
484
701
|
caseInsensitive: true
|
|
485
702
|
});
|
|
486
703
|
|
|
704
|
+
// src/languages/swift.ts
|
|
705
|
+
var INTERPOLATE3 = ["\\(", ")"];
|
|
706
|
+
var swift_default = defineLanguage({
|
|
707
|
+
...C_COMMENTS,
|
|
708
|
+
strings: [
|
|
709
|
+
{ open: '"""', multiline: true, interpolate: INTERPOLATE3 },
|
|
710
|
+
{ open: '"', interpolate: INTERPOLATE3 }
|
|
711
|
+
],
|
|
712
|
+
keywords: "import let var func class struct enum protocol extension if else guard switch case default for in while repeat return break continue fallthrough throw throws rethrows try catch do defer as is init deinit self Self super static private fileprivate internal public open final override mutating nonmutating weak unowned lazy inout where async await some any typealias associatedtype subscript convenience required",
|
|
713
|
+
constants: "true false nil",
|
|
714
|
+
types: "Int Double Float String Bool Character Array Dictionary Set Void Optional Result Error",
|
|
715
|
+
rules: [
|
|
716
|
+
{ match: /@[A-Za-z_]\w*/, type: "meta" },
|
|
717
|
+
{ match: /#(?:available|if|else|elseif|endif|selector|keyPath|warning|error)\b/, type: "meta" }
|
|
718
|
+
],
|
|
719
|
+
classNames: true
|
|
720
|
+
});
|
|
721
|
+
|
|
722
|
+
// src/languages/toml.ts
|
|
723
|
+
var toml_default = defineLanguage({
|
|
724
|
+
lineComment: ["#"],
|
|
725
|
+
strings: [
|
|
726
|
+
{ open: '"""', multiline: true },
|
|
727
|
+
{ open: "'''", multiline: true, escape: false },
|
|
728
|
+
{ open: '"' },
|
|
729
|
+
{ open: "'", escape: false }
|
|
730
|
+
],
|
|
731
|
+
constants: "true false inf nan",
|
|
732
|
+
rules: [
|
|
733
|
+
{ match: /(?<=^\s*)\[\[?[^\]]*\]\]?/, type: "tag" },
|
|
734
|
+
{ match: /(?<=^\s*)[\w.\-"' ]+?(?=\s*=)/, type: "property" },
|
|
735
|
+
{ match: /\d{4}-\d{2}-\d{2}(?:[T ]\d{2}:\d{2}(?::\d{2}(?:\.\d+)?)?(?:Z|[+-]\d{2}:\d{2})?)?/, type: "number" }
|
|
736
|
+
],
|
|
737
|
+
identifier: /[A-Za-z_][\w-]*/,
|
|
738
|
+
calls: false
|
|
739
|
+
});
|
|
740
|
+
|
|
487
741
|
// src/languages/typescript.ts
|
|
488
742
|
var TS_KEYWORDS = " interface type enum implements namespace declare abstract readonly private protected public keyof infer is satisfies asserts module override unique";
|
|
489
743
|
var ts = (jsx) => ({
|
|
490
744
|
...C_COMMENTS,
|
|
491
|
-
strings: [...QUOTES,
|
|
745
|
+
strings: [...QUOTES, TEMPLATE],
|
|
492
746
|
keywords: JS_KEYWORDS + TS_KEYWORDS,
|
|
493
747
|
types: "string number boolean any unknown never void object symbol bigint",
|
|
494
748
|
constants: JS_CONSTANTS,
|
|
495
749
|
// Plain TypeScript has no JSX, so `Map<string, number>` is never mistaken for a tag.
|
|
496
|
-
rules: jsx ? [JSX_TAG, DECORATOR] : [DECORATOR],
|
|
750
|
+
rules: jsx ? [JSX_TAG, REGEX, DECORATOR] : [REGEX, DECORATOR],
|
|
497
751
|
classNames: true
|
|
498
752
|
});
|
|
499
753
|
var typescript_default = defineLanguage(ts(false));
|
|
@@ -539,6 +793,14 @@ var LANGUAGES = {
|
|
|
539
793
|
yaml: yaml_default,
|
|
540
794
|
xml: xml_default,
|
|
541
795
|
diff: diff_default,
|
|
796
|
+
kotlin: kotlin_default,
|
|
797
|
+
swift: swift_default,
|
|
798
|
+
dockerfile: dockerfile_default,
|
|
799
|
+
toml: toml_default,
|
|
800
|
+
markdown: markdown_default,
|
|
801
|
+
graphql: graphql_default,
|
|
802
|
+
powershell: powershell_default,
|
|
803
|
+
hcl: hcl_default,
|
|
542
804
|
plaintext: plain
|
|
543
805
|
};
|
|
544
806
|
|
|
@@ -588,6 +850,20 @@ var aliases = new Map(
|
|
|
588
850
|
vue: "xml",
|
|
589
851
|
svelte: "xml",
|
|
590
852
|
patch: "diff",
|
|
853
|
+
kt: "kotlin",
|
|
854
|
+
kts: "kotlin",
|
|
855
|
+
docker: "dockerfile",
|
|
856
|
+
containerfile: "dockerfile",
|
|
857
|
+
md: "markdown",
|
|
858
|
+
mdx: "markdown",
|
|
859
|
+
gql: "graphql",
|
|
860
|
+
ps1: "powershell",
|
|
861
|
+
pwsh: "powershell",
|
|
862
|
+
ps: "powershell",
|
|
863
|
+
terraform: "hcl",
|
|
864
|
+
tf: "hcl",
|
|
865
|
+
tfvars: "hcl",
|
|
866
|
+
opentofu: "hcl",
|
|
591
867
|
text: "plaintext",
|
|
592
868
|
txt: "plaintext",
|
|
593
869
|
plain: "plaintext",
|
|
@@ -606,6 +882,29 @@ function listLanguages() {
|
|
|
606
882
|
return [...registry.keys()];
|
|
607
883
|
}
|
|
608
884
|
|
|
885
|
+
// src/lib/lines.ts
|
|
886
|
+
function parseLines(spec) {
|
|
887
|
+
const out = /* @__PURE__ */ new Set();
|
|
888
|
+
if (spec == null) return out;
|
|
889
|
+
if (typeof spec === "number") return out.add(spec);
|
|
890
|
+
if (Array.isArray(spec)) return new Set(spec);
|
|
891
|
+
for (const part of spec.split(/[\s,]+/)) {
|
|
892
|
+
const m = /^(\d+)(?:-(\d+))?$/.exec(part);
|
|
893
|
+
if (!m) continue;
|
|
894
|
+
const from = Number(m[1]);
|
|
895
|
+
const to = Number(_nullishCoalesce(m[2], () => ( m[1])));
|
|
896
|
+
for (let n = Math.min(from, to); n <= Math.max(from, to) && n - from < 1e5; n++) out.add(n);
|
|
897
|
+
}
|
|
898
|
+
return out;
|
|
899
|
+
}
|
|
900
|
+
var wantsLines = (o) => !!o && (!!o.lineNumbers || o.highlightLines != null);
|
|
901
|
+
function lineClass(n, numbers, marked) {
|
|
902
|
+
return "cl-line" + (numbers ? " cl-ln" : "") + (marked.has(n) ? " cl-mark" : "");
|
|
903
|
+
}
|
|
904
|
+
function wrapLine(html, n, numbers, marked) {
|
|
905
|
+
return `<span class="${lineClass(n, numbers, marked)}" data-line="${n}">${html}</span>`;
|
|
906
|
+
}
|
|
907
|
+
|
|
609
908
|
// src/lib/stream.ts
|
|
610
909
|
var resolve = (lang) => typeof lang === "object" ? lang : _nullishCoalesce(getLanguage(lang), () => ( plain));
|
|
611
910
|
var ESCAPES = { "&": "&", "<": "<", ">": ">", '"': """ };
|
|
@@ -626,8 +925,11 @@ function tokenize(code, lang) {
|
|
|
626
925
|
return r.tokens;
|
|
627
926
|
});
|
|
628
927
|
}
|
|
629
|
-
function highlight(code, lang) {
|
|
630
|
-
|
|
928
|
+
function highlight(code, lang, options) {
|
|
929
|
+
const lines = tokenize(code, lang).map(renderLine);
|
|
930
|
+
if (!wantsLines(options)) return lines.join("\n");
|
|
931
|
+
const marked = parseLines(options.highlightLines);
|
|
932
|
+
return lines.map((html, i) => wrapLine(html, i + 1, !!options.lineNumbers, marked)).join("\n");
|
|
631
933
|
}
|
|
632
934
|
function createStream(lang) {
|
|
633
935
|
const language = resolve(lang);
|
|
@@ -657,17 +959,21 @@ function createStream(lang) {
|
|
|
657
959
|
// src/lib/dom.ts
|
|
658
960
|
function mount(target, options = {}) {
|
|
659
961
|
const doc = target.ownerDocument;
|
|
962
|
+
const numbers = !!options.lineNumbers;
|
|
963
|
+
const marked = parseLines(options.highlightLines);
|
|
660
964
|
let lang = options.lang;
|
|
661
965
|
let stream;
|
|
662
966
|
let tail;
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
el.className =
|
|
666
|
-
|
|
967
|
+
let done = 0;
|
|
968
|
+
const number = (el, n) => {
|
|
969
|
+
el.className = lineClass(n, numbers, marked);
|
|
970
|
+
if (numbers || marked.size) el.setAttribute("data-line", String(n));
|
|
667
971
|
};
|
|
668
972
|
const start = () => {
|
|
669
973
|
stream = createStream(lang);
|
|
670
|
-
|
|
974
|
+
done = 0;
|
|
975
|
+
tail = doc.createElement("span");
|
|
976
|
+
number(tail, 1);
|
|
671
977
|
target.replaceChildren(tail);
|
|
672
978
|
};
|
|
673
979
|
target.classList.add("cl");
|
|
@@ -683,13 +989,15 @@ function mount(target, options = {}) {
|
|
|
683
989
|
if (!chunk) return;
|
|
684
990
|
const { lines, tail: last } = stream.push(chunk);
|
|
685
991
|
if (lines.length) {
|
|
686
|
-
const
|
|
992
|
+
const finished = doc.createDocumentFragment();
|
|
687
993
|
for (const line of lines) {
|
|
688
|
-
const el =
|
|
994
|
+
const el = doc.createElement("span");
|
|
995
|
+
number(el, ++done);
|
|
689
996
|
el.innerHTML = renderLine(line);
|
|
690
|
-
|
|
997
|
+
finished.append(el, "\n");
|
|
691
998
|
}
|
|
692
|
-
target.insertBefore(
|
|
999
|
+
target.insertBefore(finished, tail);
|
|
1000
|
+
number(tail, done + 1);
|
|
693
1001
|
}
|
|
694
1002
|
tail.innerHTML = renderLine(last);
|
|
695
1003
|
},
|
|
@@ -720,4 +1028,6 @@ function mount(target, options = {}) {
|
|
|
720
1028
|
|
|
721
1029
|
|
|
722
1030
|
|
|
723
|
-
|
|
1031
|
+
|
|
1032
|
+
|
|
1033
|
+
exports.copyText = copyText; exports.addCopyButton = addCopyButton; exports.defineLanguage = defineLanguage; exports.createMarkup = createMarkup; exports.getLanguage = getLanguage; exports.registerLanguage = registerLanguage; exports.listLanguages = listLanguages; exports.escapeHtml = escapeHtml; exports.renderLine = renderLine; exports.tokenize = tokenize; exports.highlight = highlight; exports.createStream = createStream; exports.mount = mount;
|