codelight.js 1.2.0 → 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/es/codelight.d.ts 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.js';
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 };
package/es/codelight.js CHANGED
@@ -1,5 +1,6 @@
1
- /*! codelight.js 1.2.0 | MIT License | https://github.com/codelightjs/codelight.js */
1
+ /*! codelight.js 1.3.0 | MIT License | https://github.com/codelightjs/codelight.js */
2
2
  import {
3
+ addCopyButton,
3
4
  createMarkup,
4
5
  createStream,
5
6
  defineLanguage,
@@ -11,22 +12,29 @@ import {
11
12
  registerLanguage,
12
13
  renderLine,
13
14
  tokenize
14
- } from "./chunk-63RCMYG4.js";
15
+ } from "./chunk-F46VGBFI.js";
15
16
 
16
17
  // src/lib/auto.ts
17
18
  var MARKDOWN_CLASS = /(?:^|\s)language-(\S+)/;
19
+ var attr = (code, name) => code.getAttribute(name) ?? code.parentElement?.getAttribute(name) ?? null;
18
20
  function languageOf(code) {
19
- return code.getAttribute("data-lang") ?? code.parentElement?.getAttribute("data-lang") ?? MARKDOWN_CLASS.exec(code.className)?.[1] ?? "";
21
+ return attr(code, "data-lang") ?? MARKDOWN_CLASS.exec(code.className)?.[1] ?? "";
20
22
  }
21
- function highlightElement(code, lang = languageOf(code)) {
22
- code.innerHTML = highlight(code.textContent ?? "", lang);
23
+ function highlightElement(code, lang = languageOf(code), options = {}) {
24
+ const lineNumbers = options.lineNumbers ?? attr(code, "data-line-numbers") != null;
25
+ const highlightLines = options.highlightLines ?? attr(code, "data-highlight-lines") ?? void 0;
26
+ code.innerHTML = highlight(code.textContent ?? "", lang, { lineNumbers, highlightLines });
23
27
  code.classList.add("cl");
24
28
  code.setAttribute("data-cl", lang);
29
+ if (options.copyButton) addCopyButton(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 (root ?? document).querySelectorAll("pre > code:not([data-cl])")) highlightElement(code);
37
+ for (const code of (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,6 +280,7 @@ function injectTheme(doc = document) {
97
280
 
98
281
  // src/codelight.ts
99
282
  var codelight = {
283
+ addCopyButton,
100
284
  createMarkup,
101
285
  createStream,
102
286
  defineLanguage,
@@ -116,6 +300,7 @@ var codelight = {
116
300
  };
117
301
  var codelight_default = codelight;
118
302
  export {
303
+ addCopyButton,
119
304
  createMarkup,
120
305
  createStream,
121
306
  codelight_default as default,
@@ -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/es/react.d.ts CHANGED
@@ -1,15 +1,18 @@
1
1
  import { HTMLAttributes, ReactElement } from 'react';
2
+ import { L as LineOptions } from './lines-BCdhnSO4.js';
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/es/react.js CHANGED
@@ -1,32 +1,46 @@
1
- /*! codelight.js 1.2.0 | MIT License | https://github.com/codelightjs/codelight.js */
1
+ /*! codelight.js 1.3.0 | MIT License | https://github.com/codelightjs/codelight.js */
2
2
  import {
3
+ copyText,
3
4
  highlight,
4
5
  mount
5
- } from "./chunk-63RCMYG4.js";
6
+ } from "./chunk-F46VGBFI.js";
6
7
 
7
8
  // src/react.ts
8
9
  import { createElement, useEffect, useLayoutEffect, useRef, useState } from "react";
9
10
  var useIsomorphicLayoutEffect = typeof window === "undefined" ? useEffect : useLayoutEffect;
10
- function CodeLight({ code, lang, className, ...rest }) {
11
+ function CodeLight({ code, lang, lineNumbers, highlightLines, copyButton, className, ...rest }) {
11
12
  const ref = useRef(null);
12
13
  const view = useRef(null);
13
- const [initial] = useState(() => ({ __html: highlight(code, lang) }));
14
+ const [copied, setCopied] = useState(false);
15
+ const [initial] = useState(() => ({ __html: highlight(code, lang, { lineNumbers, highlightLines }) }));
16
+ const lines = `${!!lineNumbers}|${String(highlightLines ?? "")}`;
14
17
  useIsomorphicLayoutEffect(() => {
15
- const v = mount(ref.current, { lang });
18
+ const v = mount(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
  view.current?.set(code);
24
27
  }, [code]);
28
+ useEffect(() => {
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 createElement(
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
- createElement("code", { ref, className: "cl", dangerouslySetInnerHTML: initial })
38
+ createElement("code", { ref, className: "cl", dangerouslySetInnerHTML: initial }),
39
+ copyButton ? createElement(
40
+ "button",
41
+ { type: "button", className: "cl-copy", "aria-label": "Copy code", onClick: () => void copyText(code).then(() => setCopied(true)) },
42
+ copied ? "Copied" : "Copy"
43
+ ) : null
30
44
  );
31
45
  }
32
46
  export {