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/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.1 | MIT License | https://github.com/codelightjs/codelight.js */
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
 
3
3
 
4
4
 
@@ -11,22 +11,30 @@
11
11
 
12
12
 
13
13
 
14
- var _chunkKY23A6M5cjs = require('./chunk-KY23A6M5.cjs');
14
+
15
+ var _chunkHSSBKI63cjs = require('./chunk-HSSBKI63.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(_nullishCoalesce(code.getAttribute("data-lang"), () => ( _optionalChain([code, 'access', _ => _.parentElement, 'optionalAccess', _2 => _2.getAttribute, 'call', _3 => _3("data-lang")]))), () => ( _optionalChain([MARKDOWN_CLASS, 'access', _4 => _4.exec, 'call', _5 => _5(code.className), 'optionalAccess', _6 => _6[1]]))), () => ( ""));
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
- code.innerHTML = _chunkKY23A6M5cjs.highlight.call(void 0, _nullishCoalesce(code.textContent, () => ( "")), lang);
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 = _chunkHSSBKI63cjs.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) _chunkHSSBKI63cjs.addCopyButton.call(void 0, code);
25
30
  }
26
- function highlightAll(root) {
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 = '/*!\n codelight.js theme: default\n Light or dark to match the system. To force one, put a cl-light or cl-dark class\n (or light / dark, or data-theme="light" / "dark") on the code or any parent.\n*/\n.cl {\n --cl-fg: #1e2430;\n --cl-bg: #f7f7f9;\n --cl-keyword: #b4235a;\n --cl-string: #1d6b45;\n --cl-comment: #6b7280;\n --cl-number: #a35200;\n --cl-function: #3a52c4;\n --cl-type: #8a3ab9;\n --cl-constant: #a35200;\n --cl-variable: #9a3f0c;\n --cl-property: #1f6f9a;\n --cl-operator: #b4235a;\n --cl-punctuation: #4b5563;\n --cl-tag: #2459a8;\n --cl-attr: #8a3ab9;\n --cl-meta: #6b4fbb;\n --cl-inserted: #166534;\n --cl-inserted-bg: #dcfce7;\n --cl-deleted: #9f1239;\n --cl-deleted-bg: #ffe4e6;\n color: var(--cl-fg);\n background: var(--cl-bg);\n}\n@media (prefers-color-scheme: dark) {\n .cl {\n --cl-fg: #e4e7ee;\n --cl-bg: #11141a;\n --cl-keyword: #ff7aa8;\n --cl-string: #8fdba8;\n --cl-comment: #8a93a3;\n --cl-number: #ffb86b;\n --cl-function: #8fb4ff;\n --cl-type: #d0a3ff;\n --cl-constant: #ffb86b;\n --cl-variable: #ffa070;\n --cl-property: #7fd0f0;\n --cl-operator: #ff7aa8;\n --cl-punctuation: #aab1bd;\n --cl-tag: #8fb4ff;\n --cl-attr: #d0a3ff;\n --cl-meta: #b49cff;\n --cl-inserted: #a7f3c0;\n --cl-inserted-bg: #0f3a24;\n --cl-deleted: #fecdd3;\n --cl-deleted-bg: #4c1020;\n }\n}\n.cl-dark .cl, .cl.cl-dark, .dark .cl, [data-theme="dark"] .cl {\n --cl-fg: #e4e7ee;\n --cl-bg: #11141a;\n --cl-keyword: #ff7aa8;\n --cl-string: #8fdba8;\n --cl-comment: #8a93a3;\n --cl-number: #ffb86b;\n --cl-function: #8fb4ff;\n --cl-type: #d0a3ff;\n --cl-constant: #ffb86b;\n --cl-variable: #ffa070;\n --cl-property: #7fd0f0;\n --cl-operator: #ff7aa8;\n --cl-punctuation: #aab1bd;\n --cl-tag: #8fb4ff;\n --cl-attr: #d0a3ff;\n --cl-meta: #b49cff;\n --cl-inserted: #a7f3c0;\n --cl-inserted-bg: #0f3a24;\n --cl-deleted: #fecdd3;\n --cl-deleted-bg: #4c1020;\n}\n.cl-light .cl, .cl.cl-light, .light .cl, [data-theme="light"] .cl {\n --cl-fg: #1e2430;\n --cl-bg: #f7f7f9;\n --cl-keyword: #b4235a;\n --cl-string: #1d6b45;\n --cl-comment: #6b7280;\n --cl-number: #a35200;\n --cl-function: #3a52c4;\n --cl-type: #8a3ab9;\n --cl-constant: #a35200;\n --cl-variable: #9a3f0c;\n --cl-property: #1f6f9a;\n --cl-operator: #b4235a;\n --cl-punctuation: #4b5563;\n --cl-tag: #2459a8;\n --cl-attr: #8a3ab9;\n --cl-meta: #6b4fbb;\n --cl-inserted: #166534;\n --cl-inserted-bg: #dcfce7;\n --cl-deleted: #9f1239;\n --cl-deleted-bg: #ffe4e6;\n}\npre > code.cl {\n display: block;\n padding: 1rem 1.25rem;\n overflow: auto;\n}\n.cl-keyword { color: var(--cl-keyword); }\n.cl-string { color: var(--cl-string); }\n.cl-comment { color: var(--cl-comment); }\n.cl-number { color: var(--cl-number); }\n.cl-function { color: var(--cl-function); }\n.cl-type { color: var(--cl-type); }\n.cl-constant { color: var(--cl-constant); }\n.cl-variable { color: var(--cl-variable); }\n.cl-property { color: var(--cl-property); }\n.cl-operator { color: var(--cl-operator); }\n.cl-punctuation { color: var(--cl-punctuation); }\n.cl-tag { color: var(--cl-tag); }\n.cl-attr { color: var(--cl-attr); }\n.cl-meta { color: var(--cl-meta); }\n.cl-comment { font-style: italic; }\n.cl-inserted { color: var(--cl-inserted); background: var(--cl-inserted-bg); }\n.cl-deleted { color: var(--cl-deleted); background: var(--cl-deleted-bg); }\n';
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
- createMarkup: _chunkKY23A6M5cjs.createMarkup,
101
- createStream: _chunkKY23A6M5cjs.createStream,
102
- defineLanguage: _chunkKY23A6M5cjs.defineLanguage,
103
- escapeHtml: _chunkKY23A6M5cjs.escapeHtml,
104
- getLanguage: _chunkKY23A6M5cjs.getLanguage,
105
- highlight: _chunkKY23A6M5cjs.highlight,
283
+ addCopyButton: _chunkHSSBKI63cjs.addCopyButton,
284
+ createMarkup: _chunkHSSBKI63cjs.createMarkup,
285
+ createStream: _chunkHSSBKI63cjs.createStream,
286
+ defineLanguage: _chunkHSSBKI63cjs.defineLanguage,
287
+ escapeHtml: _chunkHSSBKI63cjs.escapeHtml,
288
+ getLanguage: _chunkHSSBKI63cjs.getLanguage,
289
+ highlight: _chunkHSSBKI63cjs.highlight,
106
290
  highlightAll,
107
291
  highlightElement,
108
292
  injectTheme,
109
- listLanguages: _chunkKY23A6M5cjs.listLanguages,
110
- mount: _chunkKY23A6M5cjs.mount,
111
- registerLanguage: _chunkKY23A6M5cjs.registerLanguage,
112
- renderLine: _chunkKY23A6M5cjs.renderLine,
293
+ listLanguages: _chunkHSSBKI63cjs.listLanguages,
294
+ mount: _chunkHSSBKI63cjs.mount,
295
+ registerLanguage: _chunkHSSBKI63cjs.registerLanguage,
296
+ renderLine: _chunkHSSBKI63cjs.renderLine,
113
297
  splitFences,
114
298
  themeCss,
115
- tokenize: _chunkKY23A6M5cjs.tokenize
299
+ tokenize: _chunkHSSBKI63cjs.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
- exports.createMarkup = _chunkKY23A6M5cjs.createMarkup; exports.createStream = _chunkKY23A6M5cjs.createStream; exports.default = codelight_default; exports.defineLanguage = _chunkKY23A6M5cjs.defineLanguage; exports.escapeHtml = _chunkKY23A6M5cjs.escapeHtml; exports.getLanguage = _chunkKY23A6M5cjs.getLanguage; exports.highlight = _chunkKY23A6M5cjs.highlight; exports.highlightAll = highlightAll; exports.highlightElement = highlightElement; exports.injectTheme = injectTheme; exports.listLanguages = _chunkKY23A6M5cjs.listLanguages; exports.mount = _chunkKY23A6M5cjs.mount; exports.registerLanguage = _chunkKY23A6M5cjs.registerLanguage; exports.renderLine = _chunkKY23A6M5cjs.renderLine; exports.splitFences = splitFences; exports.themeCss = themeCss; exports.tokenize = _chunkKY23A6M5cjs.tokenize;
320
+
321
+ exports.addCopyButton = _chunkHSSBKI63cjs.addCopyButton; exports.createMarkup = _chunkHSSBKI63cjs.createMarkup; exports.createStream = _chunkHSSBKI63cjs.createStream; exports.default = codelight_default; exports.defineLanguage = _chunkHSSBKI63cjs.defineLanguage; exports.escapeHtml = _chunkHSSBKI63cjs.escapeHtml; exports.getLanguage = _chunkHSSBKI63cjs.getLanguage; exports.highlight = _chunkHSSBKI63cjs.highlight; exports.highlightAll = highlightAll; exports.highlightElement = highlightElement; exports.injectTheme = injectTheme; exports.listLanguages = _chunkHSSBKI63cjs.listLanguages; exports.mount = _chunkHSSBKI63cjs.mount; exports.registerLanguage = _chunkHSSBKI63cjs.registerLanguage; exports.renderLine = _chunkHSSBKI63cjs.renderLine; exports.splitFences = splitFences; exports.themeCss = themeCss; exports.tokenize = _chunkHSSBKI63cjs.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
@@ -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
- /** Highlighted HTML for finished code, to put inside `<pre><code class="cl">`. */
131
- declare function highlight(code: string, lang?: string | Language): string;
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.1 | MIT License | https://github.com/codelightjs/codelight.js */
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
 
3
3
 
4
4
 
5
- var _chunkKY23A6M5cjs = require('./chunk-KY23A6M5.cjs');
5
+
6
+ var _chunkHSSBKI63cjs = require('./chunk-HSSBKI63.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 [initial] = _react.useState.call(void 0, () => ({ __html: _chunkKY23A6M5cjs.highlight.call(void 0, code, lang) }));
14
+ const [copied, setCopied] = _react.useState.call(void 0, false);
15
+ const [initial] = _react.useState.call(void 0, () => ({ __html: _chunkHSSBKI63cjs.highlight.call(void 0, code, lang, { lineNumbers, highlightLines }) }));
16
+ const lines = `${!!lineNumbers}|${String(_nullishCoalesce(highlightLines, () => ( "")))}`;
14
17
  useIsomorphicLayoutEffect(() => {
15
- const v = _chunkKY23A6M5cjs.mount.call(void 0, ref.current, { lang });
18
+ const v = _chunkHSSBKI63cjs.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 _chunkHSSBKI63cjs.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 streams in. Finished lines are never re-rendered,
11
- * so long AI replies don't flicker or slow down. Renders highlighted HTML on the server too.
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.1.1",
4
- "description": "Very fast, lightweight syntax highlighter. Highlights each line once and redraws only what changed, so live code never flickers. 19 languages, no dependencies, React or a script tag.",
3
+ "version": "1.3.0",
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",