codelight.js 1.2.0 → 1.3.1
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 +18 -0
- package/README.CDN.md +7 -7
- package/README.md +34 -12
- 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-63RCMYG4.js → chunk-XG43K62Y.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-KNCCWXKW.cjs → chunk-KZATGNQA.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 +8 -3
package/lib/codelight.cjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
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.
|
|
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.1 | MIT License | https://github.com/codelightjs/codelight.js */
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
|
|
@@ -11,22 +11,30 @@
|
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
|
|
15
|
+
var _chunkKZATGNQAcjs = require('./chunk-KZATGNQA.cjs');
|
|
15
16
|
|
|
16
17
|
// src/lib/auto.ts
|
|
17
18
|
var MARKDOWN_CLASS = /(?:^|\s)language-(\S+)/;
|
|
19
|
+
var attr = (code, name) => _nullishCoalesce(_nullishCoalesce(code.getAttribute(name), () => ( _optionalChain([code, 'access', _ => _.parentElement, 'optionalAccess', _2 => _2.getAttribute, 'call', _3 => _3(name)]))), () => ( null));
|
|
18
20
|
function languageOf(code) {
|
|
19
|
-
return _nullishCoalesce(_nullishCoalesce(
|
|
21
|
+
return _nullishCoalesce(_nullishCoalesce(attr(code, "data-lang"), () => ( _optionalChain([MARKDOWN_CLASS, 'access', _4 => _4.exec, 'call', _5 => _5(code.className), 'optionalAccess', _6 => _6[1]]))), () => ( ""));
|
|
20
22
|
}
|
|
21
|
-
function highlightElement(code, lang = languageOf(code)) {
|
|
22
|
-
|
|
23
|
+
function highlightElement(code, lang = languageOf(code), options = {}) {
|
|
24
|
+
const lineNumbers = _nullishCoalesce(options.lineNumbers, () => ( attr(code, "data-line-numbers") != null));
|
|
25
|
+
const highlightLines = _nullishCoalesce(_nullishCoalesce(options.highlightLines, () => ( attr(code, "data-highlight-lines"))), () => ( void 0));
|
|
26
|
+
code.innerHTML = _chunkKZATGNQAcjs.highlight.call(void 0, _nullishCoalesce(code.textContent, () => ( "")), lang, { lineNumbers, highlightLines });
|
|
23
27
|
code.classList.add("cl");
|
|
24
28
|
code.setAttribute("data-cl", lang);
|
|
29
|
+
if (options.copyButton) _chunkKZATGNQAcjs.addCopyButton.call(void 0, code);
|
|
25
30
|
}
|
|
26
|
-
function highlightAll(
|
|
31
|
+
function highlightAll(rootOrOptions, maybeOptions = {}) {
|
|
32
|
+
const isRoot = !!rootOrOptions && typeof rootOrOptions.querySelectorAll === "function";
|
|
33
|
+
const root = isRoot ? rootOrOptions : void 0;
|
|
34
|
+
const options = isRoot ? maybeOptions : { ...rootOrOptions, ...maybeOptions };
|
|
27
35
|
if (!root && typeof document === "undefined") return;
|
|
28
36
|
const run = () => {
|
|
29
|
-
for (const code of (_nullishCoalesce(root, () => ( document))).querySelectorAll("pre > code:not([data-cl])")) highlightElement(code);
|
|
37
|
+
for (const code of (_nullishCoalesce(root, () => ( document))).querySelectorAll("pre > code:not([data-cl])")) highlightElement(code, void 0, options);
|
|
30
38
|
};
|
|
31
39
|
if (!root && document.readyState === "loading") document.addEventListener("DOMContentLoaded", run, { once: true });
|
|
32
40
|
else run();
|
|
@@ -83,7 +91,182 @@ function splitFences(markdown) {
|
|
|
83
91
|
}
|
|
84
92
|
|
|
85
93
|
// src/styles/default.css
|
|
86
|
-
var default_default =
|
|
94
|
+
var default_default = `/*!
|
|
95
|
+
codelight.js theme: default
|
|
96
|
+
Light or dark to match the system. To force one, put a cl-light or cl-dark class
|
|
97
|
+
(or light / dark, or data-theme="light" / "dark") on the code or any parent.
|
|
98
|
+
*/
|
|
99
|
+
.cl, .cl-copy {
|
|
100
|
+
--cl-fg: #1e2430;
|
|
101
|
+
--cl-bg: #f7f7f9;
|
|
102
|
+
--cl-keyword: #b4235a;
|
|
103
|
+
--cl-string: #1d6b45;
|
|
104
|
+
--cl-regex: #0b6477;
|
|
105
|
+
--cl-comment: #656c79;
|
|
106
|
+
--cl-number: #a35200;
|
|
107
|
+
--cl-function: #3a52c4;
|
|
108
|
+
--cl-type: #8a3ab9;
|
|
109
|
+
--cl-constant: #a35200;
|
|
110
|
+
--cl-variable: #9a3f0c;
|
|
111
|
+
--cl-property: #1f6f9a;
|
|
112
|
+
--cl-operator: #b4235a;
|
|
113
|
+
--cl-punctuation: #4b5563;
|
|
114
|
+
--cl-tag: #2459a8;
|
|
115
|
+
--cl-attr: #8a3ab9;
|
|
116
|
+
--cl-meta: #6b4fbb;
|
|
117
|
+
--cl-inserted: #166534;
|
|
118
|
+
--cl-inserted-bg: #dcfce7;
|
|
119
|
+
--cl-deleted: #9f1239;
|
|
120
|
+
--cl-deleted-bg: #ffe4e6;
|
|
121
|
+
--cl-line-number: #656c79;
|
|
122
|
+
--cl-mark-bg: #fdf6e1;
|
|
123
|
+
--cl-border: #d5d9e0;
|
|
124
|
+
}
|
|
125
|
+
.cl {
|
|
126
|
+
color: var(--cl-fg);
|
|
127
|
+
background: var(--cl-bg);
|
|
128
|
+
}
|
|
129
|
+
@media (prefers-color-scheme: dark) {
|
|
130
|
+
.cl, .cl-copy {
|
|
131
|
+
--cl-fg: #e4e7ee;
|
|
132
|
+
--cl-bg: #11141a;
|
|
133
|
+
--cl-keyword: #ff7aa8;
|
|
134
|
+
--cl-string: #8fdba8;
|
|
135
|
+
--cl-regex: #67e3f2;
|
|
136
|
+
--cl-comment: #8a93a3;
|
|
137
|
+
--cl-number: #ffb86b;
|
|
138
|
+
--cl-function: #8fb4ff;
|
|
139
|
+
--cl-type: #d0a3ff;
|
|
140
|
+
--cl-constant: #ffb86b;
|
|
141
|
+
--cl-variable: #ffa070;
|
|
142
|
+
--cl-property: #7fd0f0;
|
|
143
|
+
--cl-operator: #ff7aa8;
|
|
144
|
+
--cl-punctuation: #aab1bd;
|
|
145
|
+
--cl-tag: #8fb4ff;
|
|
146
|
+
--cl-attr: #d0a3ff;
|
|
147
|
+
--cl-meta: #b49cff;
|
|
148
|
+
--cl-inserted: #a7f3c0;
|
|
149
|
+
--cl-inserted-bg: #0f3a24;
|
|
150
|
+
--cl-deleted: #fecdd3;
|
|
151
|
+
--cl-deleted-bg: #4c1020;
|
|
152
|
+
--cl-line-number: #8a93a3;
|
|
153
|
+
--cl-mark-bg: #262116;
|
|
154
|
+
--cl-border: #2e3440;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
.cl-dark .cl, .cl.cl-dark, .dark .cl, [data-theme="dark"] .cl, .cl-dark .cl-copy, .dark .cl-copy, [data-theme="dark"] .cl-copy {
|
|
158
|
+
--cl-fg: #e4e7ee;
|
|
159
|
+
--cl-bg: #11141a;
|
|
160
|
+
--cl-keyword: #ff7aa8;
|
|
161
|
+
--cl-string: #8fdba8;
|
|
162
|
+
--cl-regex: #67e3f2;
|
|
163
|
+
--cl-comment: #8a93a3;
|
|
164
|
+
--cl-number: #ffb86b;
|
|
165
|
+
--cl-function: #8fb4ff;
|
|
166
|
+
--cl-type: #d0a3ff;
|
|
167
|
+
--cl-constant: #ffb86b;
|
|
168
|
+
--cl-variable: #ffa070;
|
|
169
|
+
--cl-property: #7fd0f0;
|
|
170
|
+
--cl-operator: #ff7aa8;
|
|
171
|
+
--cl-punctuation: #aab1bd;
|
|
172
|
+
--cl-tag: #8fb4ff;
|
|
173
|
+
--cl-attr: #d0a3ff;
|
|
174
|
+
--cl-meta: #b49cff;
|
|
175
|
+
--cl-inserted: #a7f3c0;
|
|
176
|
+
--cl-inserted-bg: #0f3a24;
|
|
177
|
+
--cl-deleted: #fecdd3;
|
|
178
|
+
--cl-deleted-bg: #4c1020;
|
|
179
|
+
--cl-line-number: #8a93a3;
|
|
180
|
+
--cl-mark-bg: #262116;
|
|
181
|
+
--cl-border: #2e3440;
|
|
182
|
+
}
|
|
183
|
+
.cl-light .cl, .cl.cl-light, .light .cl, [data-theme="light"] .cl, .cl-light .cl-copy, .light .cl-copy, [data-theme="light"] .cl-copy {
|
|
184
|
+
--cl-fg: #1e2430;
|
|
185
|
+
--cl-bg: #f7f7f9;
|
|
186
|
+
--cl-keyword: #b4235a;
|
|
187
|
+
--cl-string: #1d6b45;
|
|
188
|
+
--cl-regex: #0b6477;
|
|
189
|
+
--cl-comment: #656c79;
|
|
190
|
+
--cl-number: #a35200;
|
|
191
|
+
--cl-function: #3a52c4;
|
|
192
|
+
--cl-type: #8a3ab9;
|
|
193
|
+
--cl-constant: #a35200;
|
|
194
|
+
--cl-variable: #9a3f0c;
|
|
195
|
+
--cl-property: #1f6f9a;
|
|
196
|
+
--cl-operator: #b4235a;
|
|
197
|
+
--cl-punctuation: #4b5563;
|
|
198
|
+
--cl-tag: #2459a8;
|
|
199
|
+
--cl-attr: #8a3ab9;
|
|
200
|
+
--cl-meta: #6b4fbb;
|
|
201
|
+
--cl-inserted: #166534;
|
|
202
|
+
--cl-inserted-bg: #dcfce7;
|
|
203
|
+
--cl-deleted: #9f1239;
|
|
204
|
+
--cl-deleted-bg: #ffe4e6;
|
|
205
|
+
--cl-line-number: #656c79;
|
|
206
|
+
--cl-mark-bg: #fdf6e1;
|
|
207
|
+
--cl-border: #d5d9e0;
|
|
208
|
+
}
|
|
209
|
+
pre > code.cl {
|
|
210
|
+
display: block;
|
|
211
|
+
padding: 1rem 1.25rem;
|
|
212
|
+
overflow: auto;
|
|
213
|
+
}
|
|
214
|
+
.cl-keyword { color: var(--cl-keyword); }
|
|
215
|
+
.cl-string { color: var(--cl-string); }
|
|
216
|
+
.cl-regex { color: var(--cl-regex); }
|
|
217
|
+
.cl-comment { color: var(--cl-comment); }
|
|
218
|
+
.cl-number { color: var(--cl-number); }
|
|
219
|
+
.cl-function { color: var(--cl-function); }
|
|
220
|
+
.cl-type { color: var(--cl-type); }
|
|
221
|
+
.cl-constant { color: var(--cl-constant); }
|
|
222
|
+
.cl-variable { color: var(--cl-variable); }
|
|
223
|
+
.cl-property { color: var(--cl-property); }
|
|
224
|
+
.cl-operator { color: var(--cl-operator); }
|
|
225
|
+
.cl-punctuation { color: var(--cl-punctuation); }
|
|
226
|
+
.cl-tag { color: var(--cl-tag); }
|
|
227
|
+
.cl-attr { color: var(--cl-attr); }
|
|
228
|
+
.cl-meta { color: var(--cl-meta); }
|
|
229
|
+
.cl-comment { font-style: italic; }
|
|
230
|
+
.cl-inserted { color: var(--cl-inserted); background: var(--cl-inserted-bg); }
|
|
231
|
+
.cl-deleted { color: var(--cl-deleted); background: var(--cl-deleted-bg); }
|
|
232
|
+
|
|
233
|
+
/* Line numbers: from data-line, so they're never copied with the code. */
|
|
234
|
+
.cl-ln::before {
|
|
235
|
+
content: attr(data-line);
|
|
236
|
+
display: inline-block;
|
|
237
|
+
min-width: var(--cl-gutter, 2.5ch);
|
|
238
|
+
margin-right: 1.25em;
|
|
239
|
+
text-align: right;
|
|
240
|
+
color: var(--cl-line-number);
|
|
241
|
+
font-style: normal;
|
|
242
|
+
user-select: none;
|
|
243
|
+
-webkit-user-select: none;
|
|
244
|
+
}
|
|
245
|
+
/* Highlighted lines fill the width of the block. */
|
|
246
|
+
.cl-mark {
|
|
247
|
+
display: inline-block;
|
|
248
|
+
min-width: 100%;
|
|
249
|
+
min-height: 1lh;
|
|
250
|
+
background: var(--cl-mark-bg);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/* Copy button */
|
|
254
|
+
.cl-has-copy { position: relative; }
|
|
255
|
+
.cl-copy {
|
|
256
|
+
position: absolute;
|
|
257
|
+
top: 0.5rem;
|
|
258
|
+
right: 0.5rem;
|
|
259
|
+
padding: 0.2rem 0.65rem;
|
|
260
|
+
font: 500 12px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
|
|
261
|
+
color: var(--cl-fg);
|
|
262
|
+
background: var(--cl-bg);
|
|
263
|
+
border: 1px solid var(--cl-border);
|
|
264
|
+
border-radius: 6px;
|
|
265
|
+
cursor: pointer;
|
|
266
|
+
opacity: 0.8;
|
|
267
|
+
}
|
|
268
|
+
.cl-copy:hover, .cl-copy:focus-visible { opacity: 1; }
|
|
269
|
+
`;
|
|
87
270
|
|
|
88
271
|
// src/lib/theme.ts
|
|
89
272
|
var themeCss = default_default;
|
|
@@ -97,22 +280,23 @@ function injectTheme(doc = document) {
|
|
|
97
280
|
|
|
98
281
|
// src/codelight.ts
|
|
99
282
|
var codelight = {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
283
|
+
addCopyButton: _chunkKZATGNQAcjs.addCopyButton,
|
|
284
|
+
createMarkup: _chunkKZATGNQAcjs.createMarkup,
|
|
285
|
+
createStream: _chunkKZATGNQAcjs.createStream,
|
|
286
|
+
defineLanguage: _chunkKZATGNQAcjs.defineLanguage,
|
|
287
|
+
escapeHtml: _chunkKZATGNQAcjs.escapeHtml,
|
|
288
|
+
getLanguage: _chunkKZATGNQAcjs.getLanguage,
|
|
289
|
+
highlight: _chunkKZATGNQAcjs.highlight,
|
|
106
290
|
highlightAll,
|
|
107
291
|
highlightElement,
|
|
108
292
|
injectTheme,
|
|
109
|
-
listLanguages:
|
|
110
|
-
mount:
|
|
111
|
-
registerLanguage:
|
|
112
|
-
renderLine:
|
|
293
|
+
listLanguages: _chunkKZATGNQAcjs.listLanguages,
|
|
294
|
+
mount: _chunkKZATGNQAcjs.mount,
|
|
295
|
+
registerLanguage: _chunkKZATGNQAcjs.registerLanguage,
|
|
296
|
+
renderLine: _chunkKZATGNQAcjs.renderLine,
|
|
113
297
|
splitFences,
|
|
114
298
|
themeCss,
|
|
115
|
-
tokenize:
|
|
299
|
+
tokenize: _chunkKZATGNQAcjs.tokenize
|
|
116
300
|
};
|
|
117
301
|
var codelight_default = codelight;
|
|
118
302
|
|
|
@@ -133,7 +317,8 @@ var codelight_default = codelight;
|
|
|
133
317
|
|
|
134
318
|
|
|
135
319
|
|
|
136
|
-
|
|
320
|
+
|
|
321
|
+
exports.addCopyButton = _chunkKZATGNQAcjs.addCopyButton; exports.createMarkup = _chunkKZATGNQAcjs.createMarkup; exports.createStream = _chunkKZATGNQAcjs.createStream; exports.default = codelight_default; exports.defineLanguage = _chunkKZATGNQAcjs.defineLanguage; exports.escapeHtml = _chunkKZATGNQAcjs.escapeHtml; exports.getLanguage = _chunkKZATGNQAcjs.getLanguage; exports.highlight = _chunkKZATGNQAcjs.highlight; exports.highlightAll = highlightAll; exports.highlightElement = highlightElement; exports.injectTheme = injectTheme; exports.listLanguages = _chunkKZATGNQAcjs.listLanguages; exports.mount = _chunkKZATGNQAcjs.mount; exports.registerLanguage = _chunkKZATGNQAcjs.registerLanguage; exports.renderLine = _chunkKZATGNQAcjs.renderLine; exports.splitFences = splitFences; exports.themeCss = themeCss; exports.tokenize = _chunkKZATGNQAcjs.tokenize;
|
|
137
322
|
/*!
|
|
138
323
|
codelight.js — syntax highlighting for code that streams in, like AI chat replies.
|
|
139
324
|
MIT License · https://github.com/codelightjs/codelight.js
|
package/lib/codelight.d.cts
CHANGED
|
@@ -1,39 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
* Highlights a finished code element in place from its text. The language comes from `data-lang`
|
|
3
|
-
* or a `language-xxx` class unless you pass one. Marks the element with `data-cl` so it isn't done twice.
|
|
4
|
-
*/
|
|
5
|
-
declare function highlightElement(code: Element, lang?: string): void;
|
|
6
|
-
/**
|
|
7
|
-
* Highlights every `<pre><code>` inside `root` (the whole page by default) that hasn't been
|
|
8
|
-
* highlighted yet. For code that's already on the page; use `mount` for code that's streaming in.
|
|
9
|
-
* If the page is still loading, it waits for it.
|
|
10
|
-
*/
|
|
11
|
-
declare function highlightAll(root?: ParentNode): void;
|
|
12
|
-
|
|
13
|
-
interface CodeView {
|
|
14
|
-
/** Appends text. Finished lines are added once; only the last line is redrawn. */
|
|
15
|
-
write(chunk: string): void;
|
|
16
|
-
/**
|
|
17
|
-
* Shows `text`. If it starts with what's already shown, only the new part is written,
|
|
18
|
-
* so you can pass the whole reply on every update, as React and most chat state do.
|
|
19
|
-
*/
|
|
20
|
-
set(text: string): void;
|
|
21
|
-
/** Clears the code, optionally switching language. */
|
|
22
|
-
reset(lang?: string): void;
|
|
23
|
-
readonly text: string;
|
|
24
|
-
readonly lang: string | undefined;
|
|
25
|
-
}
|
|
26
|
-
/**
|
|
27
|
-
* Streams highlighted code into an element, usually a `<code>` inside a `<pre>`.
|
|
28
|
-
* Lines that are finished are never touched again, so there's no flicker, text selection
|
|
29
|
-
* keeps working while the reply streams in, and the cost doesn't grow with the length of the code.
|
|
30
|
-
*/
|
|
31
|
-
declare function mount(target: Element, options?: {
|
|
32
|
-
lang?: string;
|
|
33
|
-
}): CodeView;
|
|
1
|
+
import { L as LineOptions } from './lines-BCdhnSO4.cjs';
|
|
34
2
|
|
|
35
3
|
/** What a piece of code is. Each type becomes a `cl-<type>` CSS class. */
|
|
36
|
-
type TokenType = "keyword" | "string" | "comment" | "number" | "function" | "type" | "constant" | "variable" | "property" | "operator" | "punctuation" | "tag" | "attr" | "meta" | "inserted" | "deleted";
|
|
4
|
+
type TokenType = "keyword" | "string" | "regex" | "comment" | "number" | "function" | "type" | "constant" | "variable" | "property" | "operator" | "punctuation" | "tag" | "attr" | "meta" | "inserted" | "deleted";
|
|
37
5
|
/** A run of text. Plain text has no `type`. */
|
|
38
6
|
interface Token {
|
|
39
7
|
type?: TokenType;
|
|
@@ -55,6 +23,58 @@ interface Language {
|
|
|
55
23
|
};
|
|
56
24
|
}
|
|
57
25
|
|
|
26
|
+
interface HighlightOptions extends LineOptions {
|
|
27
|
+
/** Add a Copy button to each `<pre>`. */
|
|
28
|
+
copyButton?: boolean;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Highlights a finished code element in place from its text. The language comes from `data-lang`
|
|
32
|
+
* or a `language-xxx` class unless you pass one. `data-line-numbers` and `data-highlight-lines="2,4-6"`
|
|
33
|
+
* on the code or its `<pre>` work like the options. Marks the element with `data-cl` so it isn't done twice.
|
|
34
|
+
*/
|
|
35
|
+
declare function highlightElement(code: Element, lang?: string, options?: HighlightOptions): void;
|
|
36
|
+
/**
|
|
37
|
+
* Highlights every `<pre><code>` inside `root` (the whole page by default) that hasn't been
|
|
38
|
+
* highlighted yet. For code that's already on the page; use `mount` for code that's still changing.
|
|
39
|
+
* If the page is still loading, it waits for it. Options can come first: `highlightAll({ lineNumbers: true })`.
|
|
40
|
+
*/
|
|
41
|
+
declare function highlightAll(root?: ParentNode | HighlightOptions, options?: HighlightOptions): void;
|
|
42
|
+
|
|
43
|
+
interface CopyButtonOptions {
|
|
44
|
+
/** Button text. Default "Copy". */
|
|
45
|
+
label?: string;
|
|
46
|
+
/** Text shown for a moment after copying. Default "Copied". */
|
|
47
|
+
copiedLabel?: string;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Adds a Copy button to the `<pre>` around a code element. It copies the code's text (line numbers
|
|
51
|
+
* aren't included) and says "Copied" for a moment. Does nothing if the button is already there.
|
|
52
|
+
*/
|
|
53
|
+
declare function addCopyButton(code: Element, options?: CopyButtonOptions): HTMLButtonElement | null;
|
|
54
|
+
|
|
55
|
+
interface CodeView {
|
|
56
|
+
/** Appends text. Finished lines are added once; only the last line is updated. */
|
|
57
|
+
write(chunk: string): void;
|
|
58
|
+
/**
|
|
59
|
+
* Shows `text`. If it starts with what's already shown, only the new part is written,
|
|
60
|
+
* so you can pass the whole text on every update, as React and most app state do.
|
|
61
|
+
*/
|
|
62
|
+
set(text: string): void;
|
|
63
|
+
/** Clears the code, optionally switching language. */
|
|
64
|
+
reset(lang?: string): void;
|
|
65
|
+
readonly text: string;
|
|
66
|
+
readonly lang: string | undefined;
|
|
67
|
+
}
|
|
68
|
+
interface MountOptions extends LineOptions {
|
|
69
|
+
lang?: string;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Shows highlighted code in an element (usually a `<code>` inside a `<pre>`) and keeps it up to date
|
|
73
|
+
* as text arrives. Finished lines are never touched again, so there's no flicker, text selection keeps
|
|
74
|
+
* working while it changes, and the cost doesn't grow with the length of the code.
|
|
75
|
+
*/
|
|
76
|
+
declare function mount(target: Element, options?: MountOptions): CodeView;
|
|
77
|
+
|
|
58
78
|
interface StringDef {
|
|
59
79
|
open: string;
|
|
60
80
|
/** Defaults to `open`. */
|
|
@@ -65,6 +85,11 @@ interface StringDef {
|
|
|
65
85
|
multiline?: boolean;
|
|
66
86
|
/** Default "string". */
|
|
67
87
|
type?: TokenType;
|
|
88
|
+
/**
|
|
89
|
+
* Code inside the string, highlighted as code: `["${", "}"]` for JavaScript template literals,
|
|
90
|
+
* `["{", "}"]` for Python f-strings (where `{{` stays literal), `["\\(", ")"]` for Swift.
|
|
91
|
+
*/
|
|
92
|
+
interpolate?: [open: string, close: string];
|
|
68
93
|
}
|
|
69
94
|
interface Rule {
|
|
70
95
|
/** Tried at each position before anything else. Lookbehind works, so `(?<=^\s*)@\w+` matches at line start. */
|
|
@@ -89,6 +114,12 @@ interface LanguageDef {
|
|
|
89
114
|
classNames?: boolean;
|
|
90
115
|
/** A name followed by `(` is a function. Default true. */
|
|
91
116
|
calls?: boolean;
|
|
117
|
+
/** Colour numbers. Default true. Prose formats like Markdown turn this off. */
|
|
118
|
+
numbers?: boolean;
|
|
119
|
+
/** Colour operators such as `=` and `&&`. Default true. */
|
|
120
|
+
operators?: boolean;
|
|
121
|
+
/** Shell-style heredocs: `<<EOF` … `EOF`, `<<-EOF`, `<<'EOF'`. */
|
|
122
|
+
heredoc?: boolean;
|
|
92
123
|
}
|
|
93
124
|
/** Builds a language from keyword lists and comment and string rules. */
|
|
94
125
|
declare function defineLanguage(def: LanguageDef): Language;
|
|
@@ -127,8 +158,11 @@ declare function escapeHtml(text: string): string;
|
|
|
127
158
|
declare function renderLine(line: Line): string;
|
|
128
159
|
/** Tokens for every line of `code`. */
|
|
129
160
|
declare function tokenize(code: string, lang?: string | Language): Line[];
|
|
130
|
-
/**
|
|
131
|
-
|
|
161
|
+
/**
|
|
162
|
+
* Highlighted HTML for finished code, to put inside `<pre><code class="cl">`. With `lineNumbers` or
|
|
163
|
+
* `highlightLines`, each line is wrapped in `<span class="cl-line" data-line="n">`.
|
|
164
|
+
*/
|
|
165
|
+
declare function highlight(code: string, lang?: string | Language, options?: LineOptions): string;
|
|
132
166
|
interface StreamUpdate {
|
|
133
167
|
/** Lines finished by this chunk. They never change again. */
|
|
134
168
|
lines: Line[];
|
|
@@ -163,6 +197,7 @@ declare function injectTheme(doc?: Document): void;
|
|
|
163
197
|
|
|
164
198
|
/** Everything in one object: `import codelight from "codelight.js"`. */
|
|
165
199
|
declare const codelight: {
|
|
200
|
+
addCopyButton: typeof addCopyButton;
|
|
166
201
|
createMarkup: typeof createMarkup;
|
|
167
202
|
createStream: typeof createStream;
|
|
168
203
|
defineLanguage: typeof defineLanguage;
|
|
@@ -181,4 +216,4 @@ declare const codelight: {
|
|
|
181
216
|
tokenize: typeof tokenize;
|
|
182
217
|
};
|
|
183
218
|
|
|
184
|
-
export { type CodeView, type Language, type LanguageDef, type Line, type Rule, type Segment, type State, type Stream, type StreamUpdate, type StringDef, type Token, type TokenType, createMarkup, createStream, codelight as default, defineLanguage, escapeHtml, getLanguage, highlight, highlightAll, highlightElement, injectTheme, listLanguages, mount, registerLanguage, renderLine, splitFences, themeCss, tokenize };
|
|
219
|
+
export { type CodeView, type CopyButtonOptions, type HighlightOptions, type Language, type LanguageDef, type Line, LineOptions, type MountOptions, type Rule, type Segment, type State, type Stream, type StreamUpdate, type StringDef, type Token, type TokenType, addCopyButton, createMarkup, createStream, codelight as default, defineLanguage, escapeHtml, getLanguage, highlight, highlightAll, highlightElement, injectTheme, listLanguages, mount, registerLanguage, renderLine, splitFences, themeCss, tokenize };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
interface LineOptions {
|
|
2
|
+
/** Show a number before each line. Numbers aren't part of the text, so copying code leaves them out. */
|
|
3
|
+
lineNumbers?: boolean;
|
|
4
|
+
/** Lines to highlight, counting from 1: `"3"`, `"1,4-6"`, `5` or `[1, 4, 5, 6]`. */
|
|
5
|
+
highlightLines?: string | number | number[];
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type { LineOptions as L };
|
package/lib/react.cjs
CHANGED
|
@@ -1,32 +1,46 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); 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.
|
|
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.1 | MIT License | https://github.com/codelightjs/codelight.js */
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
|
|
6
|
+
var _chunkKZATGNQAcjs = require('./chunk-KZATGNQA.cjs');
|
|
6
7
|
|
|
7
8
|
// src/react.ts
|
|
8
9
|
var _react = require('react');
|
|
9
10
|
var useIsomorphicLayoutEffect = typeof window === "undefined" ? _react.useEffect : _react.useLayoutEffect;
|
|
10
|
-
function CodeLight({ code, lang, className, ...rest }) {
|
|
11
|
+
function CodeLight({ code, lang, lineNumbers, highlightLines, copyButton, className, ...rest }) {
|
|
11
12
|
const ref = _react.useRef.call(void 0, null);
|
|
12
13
|
const view = _react.useRef.call(void 0, null);
|
|
13
|
-
const [
|
|
14
|
+
const [copied, setCopied] = _react.useState.call(void 0, false);
|
|
15
|
+
const [initial] = _react.useState.call(void 0, () => ({ __html: _chunkKZATGNQAcjs.highlight.call(void 0, code, lang, { lineNumbers, highlightLines }) }));
|
|
16
|
+
const lines = `${!!lineNumbers}|${String(_nullishCoalesce(highlightLines, () => ( "")))}`;
|
|
14
17
|
useIsomorphicLayoutEffect(() => {
|
|
15
|
-
const v =
|
|
18
|
+
const v = _chunkKZATGNQAcjs.mount.call(void 0, ref.current, { lang, lineNumbers, highlightLines });
|
|
16
19
|
v.set(code);
|
|
17
20
|
view.current = v;
|
|
18
21
|
return () => {
|
|
19
22
|
view.current = null;
|
|
20
23
|
};
|
|
21
|
-
}, [lang]);
|
|
24
|
+
}, [lang, lines]);
|
|
22
25
|
useIsomorphicLayoutEffect(() => {
|
|
23
26
|
_optionalChain([view, 'access', _ => _.current, 'optionalAccess', _2 => _2.set, 'call', _3 => _3(code)]);
|
|
24
27
|
}, [code]);
|
|
28
|
+
_react.useEffect.call(void 0, () => {
|
|
29
|
+
if (!copied) return;
|
|
30
|
+
const t = setTimeout(() => setCopied(false), 1500);
|
|
31
|
+
return () => clearTimeout(t);
|
|
32
|
+
}, [copied]);
|
|
33
|
+
const classes = [className, copyButton ? "cl-has-copy" : ""].filter(Boolean).join(" ") || void 0;
|
|
25
34
|
return _react.createElement.call(void 0,
|
|
26
35
|
"pre",
|
|
27
|
-
{ ...rest, className, "data-lang": lang },
|
|
36
|
+
{ ...rest, className: classes, "data-lang": lang },
|
|
28
37
|
// `<pre><code class="cl">`, the same markup highlightAll() produces.
|
|
29
|
-
_react.createElement.call(void 0, "code", { ref, className: "cl", dangerouslySetInnerHTML: initial })
|
|
38
|
+
_react.createElement.call(void 0, "code", { ref, className: "cl", dangerouslySetInnerHTML: initial }),
|
|
39
|
+
copyButton ? _react.createElement.call(void 0,
|
|
40
|
+
"button",
|
|
41
|
+
{ type: "button", className: "cl-copy", "aria-label": "Copy code", onClick: () => void _chunkKZATGNQAcjs.copyText.call(void 0, code).then(() => setCopied(true)) },
|
|
42
|
+
copied ? "Copied" : "Copy"
|
|
43
|
+
) : null
|
|
30
44
|
);
|
|
31
45
|
}
|
|
32
46
|
|
package/lib/react.d.cts
CHANGED
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
import { HTMLAttributes, ReactElement } from 'react';
|
|
2
|
+
import { L as LineOptions } from './lines-BCdhnSO4.cjs';
|
|
2
3
|
|
|
3
|
-
interface CodeLightProps extends Omit<HTMLAttributes<HTMLPreElement>, "children"
|
|
4
|
+
interface CodeLightProps extends Omit<HTMLAttributes<HTMLPreElement>, "children">, LineOptions {
|
|
4
5
|
/** The code so far. Pass the whole thing on every update; only the new part is highlighted. */
|
|
5
6
|
code: string;
|
|
6
7
|
/** Language name or alias: `ts`, `python`, `sh`… Unknown names show plain text. */
|
|
7
8
|
lang?: string;
|
|
9
|
+
/** Show a Copy button. */
|
|
10
|
+
copyButton?: boolean;
|
|
8
11
|
}
|
|
9
12
|
/**
|
|
10
|
-
* Highlighted code that can grow while it
|
|
11
|
-
* so long
|
|
13
|
+
* Highlighted code that can grow while it's being written. Finished lines are never re-rendered,
|
|
14
|
+
* so long code doesn't flicker or slow down. Renders highlighted HTML on the server too.
|
|
12
15
|
*/
|
|
13
|
-
declare function CodeLight({ code, lang, className, ...rest }: CodeLightProps): ReactElement;
|
|
16
|
+
declare function CodeLight({ code, lang, lineNumbers, highlightLines, copyButton, className, ...rest }: CodeLightProps): ReactElement;
|
|
14
17
|
|
|
15
18
|
export { CodeLight, type CodeLightProps };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "codelight.js",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "Very fast, lightweight syntax highlighter. Highlights each line once and redraws only what changed, so live code never flickers.
|
|
3
|
+
"version": "1.3.1",
|
|
4
|
+
"description": "Very fast, lightweight syntax highlighter. Highlights each line once and redraws only what changed, so live code never flickers. 27 languages, no dependencies, React or a script tag.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"syntax-highlighting",
|
|
7
7
|
"highlight",
|
|
@@ -90,17 +90,22 @@
|
|
|
90
90
|
"release:minor": "npm version minor && git push --follow-tags",
|
|
91
91
|
"release:major": "npm version major && git push --follow-tags",
|
|
92
92
|
"screenshot": "npm run demo && node tools/screenshot.mjs",
|
|
93
|
-
"demo": "npm run build && node tools/build-demo.mjs"
|
|
93
|
+
"demo": "npm run build && node tools/build-demo.mjs",
|
|
94
|
+
"bench": "npm run build && node tools/bench/run.mjs"
|
|
94
95
|
},
|
|
95
96
|
"devDependencies": {
|
|
97
|
+
"@shikijs/stream": "^4.6.0",
|
|
96
98
|
"@types/node": "^20.19.43",
|
|
97
99
|
"@types/react": "^19.3.3",
|
|
98
100
|
"@types/react-dom": "^19.3.0",
|
|
99
101
|
"esbuild": "^0.27.7",
|
|
100
102
|
"happy-dom": "^21.0.4",
|
|
103
|
+
"highlight.js": "^11.12.0",
|
|
104
|
+
"prismjs": "^1.30.0",
|
|
101
105
|
"puppeteer-core": "^25.19.0",
|
|
102
106
|
"react": "^19.3.1",
|
|
103
107
|
"react-dom": "^19.3.1",
|
|
108
|
+
"shiki": "^4.6.0",
|
|
104
109
|
"tsup": "^8.5.1",
|
|
105
110
|
"typescript": "^5.9.3",
|
|
106
111
|
"vitest": "^2.1.9"
|