@telperion/elements 6.0.0 → 7.0.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.
Files changed (94) hide show
  1. package/.build-manifest.json +22 -1
  2. package/README.md +37 -0
  3. package/_chunks/code-cdn-element-ClAO2lep.d.cts +200 -0
  4. package/_chunks/code-cdn-element-ClAO2lep.d.ts +200 -0
  5. package/code/bundled-language-loader.cjs +18 -0
  6. package/code/bundled-language-loader.cjs.map +1 -0
  7. package/code/bundled-language-loader.js +16 -0
  8. package/code/bundled-language-loader.js.map +1 -0
  9. package/code/cdn-language-loader.cjs +41 -0
  10. package/code/cdn-language-loader.cjs.map +1 -0
  11. package/code/cdn-language-loader.js +36 -0
  12. package/code/cdn-language-loader.js.map +1 -0
  13. package/code/cdn.cjs +21 -0
  14. package/code/cdn.cjs.map +1 -0
  15. package/code/cdn.d.cts +3 -0
  16. package/code/cdn.d.ts +3 -0
  17. package/code/cdn.js +16 -0
  18. package/code/cdn.js.map +1 -0
  19. package/code/code-element-base.cjs +241 -0
  20. package/code/code-element-base.cjs.map +1 -0
  21. package/code/code-element-base.js +239 -0
  22. package/code/code-element-base.js.map +1 -0
  23. package/code/code-element.cjs +16 -0
  24. package/code/code-element.cjs.map +1 -0
  25. package/code/code-element.js +14 -0
  26. package/code/code-element.js.map +1 -0
  27. package/code/copy.cjs +41 -0
  28. package/code/copy.cjs.map +1 -0
  29. package/code/copy.js +39 -0
  30. package/code/copy.js.map +1 -0
  31. package/code/css-variables-theme.cjs +24 -0
  32. package/code/css-variables-theme.cjs.map +1 -0
  33. package/code/css-variables-theme.js +21 -0
  34. package/code/css-variables-theme.js.map +1 -0
  35. package/code/highlighter.cjs +84 -0
  36. package/code/highlighter.cjs.map +1 -0
  37. package/code/highlighter.js +80 -0
  38. package/code/highlighter.js.map +1 -0
  39. package/code/index.cjs +19 -0
  40. package/code/index.cjs.map +1 -0
  41. package/code/index.d.cts +13 -0
  42. package/code/index.d.ts +13 -0
  43. package/code/index.js +6 -0
  44. package/code/index.js.map +1 -0
  45. package/code/language-loader.cjs +22 -0
  46. package/code/language-loader.cjs.map +1 -0
  47. package/code/language-loader.js +19 -0
  48. package/code/language-loader.js.map +1 -0
  49. package/code/line-ranges.cjs +28 -0
  50. package/code/line-ranges.cjs.map +1 -0
  51. package/code/line-ranges.js +25 -0
  52. package/code/line-ranges.js.map +1 -0
  53. package/code/source.cjs +52 -0
  54. package/code/source.cjs.map +1 -0
  55. package/code/source.js +48 -0
  56. package/code/source.js.map +1 -0
  57. package/code/styles.cjs +183 -0
  58. package/code/styles.cjs.map +1 -0
  59. package/code/styles.js +181 -0
  60. package/code/styles.js.map +1 -0
  61. package/code/templates.cjs +66 -0
  62. package/code/templates.cjs.map +1 -0
  63. package/code/templates.js +63 -0
  64. package/code/templates.js.map +1 -0
  65. package/custom-elements.json +1190 -1
  66. package/fesm/elements.mjs +19 -1
  67. package/fesm/elements.mjs.map +1 -1
  68. package/iife/code/cdn.js +263 -0
  69. package/iife/code/cdn.js.map +1 -0
  70. package/iife/elements.js +25 -25
  71. package/iife/elements.js.map +1 -1
  72. package/iife/portal/index.js +1 -1
  73. package/iife/portal/index.js.map +1 -1
  74. package/index.cjs +2 -0
  75. package/index.cjs.map +1 -1
  76. package/index.d.cts +1 -1
  77. package/index.d.ts +1 -1
  78. package/index.js +1 -0
  79. package/index.js.map +1 -1
  80. package/package.json +39 -2
  81. package/portal/index.cjs +2 -0
  82. package/portal/index.cjs.map +1 -1
  83. package/portal/index.d.cts +28 -1
  84. package/portal/index.d.ts +28 -1
  85. package/portal/index.js +1 -0
  86. package/portal/index.js.map +1 -1
  87. package/portal/portal-outlet-fallback-element.cjs +19 -0
  88. package/portal/portal-outlet-fallback-element.cjs.map +1 -0
  89. package/portal/portal-outlet-fallback-element.js +17 -0
  90. package/portal/portal-outlet-fallback-element.js.map +1 -0
  91. package/portal/styles.cjs +5 -0
  92. package/portal/styles.cjs.map +1 -1
  93. package/portal/styles.js +5 -0
  94. package/portal/styles.js.map +1 -1
@@ -110,7 +110,28 @@
110
110
  "dts": "portal/index.d.ts",
111
111
  "dcts": "portal/index.d.cts"
112
112
  }
113
+ },
114
+ {
115
+ "name": "code/index",
116
+ "sourcePath": "src/code/index.ts",
117
+ "outputs": {
118
+ "esm": "code/index.js",
119
+ "cjs": "code/index.cjs",
120
+ "dts": "code/index.d.ts",
121
+ "dcts": "code/index.d.cts"
122
+ }
123
+ },
124
+ {
125
+ "name": "code/cdn",
126
+ "sourcePath": "src/code/code-cdn-element.ts",
127
+ "outputs": {
128
+ "esm": "code/cdn.js",
129
+ "cjs": "code/cdn.cjs",
130
+ "iife": "iife/code/cdn.js",
131
+ "dts": "code/cdn.d.ts",
132
+ "dcts": "code/cdn.d.cts"
133
+ }
113
134
  }
114
135
  ],
115
- "generatedAt": "2026-11-11T10:58:55.622Z"
136
+ "generatedAt": "2026-11-11T13:13:26.071Z"
116
137
  }
package/README.md CHANGED
@@ -72,6 +72,9 @@ Use the **explicit `/iife/...` path** so the CDN serves the standalone bundle (s
72
72
 
73
73
  <!-- Or just the portal module (TelperionElements.Portal) -->
74
74
  <script src="https://unpkg.com/@telperion/elements/iife/portal/index.js"></script>
75
+
76
+ <!-- The code module (TelperionElements.Code) — not included in elements.js; grammars load from jsDelivr -->
77
+ <script src="https://unpkg.com/@telperion/elements/iife/code/cdn.js"></script>
75
78
  ```
76
79
 
77
80
  Each per-module bundle is fully self-contained: side-effect imports register the custom elements as soon as the script is evaluated. jsDelivr (`https://cdn.jsdelivr.net/npm/@telperion/elements/iife/...`) and other npm CDNs work the same way.
@@ -306,6 +309,7 @@ Render content somewhere else in the document. Children of `<tp-portal>` are **m
306
309
  **Includes:**
307
310
  - `<tp-portal>` &mdash; declares the content and the outlet `name` to render into
308
311
  - `<tp-portal-outlet>` &mdash; the render target
312
+ - `<tp-portal-outlet-fallback>` &mdash; default content of an outlet, shown while nothing is projected
309
313
 
310
314
  **Features:**
311
315
  - Same nodes, moved: state and bindings survive; uses the state-preserving `moveBefore()` where supported
@@ -313,6 +317,7 @@ Render content somewhere else in the document. Children of `<tp-portal>` are **m
313
317
  - Connection order doesn't matter; content returns to the portal when the outlet leaves
314
318
  - Several portals per outlet (in connection order); the most recent outlet of a name wins, the previous one takes over when it leaves
315
319
  - Reflected `projected` (portal) and `active` (outlet) attributes for styling
320
+ - Declarative fallback content per outlet via `<tp-portal-outlet-fallback>`
316
321
 
317
322
  #### JS/TS
318
323
  ```ts
@@ -333,6 +338,38 @@ import "@telperion/elements/portal";
333
338
 
334
339
  ---
335
340
 
341
+ ### Code
342
+
343
+ Syntax-highlighted code, inline or as a block, powered by [Shiki](https://shiki.style). Every Shiki language (~240 ids and aliases) loads lazily on first use.
344
+
345
+ **Features:**
346
+ - `inline` (default) and `block` variants
347
+ - Block extras: `line-numbers` (+ `line-start`), `highlight-lines="2, 4-6"`, `copyable`
348
+ - Themed entirely through `--tp-code-*` CSS variables, with defaults derived from the shared `--tp-color-*` palette
349
+ - Source from text content, a `<template>` child (HTML samples, no escaping), or the `code` property; light-DOM changes re-highlight
350
+ - Plain text renders instantly and is swapped for highlighted tokens without layout shift
351
+
352
+ > `<tp-code>` is not part of the root `@telperion/elements` import (its grammars would bloat the all-in-one bundle). Import `@telperion/elements/code` explicitly.
353
+
354
+ #### JS/TS
355
+ ```ts
356
+ import "@telperion/elements/code";
357
+ ```
358
+
359
+ #### HTML
360
+ ```html
361
+ <p>Run <tp-code language="sh">pnpm install</tp-code> first.</p>
362
+
363
+ <tp-code variant="block" language="ts" line-numbers highlight-lines="2" copyable>
364
+ const answer = 42;
365
+ console.log(answer);
366
+ </tp-code>
367
+ ```
368
+
369
+ [Code Documentation →](./src/code/README.md)
370
+
371
+ ---
372
+
336
373
  This library is actively being developed. More components will be added over time.
337
374
 
338
375
  ## 🤝 Contributing
@@ -0,0 +1,200 @@
1
+ import { LitElement, PropertyValues } from 'lit';
2
+ import { LanguageRegistration } from 'shiki/core';
3
+
4
+ /**
5
+ * Layout of `<tp-code>`.
6
+ *
7
+ * - `inline`: renders a single `<code>` that flows with surrounding text.
8
+ * - `block`: renders a `<pre><code>` block with optional line numbers,
9
+ * highlighted lines, and a copy button.
10
+ */
11
+ type CodeVariant = "inline" | "block";
12
+ /** `detail` of the `tp-code-highlight` event. */
13
+ interface CodeHighlightEventDetail {
14
+ /** The `language` attribute as requested by the author. */
15
+ language: string;
16
+ /** `false` when the language was unknown/plain and the code rendered as plain text. */
17
+ highlighted: boolean;
18
+ }
19
+ /** `detail` of the `tp-code-copy` event. */
20
+ interface CodeCopyEventDetail {
21
+ /** The (normalized) source that was copied. */
22
+ code: string;
23
+ /** Whether writing to the clipboard succeeded. */
24
+ success: boolean;
25
+ }
26
+
27
+ declare global {
28
+ interface HTMLElementTagNameMap {
29
+ /**
30
+ * Syntax-highlighted code, inline or as a block. Highlighting is powered by
31
+ * Shiki; every Shiki language loads lazily on first use.
32
+ *
33
+ * Source comes from the element's text content (or a `<template>` child, or
34
+ * the `code` property). Variants: `inline` (default) and `block` (with
35
+ * optional `line-numbers`, `highlight-lines`, and `copyable`). Colors are
36
+ * entirely `--tp-code-*` CSS variables.
37
+ *
38
+ * @example
39
+ * ```html
40
+ * <p>Run <tp-code language="sh">pnpm install</tp-code> first.</p>
41
+ *
42
+ * <tp-code variant="block" language="ts" line-numbers highlight-lines="2" copyable>
43
+ * const answer = 42;
44
+ * console.log(answer);
45
+ * </tp-code>
46
+ * ```
47
+ */
48
+ "tp-code": CodeElement;
49
+ }
50
+ interface HTMLElementEventMap {
51
+ "tp-code-highlight": CustomEvent<CodeHighlightEventDetail>;
52
+ "tp-code-copy": CustomEvent<CodeCopyEventDetail>;
53
+ }
54
+ }
55
+ /**
56
+ * `<tp-code>` &mdash; syntax-highlighted code powered by [Shiki](https://shiki.style).
57
+ *
58
+ * - **Source**: the element's text content, a direct `<template>` child (for
59
+ * HTML samples, no escaping needed), or the `code` property. Light-DOM
60
+ * changes are observed and re-highlighted.
61
+ * - **Languages**: any Shiki language id or alias via `language`, loaded lazily
62
+ * once per page. Unknown languages render as plain text.
63
+ * - **Rendering**: into shadow DOM, so the light DOM stays the untouched
64
+ * source of truth; style through `--tp-code-*` variables and `::part()`.
65
+ * - **Async**: plain text renders immediately and is swapped for highlighted
66
+ * tokens once the grammar loads &mdash; same line layout, no shift.
67
+ *
68
+ * @element tp-code
69
+ *
70
+ * @attr {"inline" | "block"} [variant="inline"] - Layout.
71
+ * @attr {string} [language=""] - Shiki language id or alias (`ts`, `html`, `sh`, …). Empty, `text`, or unknown → plain text.
72
+ * @attr {boolean} [line-numbers=false] - Block only. Shows line numbers.
73
+ * @attr {number} [line-start=1] - Block only. Number of the first line.
74
+ * @attr {string} [highlight-lines=""] - Block only. Lines to emphasize, e.g. `"2, 4-6"` (in displayed line numbers).
75
+ * @attr {boolean} [copyable=false] - Block only. Shows a copy-to-clipboard button.
76
+ *
77
+ * @fires tp-code-highlight - After each highlight pass. `detail: { language, highlighted }`.
78
+ * @fires tp-code-copy - After the copy button is used. `detail: { code, success }`.
79
+ *
80
+ * @csspart pre - Block wrapper (`<pre>`).
81
+ * @csspart code - The `<code>` element (both variants).
82
+ * @csspart line - Every line (block).
83
+ * @csspart highlighted-line - Lines matched by `highlight-lines`.
84
+ * @csspart copy-button - The copy button.
85
+ *
86
+ * @cssprop --tp-code-foreground - Text color. Defaults to `--tp-color-contrast`.
87
+ * @cssprop --tp-code-background - Surface color. Defaults to a 6% tint of the foreground.
88
+ * @cssprop --tp-code-font-family - Monospace font stack.
89
+ * @cssprop --tp-code-font-size - Defaults to `0.875em`.
90
+ * @cssprop --tp-code-line-height - Block line height. Defaults to `1.6`.
91
+ * @cssprop --tp-code-tab-size - Defaults to `2`.
92
+ * @cssprop --tp-code-radius - Corner radius (`0.3em` inline, `0.5em` block).
93
+ * @cssprop --tp-code-inline-padding - Inline padding. Defaults to `0.1em 0.35em`.
94
+ * @cssprop --tp-code-padding-block - Block vertical padding. Defaults to `1em`.
95
+ * @cssprop --tp-code-padding-inline - Block horizontal padding per line. Defaults to `1em`.
96
+ * @cssprop --tp-code-accent-strength - How much of the palette color token defaults keep vs. the foreground. Defaults to `80%`.
97
+ * @cssprop --tp-code-token-keyword - Keywords. Defaults from `--tp-color-secondary`.
98
+ * @cssprop --tp-code-token-function - Functions. Defaults from `--tp-color-primary`.
99
+ * @cssprop --tp-code-token-constant - Constants, numbers, variables.
100
+ * @cssprop --tp-code-token-string - Strings. Defaults from `--tp-color-success`.
101
+ * @cssprop --tp-code-token-string-expression - Template/string expressions.
102
+ * @cssprop --tp-code-token-parameter - Parameters. Defaults from `--tp-color-warning`.
103
+ * @cssprop --tp-code-token-comment - Comments.
104
+ * @cssprop --tp-code-token-punctuation - Punctuation.
105
+ * @cssprop --tp-code-token-link - Links.
106
+ * @cssprop --tp-code-token-inserted - Diff insertions.
107
+ * @cssprop --tp-code-token-deleted - Diff deletions. Defaults from `--tp-color-danger`.
108
+ * @cssprop --tp-code-token-changed - Diff changes.
109
+ * @cssprop --tp-code-ansi-black - ANSI colors (also `-red`, `-green`, `-yellow`, `-blue`, `-magenta`, `-cyan`, `-white` and their `-bright-*` variants) for `ansi` code.
110
+ * @cssprop --tp-code-line-number-color - Line number color.
111
+ * @cssprop --tp-code-line-number-gap - Space between line numbers and code. Defaults to `1.5em`.
112
+ * @cssprop --tp-code-line-highlight-background - Highlighted line background. Defaults from `--tp-color-primary`.
113
+ * @cssprop --tp-code-line-highlight-border-color - Highlighted line edge marker.
114
+ * @cssprop --tp-code-copy-background - Copy button background. Defaults to `Canvas`.
115
+ * @cssprop --tp-code-copy-color - Copy button icon color.
116
+ * @cssprop --tp-code-copy-success-color - Copy button color after a successful copy.
117
+ */
118
+ declare class CodeElement extends LitElement {
119
+ #private;
120
+ static styles: any[];
121
+ /** Layout: `inline` (default) or `block`. */
122
+ variant: CodeVariant;
123
+ /** Shiki language id or alias. Empty/`text`/unknown renders plain text. */
124
+ language: string;
125
+ /** Block only. Shows line numbers. */
126
+ lineNumbers: boolean;
127
+ /** Block only. Number of the first line. */
128
+ lineStart: number;
129
+ /** Block only. Lines to emphasize, e.g. `"2, 4-6"`. */
130
+ highlightLines: string;
131
+ /** Block only. Shows a copy-to-clipboard button. */
132
+ copyable: boolean;
133
+ /**
134
+ * Source code. When set, it overrides the light-DOM content; set it back to
135
+ * `undefined` to fall back to the children again.
136
+ */
137
+ code?: string;
138
+ /** Raw source read from `code` or the light DOM. */
139
+ private rawSource;
140
+ private tokens?;
141
+ private copied;
142
+ /** The normalized source being displayed (dedented for blocks, trimmed inline). */
143
+ get source(): string;
144
+ /** Resolves once the current highlight pass (if any) has rendered. */
145
+ get highlightComplete(): Promise<void>;
146
+ connectedCallback(): void;
147
+ disconnectedCallback(): void;
148
+ protected willUpdate(changed: PropertyValues<this>): void;
149
+ protected render(): unknown;
150
+ /**
151
+ * Copies the displayed source to the clipboard and fires `tp-code-copy`.
152
+ *
153
+ * @returns Whether the copy succeeded.
154
+ */
155
+ copy(): Promise<boolean>;
156
+ }
157
+
158
+ /**
159
+ * Resolves a language id or alias (already trimmed and lower-cased) to the
160
+ * Shiki grammar registrations it needs. The **last** registration is the
161
+ * requested language itself; earlier ones are its embedded dependencies.
162
+ *
163
+ * Return `undefined` (or reject) for unknown languages &mdash; the element then
164
+ * renders plain text.
165
+ */
166
+ type LanguageLoader = (name: string) => Promise<readonly LanguageRegistration[] | undefined>;
167
+ /**
168
+ * Replaces the loader `<tp-code>` uses to fetch grammars.
169
+ *
170
+ * `@telperion/elements/code` installs a loader backed by Shiki's bundled,
171
+ * lazily imported grammars; the CDN build (`code/cdn`) installs one that
172
+ * imports grammars from jsDelivr. Call this before the first `<tp-code>`
173
+ * highlights &mdash; languages that already loaded are cached.
174
+ */
175
+ declare function setLanguageLoader(next: LanguageLoader): void;
176
+ /** The loader currently in use. */
177
+ declare function getLanguageLoader(): LanguageLoader;
178
+
179
+ /**
180
+ * Version of `@shikijs/langs` fetched from the CDN. Must match the installed
181
+ * `shiki` version (a spec enforces this).
182
+ */
183
+ declare const SHIKI_LANGS_VERSION = "4.6.0";
184
+ /**
185
+ * Points the CDN loader at another host serving `@shikijs/langs/dist/*.mjs`
186
+ * (e.g. a self-hosted mirror). Pass nothing to restore the jsDelivr default.
187
+ */
188
+ declare function setLanguageCdnBaseUrl(url?: string): void;
189
+ /** The URL a language module is fetched from. */
190
+ declare function languageCdnUrl(name: string): string;
191
+ /**
192
+ * Imports grammars at runtime from the CDN. Used by the self-contained IIFE
193
+ * build, which cannot code-split Shiki's bundled grammars. Every id and alias
194
+ * has its own module in `@shikijs/langs`, and embedded languages are imported
195
+ * relatively, so no alias table is needed here.
196
+ */
197
+ declare const cdnLanguageLoader: LanguageLoader;
198
+
199
+ export { SHIKI_LANGS_VERSION as S, CodeElement as a, cdnLanguageLoader as d, setLanguageLoader as e, getLanguageLoader as g, languageCdnUrl as l, setLanguageCdnBaseUrl as s };
200
+ export type { CodeCopyEventDetail as C, LanguageLoader as L, CodeHighlightEventDetail as b, CodeVariant as c };
@@ -0,0 +1,200 @@
1
+ import { LitElement, PropertyValues } from 'lit';
2
+ import { LanguageRegistration } from 'shiki/core';
3
+
4
+ /**
5
+ * Layout of `<tp-code>`.
6
+ *
7
+ * - `inline`: renders a single `<code>` that flows with surrounding text.
8
+ * - `block`: renders a `<pre><code>` block with optional line numbers,
9
+ * highlighted lines, and a copy button.
10
+ */
11
+ type CodeVariant = "inline" | "block";
12
+ /** `detail` of the `tp-code-highlight` event. */
13
+ interface CodeHighlightEventDetail {
14
+ /** The `language` attribute as requested by the author. */
15
+ language: string;
16
+ /** `false` when the language was unknown/plain and the code rendered as plain text. */
17
+ highlighted: boolean;
18
+ }
19
+ /** `detail` of the `tp-code-copy` event. */
20
+ interface CodeCopyEventDetail {
21
+ /** The (normalized) source that was copied. */
22
+ code: string;
23
+ /** Whether writing to the clipboard succeeded. */
24
+ success: boolean;
25
+ }
26
+
27
+ declare global {
28
+ interface HTMLElementTagNameMap {
29
+ /**
30
+ * Syntax-highlighted code, inline or as a block. Highlighting is powered by
31
+ * Shiki; every Shiki language loads lazily on first use.
32
+ *
33
+ * Source comes from the element's text content (or a `<template>` child, or
34
+ * the `code` property). Variants: `inline` (default) and `block` (with
35
+ * optional `line-numbers`, `highlight-lines`, and `copyable`). Colors are
36
+ * entirely `--tp-code-*` CSS variables.
37
+ *
38
+ * @example
39
+ * ```html
40
+ * <p>Run <tp-code language="sh">pnpm install</tp-code> first.</p>
41
+ *
42
+ * <tp-code variant="block" language="ts" line-numbers highlight-lines="2" copyable>
43
+ * const answer = 42;
44
+ * console.log(answer);
45
+ * </tp-code>
46
+ * ```
47
+ */
48
+ "tp-code": CodeElement;
49
+ }
50
+ interface HTMLElementEventMap {
51
+ "tp-code-highlight": CustomEvent<CodeHighlightEventDetail>;
52
+ "tp-code-copy": CustomEvent<CodeCopyEventDetail>;
53
+ }
54
+ }
55
+ /**
56
+ * `<tp-code>` &mdash; syntax-highlighted code powered by [Shiki](https://shiki.style).
57
+ *
58
+ * - **Source**: the element's text content, a direct `<template>` child (for
59
+ * HTML samples, no escaping needed), or the `code` property. Light-DOM
60
+ * changes are observed and re-highlighted.
61
+ * - **Languages**: any Shiki language id or alias via `language`, loaded lazily
62
+ * once per page. Unknown languages render as plain text.
63
+ * - **Rendering**: into shadow DOM, so the light DOM stays the untouched
64
+ * source of truth; style through `--tp-code-*` variables and `::part()`.
65
+ * - **Async**: plain text renders immediately and is swapped for highlighted
66
+ * tokens once the grammar loads &mdash; same line layout, no shift.
67
+ *
68
+ * @element tp-code
69
+ *
70
+ * @attr {"inline" | "block"} [variant="inline"] - Layout.
71
+ * @attr {string} [language=""] - Shiki language id or alias (`ts`, `html`, `sh`, …). Empty, `text`, or unknown → plain text.
72
+ * @attr {boolean} [line-numbers=false] - Block only. Shows line numbers.
73
+ * @attr {number} [line-start=1] - Block only. Number of the first line.
74
+ * @attr {string} [highlight-lines=""] - Block only. Lines to emphasize, e.g. `"2, 4-6"` (in displayed line numbers).
75
+ * @attr {boolean} [copyable=false] - Block only. Shows a copy-to-clipboard button.
76
+ *
77
+ * @fires tp-code-highlight - After each highlight pass. `detail: { language, highlighted }`.
78
+ * @fires tp-code-copy - After the copy button is used. `detail: { code, success }`.
79
+ *
80
+ * @csspart pre - Block wrapper (`<pre>`).
81
+ * @csspart code - The `<code>` element (both variants).
82
+ * @csspart line - Every line (block).
83
+ * @csspart highlighted-line - Lines matched by `highlight-lines`.
84
+ * @csspart copy-button - The copy button.
85
+ *
86
+ * @cssprop --tp-code-foreground - Text color. Defaults to `--tp-color-contrast`.
87
+ * @cssprop --tp-code-background - Surface color. Defaults to a 6% tint of the foreground.
88
+ * @cssprop --tp-code-font-family - Monospace font stack.
89
+ * @cssprop --tp-code-font-size - Defaults to `0.875em`.
90
+ * @cssprop --tp-code-line-height - Block line height. Defaults to `1.6`.
91
+ * @cssprop --tp-code-tab-size - Defaults to `2`.
92
+ * @cssprop --tp-code-radius - Corner radius (`0.3em` inline, `0.5em` block).
93
+ * @cssprop --tp-code-inline-padding - Inline padding. Defaults to `0.1em 0.35em`.
94
+ * @cssprop --tp-code-padding-block - Block vertical padding. Defaults to `1em`.
95
+ * @cssprop --tp-code-padding-inline - Block horizontal padding per line. Defaults to `1em`.
96
+ * @cssprop --tp-code-accent-strength - How much of the palette color token defaults keep vs. the foreground. Defaults to `80%`.
97
+ * @cssprop --tp-code-token-keyword - Keywords. Defaults from `--tp-color-secondary`.
98
+ * @cssprop --tp-code-token-function - Functions. Defaults from `--tp-color-primary`.
99
+ * @cssprop --tp-code-token-constant - Constants, numbers, variables.
100
+ * @cssprop --tp-code-token-string - Strings. Defaults from `--tp-color-success`.
101
+ * @cssprop --tp-code-token-string-expression - Template/string expressions.
102
+ * @cssprop --tp-code-token-parameter - Parameters. Defaults from `--tp-color-warning`.
103
+ * @cssprop --tp-code-token-comment - Comments.
104
+ * @cssprop --tp-code-token-punctuation - Punctuation.
105
+ * @cssprop --tp-code-token-link - Links.
106
+ * @cssprop --tp-code-token-inserted - Diff insertions.
107
+ * @cssprop --tp-code-token-deleted - Diff deletions. Defaults from `--tp-color-danger`.
108
+ * @cssprop --tp-code-token-changed - Diff changes.
109
+ * @cssprop --tp-code-ansi-black - ANSI colors (also `-red`, `-green`, `-yellow`, `-blue`, `-magenta`, `-cyan`, `-white` and their `-bright-*` variants) for `ansi` code.
110
+ * @cssprop --tp-code-line-number-color - Line number color.
111
+ * @cssprop --tp-code-line-number-gap - Space between line numbers and code. Defaults to `1.5em`.
112
+ * @cssprop --tp-code-line-highlight-background - Highlighted line background. Defaults from `--tp-color-primary`.
113
+ * @cssprop --tp-code-line-highlight-border-color - Highlighted line edge marker.
114
+ * @cssprop --tp-code-copy-background - Copy button background. Defaults to `Canvas`.
115
+ * @cssprop --tp-code-copy-color - Copy button icon color.
116
+ * @cssprop --tp-code-copy-success-color - Copy button color after a successful copy.
117
+ */
118
+ declare class CodeElement extends LitElement {
119
+ #private;
120
+ static styles: any[];
121
+ /** Layout: `inline` (default) or `block`. */
122
+ variant: CodeVariant;
123
+ /** Shiki language id or alias. Empty/`text`/unknown renders plain text. */
124
+ language: string;
125
+ /** Block only. Shows line numbers. */
126
+ lineNumbers: boolean;
127
+ /** Block only. Number of the first line. */
128
+ lineStart: number;
129
+ /** Block only. Lines to emphasize, e.g. `"2, 4-6"`. */
130
+ highlightLines: string;
131
+ /** Block only. Shows a copy-to-clipboard button. */
132
+ copyable: boolean;
133
+ /**
134
+ * Source code. When set, it overrides the light-DOM content; set it back to
135
+ * `undefined` to fall back to the children again.
136
+ */
137
+ code?: string;
138
+ /** Raw source read from `code` or the light DOM. */
139
+ private rawSource;
140
+ private tokens?;
141
+ private copied;
142
+ /** The normalized source being displayed (dedented for blocks, trimmed inline). */
143
+ get source(): string;
144
+ /** Resolves once the current highlight pass (if any) has rendered. */
145
+ get highlightComplete(): Promise<void>;
146
+ connectedCallback(): void;
147
+ disconnectedCallback(): void;
148
+ protected willUpdate(changed: PropertyValues<this>): void;
149
+ protected render(): unknown;
150
+ /**
151
+ * Copies the displayed source to the clipboard and fires `tp-code-copy`.
152
+ *
153
+ * @returns Whether the copy succeeded.
154
+ */
155
+ copy(): Promise<boolean>;
156
+ }
157
+
158
+ /**
159
+ * Resolves a language id or alias (already trimmed and lower-cased) to the
160
+ * Shiki grammar registrations it needs. The **last** registration is the
161
+ * requested language itself; earlier ones are its embedded dependencies.
162
+ *
163
+ * Return `undefined` (or reject) for unknown languages &mdash; the element then
164
+ * renders plain text.
165
+ */
166
+ type LanguageLoader = (name: string) => Promise<readonly LanguageRegistration[] | undefined>;
167
+ /**
168
+ * Replaces the loader `<tp-code>` uses to fetch grammars.
169
+ *
170
+ * `@telperion/elements/code` installs a loader backed by Shiki's bundled,
171
+ * lazily imported grammars; the CDN build (`code/cdn`) installs one that
172
+ * imports grammars from jsDelivr. Call this before the first `<tp-code>`
173
+ * highlights &mdash; languages that already loaded are cached.
174
+ */
175
+ declare function setLanguageLoader(next: LanguageLoader): void;
176
+ /** The loader currently in use. */
177
+ declare function getLanguageLoader(): LanguageLoader;
178
+
179
+ /**
180
+ * Version of `@shikijs/langs` fetched from the CDN. Must match the installed
181
+ * `shiki` version (a spec enforces this).
182
+ */
183
+ declare const SHIKI_LANGS_VERSION = "4.6.0";
184
+ /**
185
+ * Points the CDN loader at another host serving `@shikijs/langs/dist/*.mjs`
186
+ * (e.g. a self-hosted mirror). Pass nothing to restore the jsDelivr default.
187
+ */
188
+ declare function setLanguageCdnBaseUrl(url?: string): void;
189
+ /** The URL a language module is fetched from. */
190
+ declare function languageCdnUrl(name: string): string;
191
+ /**
192
+ * Imports grammars at runtime from the CDN. Used by the self-contained IIFE
193
+ * build, which cannot code-split Shiki's bundled grammars. Every id and alias
194
+ * has its own module in `@shikijs/langs`, and embedded languages are imported
195
+ * relatively, so no alias table is needed here.
196
+ */
197
+ declare const cdnLanguageLoader: LanguageLoader;
198
+
199
+ export { SHIKI_LANGS_VERSION as S, CodeElement as a, cdnLanguageLoader as d, setLanguageLoader as e, getLanguageLoader as g, languageCdnUrl as l, setLanguageCdnBaseUrl as s };
200
+ export type { CodeCopyEventDetail as C, LanguageLoader as L, CodeHighlightEventDetail as b, CodeVariant as c };
@@ -0,0 +1,18 @@
1
+ 'use strict';
2
+
3
+ var langs = require('shiki/langs');
4
+
5
+ /**
6
+ * Loads any Shiki-bundled language (ids and aliases) through its own dynamic
7
+ * `import()`, so bundlers split every grammar into a separate lazy chunk and
8
+ * consumers only download the languages they actually render.
9
+ */
10
+ const bundledLanguageLoader = async (name) => {
11
+ if (!Object.hasOwn(langs.bundledLanguages, name))
12
+ return undefined;
13
+ const module = await langs.bundledLanguages[name]();
14
+ return module.default;
15
+ };
16
+
17
+ exports.bundledLanguageLoader = bundledLanguageLoader;
18
+ //# sourceMappingURL=bundled-language-loader.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bundled-language-loader.cjs","sources":["../../../../libs/elements/src/code/bundled-language-loader.ts"],"sourcesContent":[null],"names":["bundledLanguages"],"mappings":";;;;AAIA;;;;AAIG;MACU,qBAAqB,GAAmB,OAAM,IAAI,KAAG;IAChE,IAAI,CAAC,MAAM,CAAC,MAAM,CAACA,sBAAgB,EAAE,IAAI,CAAC;AAAE,QAAA,OAAO,SAAS;IAE5D,MAAM,MAAM,GAAG,MAAMA,sBAAgB,CAAC,IAAqC,CAAC,EAAE;IAE9E,OAAO,MAAM,CAAC,OAAO;AACvB;;;;"}
@@ -0,0 +1,16 @@
1
+ import { bundledLanguages } from 'shiki/langs';
2
+
3
+ /**
4
+ * Loads any Shiki-bundled language (ids and aliases) through its own dynamic
5
+ * `import()`, so bundlers split every grammar into a separate lazy chunk and
6
+ * consumers only download the languages they actually render.
7
+ */
8
+ const bundledLanguageLoader = async (name) => {
9
+ if (!Object.hasOwn(bundledLanguages, name))
10
+ return undefined;
11
+ const module = await bundledLanguages[name]();
12
+ return module.default;
13
+ };
14
+
15
+ export { bundledLanguageLoader };
16
+ //# sourceMappingURL=bundled-language-loader.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bundled-language-loader.js","sources":["../../../../libs/elements/src/code/bundled-language-loader.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAIA;;;;AAIG;MACU,qBAAqB,GAAmB,OAAM,IAAI,KAAG;IAChE,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,gBAAgB,EAAE,IAAI,CAAC;AAAE,QAAA,OAAO,SAAS;IAE5D,MAAM,MAAM,GAAG,MAAM,gBAAgB,CAAC,IAAqC,CAAC,EAAE;IAE9E,OAAO,MAAM,CAAC,OAAO;AACvB;;;;"}
@@ -0,0 +1,41 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Version of `@shikijs/langs` fetched from the CDN. Must match the installed
5
+ * `shiki` version (a spec enforces this).
6
+ */
7
+ const SHIKI_LANGS_VERSION = "4.6.0";
8
+ const DEFAULT_BASE_URL = `https://cdn.jsdelivr.net/npm/@shikijs/langs@${SHIKI_LANGS_VERSION}/dist/`;
9
+ /** Language ids/aliases are lowercase words with digits and dashes; anything else is rejected. */
10
+ const SAFE_NAME = /^[a-z0-9][a-z0-9-]*$/;
11
+ let baseUrl = DEFAULT_BASE_URL;
12
+ /**
13
+ * Points the CDN loader at another host serving `@shikijs/langs/dist/*.mjs`
14
+ * (e.g. a self-hosted mirror). Pass nothing to restore the jsDelivr default.
15
+ */
16
+ function setLanguageCdnBaseUrl(url = DEFAULT_BASE_URL) {
17
+ baseUrl = url.endsWith("/") ? url : `${url}/`;
18
+ }
19
+ /** The URL a language module is fetched from. */
20
+ function languageCdnUrl(name) {
21
+ return `${baseUrl}${name}.mjs`;
22
+ }
23
+ /**
24
+ * Imports grammars at runtime from the CDN. Used by the self-contained IIFE
25
+ * build, which cannot code-split Shiki's bundled grammars. Every id and alias
26
+ * has its own module in `@shikijs/langs`, and embedded languages are imported
27
+ * relatively, so no alias table is needed here.
28
+ */
29
+ const cdnLanguageLoader = async (name) => {
30
+ if (!SAFE_NAME.test(name))
31
+ return undefined;
32
+ const module = await import(
33
+ /* @vite-ignore */ /* webpackIgnore: true */ languageCdnUrl(name));
34
+ return module.default;
35
+ };
36
+
37
+ exports.SHIKI_LANGS_VERSION = SHIKI_LANGS_VERSION;
38
+ exports.cdnLanguageLoader = cdnLanguageLoader;
39
+ exports.languageCdnUrl = languageCdnUrl;
40
+ exports.setLanguageCdnBaseUrl = setLanguageCdnBaseUrl;
41
+ //# sourceMappingURL=cdn-language-loader.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cdn-language-loader.cjs","sources":["../../../../libs/elements/src/code/cdn-language-loader.ts"],"sourcesContent":[null],"names":[],"mappings":";;AAIA;;;AAGG;AACI,MAAM,mBAAmB,GAAG;AAEnC,MAAM,gBAAgB,GAAG,CAAA,4CAAA,EAA+C,mBAAmB,QAAQ;AACnG;AACA,MAAM,SAAS,GAAG,sBAAsB;AACxC,IAAI,OAAO,GAAG,gBAAgB;AAE9B;;;AAGG;AACG,SAAU,qBAAqB,CAAC,GAAA,GAAc,gBAAgB,EAAA;AAClE,IAAA,OAAO,GAAG,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,GAAG,GAAG,CAAA,EAAG,GAAG,GAAG;AAC/C;AAEA;AACM,SAAU,cAAc,CAAC,IAAY,EAAA;AACzC,IAAA,OAAO,CAAA,EAAG,OAAO,CAAA,EAAG,IAAI,MAAM;AAChC;AAEA;;;;;AAKG;MACU,iBAAiB,GAAmB,OAAM,IAAI,KAAG;AAC5D,IAAA,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC;AAAE,QAAA,OAAO,SAAS;IAE3C,MAAM,MAAM,GAAwC,MAAM;iDACX,cAAc,CAAC,IAAI,CAAC,CAClE;IAED,OAAO,MAAM,CAAC,OAAO;AACvB;;;;;;;"}
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Version of `@shikijs/langs` fetched from the CDN. Must match the installed
3
+ * `shiki` version (a spec enforces this).
4
+ */
5
+ const SHIKI_LANGS_VERSION = "4.6.0";
6
+ const DEFAULT_BASE_URL = `https://cdn.jsdelivr.net/npm/@shikijs/langs@${SHIKI_LANGS_VERSION}/dist/`;
7
+ /** Language ids/aliases are lowercase words with digits and dashes; anything else is rejected. */
8
+ const SAFE_NAME = /^[a-z0-9][a-z0-9-]*$/;
9
+ let baseUrl = DEFAULT_BASE_URL;
10
+ /**
11
+ * Points the CDN loader at another host serving `@shikijs/langs/dist/*.mjs`
12
+ * (e.g. a self-hosted mirror). Pass nothing to restore the jsDelivr default.
13
+ */
14
+ function setLanguageCdnBaseUrl(url = DEFAULT_BASE_URL) {
15
+ baseUrl = url.endsWith("/") ? url : `${url}/`;
16
+ }
17
+ /** The URL a language module is fetched from. */
18
+ function languageCdnUrl(name) {
19
+ return `${baseUrl}${name}.mjs`;
20
+ }
21
+ /**
22
+ * Imports grammars at runtime from the CDN. Used by the self-contained IIFE
23
+ * build, which cannot code-split Shiki's bundled grammars. Every id and alias
24
+ * has its own module in `@shikijs/langs`, and embedded languages are imported
25
+ * relatively, so no alias table is needed here.
26
+ */
27
+ const cdnLanguageLoader = async (name) => {
28
+ if (!SAFE_NAME.test(name))
29
+ return undefined;
30
+ const module = await import(
31
+ /* @vite-ignore */ /* webpackIgnore: true */ languageCdnUrl(name));
32
+ return module.default;
33
+ };
34
+
35
+ export { SHIKI_LANGS_VERSION, cdnLanguageLoader, languageCdnUrl, setLanguageCdnBaseUrl };
36
+ //# sourceMappingURL=cdn-language-loader.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cdn-language-loader.js","sources":["../../../../libs/elements/src/code/cdn-language-loader.ts"],"sourcesContent":[null],"names":[],"mappings":"AAIA;;;AAGG;AACI,MAAM,mBAAmB,GAAG;AAEnC,MAAM,gBAAgB,GAAG,CAAA,4CAAA,EAA+C,mBAAmB,QAAQ;AACnG;AACA,MAAM,SAAS,GAAG,sBAAsB;AACxC,IAAI,OAAO,GAAG,gBAAgB;AAE9B;;;AAGG;AACG,SAAU,qBAAqB,CAAC,GAAA,GAAc,gBAAgB,EAAA;AAClE,IAAA,OAAO,GAAG,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,GAAG,GAAG,CAAA,EAAG,GAAG,GAAG;AAC/C;AAEA;AACM,SAAU,cAAc,CAAC,IAAY,EAAA;AACzC,IAAA,OAAO,CAAA,EAAG,OAAO,CAAA,EAAG,IAAI,MAAM;AAChC;AAEA;;;;;AAKG;MACU,iBAAiB,GAAmB,OAAM,IAAI,KAAG;AAC5D,IAAA,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,CAAC;AAAE,QAAA,OAAO,SAAS;IAE3C,MAAM,MAAM,GAAwC,MAAM;iDACX,cAAc,CAAC,IAAI,CAAC,CAClE;IAED,OAAO,MAAM,CAAC,OAAO;AACvB;;;;"}
package/code/cdn.cjs ADDED
@@ -0,0 +1,21 @@
1
+ 'use strict';
2
+
3
+ var cdnLanguageLoader = require('./cdn-language-loader.cjs');
4
+ var codeElementBase = require('./code-element-base.cjs');
5
+ var languageLoader = require('./language-loader.cjs');
6
+
7
+ /**
8
+ * CDN entry for `<tp-code>` (used by the self-contained IIFE build): grammars
9
+ * are imported at runtime from jsDelivr instead of being bundled, since an
10
+ * IIFE cannot code-split. Point it elsewhere with `setLanguageCdnBaseUrl()`.
11
+ */
12
+ languageLoader.setLanguageLoader(cdnLanguageLoader.cdnLanguageLoader);
13
+ if (!customElements.get("tp-code"))
14
+ customElements.define("tp-code", codeElementBase.CodeElement);
15
+
16
+ exports.SHIKI_LANGS_VERSION = cdnLanguageLoader.SHIKI_LANGS_VERSION;
17
+ exports.languageCdnUrl = cdnLanguageLoader.languageCdnUrl;
18
+ exports.setLanguageCdnBaseUrl = cdnLanguageLoader.setLanguageCdnBaseUrl;
19
+ exports.CodeElement = codeElementBase.CodeElement;
20
+ exports.setLanguageLoader = languageLoader.setLanguageLoader;
21
+ //# sourceMappingURL=cdn.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cdn.cjs","sources":["../../../../libs/elements/src/code/code-cdn-element.ts"],"sourcesContent":[null],"names":["setLanguageLoader","cdnLanguageLoader","CodeElement"],"mappings":";;;;;;AAIA;;;;AAIG;AACHA,gCAAiB,CAACC,mCAAiB,CAAC;AAEpC,IAAI,CAAC,cAAc,CAAC,GAAG,CAAC,SAAS,CAAC;AAAE,IAAA,cAAc,CAAC,MAAM,CAAC,SAAS,EAAEC,2BAAW,CAAC;;;;;;;;"}
package/code/cdn.d.cts ADDED
@@ -0,0 +1,3 @@
1
+ export { C as CodeCopyEventDetail, a as CodeElement, b as CodeHighlightEventDetail, c as CodeVariant, L as LanguageLoader, S as SHIKI_LANGS_VERSION, l as languageCdnUrl, s as setLanguageCdnBaseUrl, e as setLanguageLoader } from '../_chunks/code-cdn-element-ClAO2lep.cjs';
2
+ import 'lit';
3
+ import 'shiki/core';
package/code/cdn.d.ts ADDED
@@ -0,0 +1,3 @@
1
+ export { C as CodeCopyEventDetail, a as CodeElement, b as CodeHighlightEventDetail, c as CodeVariant, L as LanguageLoader, S as SHIKI_LANGS_VERSION, l as languageCdnUrl, s as setLanguageCdnBaseUrl, e as setLanguageLoader } from '../_chunks/code-cdn-element-ClAO2lep.js';
2
+ import 'lit';
3
+ import 'shiki/core';