svelte-highlight 7.14.0 → 7.15.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 (46) hide show
  1. package/AnsiOutput.svelte +22 -3
  2. package/CopyButton.svelte +10 -2
  3. package/CopyButton.svelte.d.ts +8 -1
  4. package/FileTabs.svelte +2 -1
  5. package/Highlight.svelte +26 -15
  6. package/HighlightAuto.svelte +25 -20
  7. package/HighlightEditable.svelte +434 -43
  8. package/HighlightEditable.svelte.d.ts +31 -0
  9. package/HighlightStream.svelte +154 -0
  10. package/HighlightStream.svelte.d.ts +90 -0
  11. package/HighlightStyle.svelte +70 -5
  12. package/HighlightSvelte.svelte +24 -15
  13. package/LineNumbers.svelte +4 -2
  14. package/README.md +119 -3
  15. package/Typewriter.svelte +111 -58
  16. package/action.js +29 -3
  17. package/ansi.d.ts +9 -0
  18. package/ansi.js +112 -4
  19. package/copy-transforms.d.ts +13 -0
  20. package/copy-transforms.js +41 -0
  21. package/highlight-theme.d.ts +7 -0
  22. package/highlight-theme.js +176 -0
  23. package/index.d.ts +8 -0
  24. package/index.js +8 -0
  25. package/languages/astro.js +6 -0
  26. package/languages/hcl.js +4 -4
  27. package/languages/hlsl.js +6 -2
  28. package/languages/jq.js +2 -2
  29. package/languages/just.js +1 -1
  30. package/languages/kql.js +1 -0
  31. package/languages/rescript.js +3 -2
  32. package/languages/solidity.js +1 -1
  33. package/languages/svelte.js +6 -5
  34. package/languages/wgsl.js +32 -6
  35. package/languages/zig.js +2 -2
  36. package/package.json +19 -3
  37. package/split-lines.d.ts +6 -0
  38. package/split-lines.js +50 -0
  39. package/text-diff.d.ts +8 -0
  40. package/text-diff.js +46 -0
  41. package/token-ranges.d.ts +18 -0
  42. package/token-ranges.js +112 -0
  43. package/typewriter-units.d.ts +13 -0
  44. package/typewriter-units.js +73 -0
  45. package/core.js +0 -22
  46. package/highlighter.svelte.js +0 -22
@@ -0,0 +1,112 @@
1
+ /**
2
+ * Tokenize code into flat, absolute-offset scope ranges instead of HTML, via
3
+ * hljs's `__emitter` hook. Used by the `HighlightEditable` "css-highlights"
4
+ * engine, which paints tokens as `CSS.highlights` ranges over plain-text
5
+ * nodes rather than wrapping them in `<span>`s.
6
+ */
7
+
8
+ import hljs from "highlight.js/lib/core";
9
+
10
+ /** @typedef {import("highlight.js").Emitter} Emitter */
11
+ /** @typedef {new (options: unknown) => Emitter} EmitterConstructor */
12
+
13
+ // Nested `startScope` calls stack up (e.g. a keyword inside a sublanguage
14
+ // block); a run of text takes the innermost (top-of-stack) scope, matching
15
+ // how nested `<span>`s visually resolve to the last-applied color in the
16
+ // class-based renderer.
17
+ /** @implements {Emitter} */
18
+ class RangeEmitter {
19
+ /** @param {unknown} options */
20
+ constructor(options) {
21
+ this.options = options;
22
+ this.pos = 0;
23
+ /** @type {string[]} */
24
+ this.stack = [];
25
+ /** @type {{ start: number; end: number; scope: string }[]} */
26
+ this.ranges = [];
27
+ }
28
+
29
+ /** @param {string} text */
30
+ addText(text) {
31
+ if (text.length === 0) return;
32
+ const scope = this.stack[this.stack.length - 1];
33
+ if (scope !== undefined) {
34
+ this.ranges.push({ start: this.pos, end: this.pos + text.length, scope });
35
+ }
36
+ this.pos += text.length;
37
+ }
38
+
39
+ /** @param {string} scope */
40
+ startScope(scope) {
41
+ this.stack.push(scope);
42
+ }
43
+
44
+ endScope() {
45
+ this.stack.pop();
46
+ }
47
+
48
+ // Grammar modes (as opposed to keyword matches) open/close scopes through
49
+ // this pair instead of startScope/endScope; same bookkeeping either way.
50
+ /** @param {string} scope */
51
+ openNode(scope) {
52
+ this.startScope(scope);
53
+ }
54
+
55
+ closeNode() {
56
+ this.endScope();
57
+ }
58
+
59
+ // Sublanguage blocks are parsed by a fresh emitter of this same class,
60
+ // offsets relative to that block; splice its ranges in at our position.
61
+ /** @param {RangeEmitter} emitter */
62
+ __addSublanguage(emitter) {
63
+ const base = this.pos;
64
+ for (const range of emitter.ranges) {
65
+ this.ranges.push({
66
+ start: base + range.start,
67
+ end: base + range.end,
68
+ scope: range.scope,
69
+ });
70
+ }
71
+ this.pos += emitter.pos;
72
+ }
73
+
74
+ // Matches hljs's `Emitter.toHTML` interface; unused since we return ranges.
75
+ // biome-ignore lint/style/useNamingConvention: interface-mandated name
76
+ toHTML() {
77
+ return "";
78
+ }
79
+
80
+ finalize() {}
81
+ }
82
+
83
+ // hljs only exposes `__emitter` via the shared, module-level `configure()`
84
+ // call (not per-call options), so the default emitter class is captured
85
+ // lazily off a real highlight and restored synchronously after ours runs.
86
+ /** @type {EmitterConstructor} */
87
+ let defaultEmitter;
88
+
89
+ /**
90
+ * @param {string} code
91
+ * @param {string} languageName A language already registered with hljs.
92
+ * @returns {{ start: number; end: number; scope: string }[]} Ranges sorted
93
+ * by `start`, offsets into `code`.
94
+ */
95
+ export function tokenize(code, languageName) {
96
+ if (defaultEmitter === undefined) {
97
+ const probe = hljs.highlight("", { language: languageName });
98
+ defaultEmitter = /** @type {EmitterConstructor} */ (
99
+ /** @type {unknown} */ (probe._emitter.constructor)
100
+ );
101
+ }
102
+
103
+ hljs.configure({ __emitter: RangeEmitter });
104
+ try {
105
+ const result = hljs.highlight(code, { language: languageName });
106
+ return /** @type {RangeEmitter} */ (
107
+ /** @type {unknown} */ (result._emitter)
108
+ ).ranges;
109
+ } finally {
110
+ hljs.configure({ __emitter: defaultEmitter });
111
+ }
112
+ }
@@ -0,0 +1,13 @@
1
+ export interface TypewriterUnit {
2
+ raw: string;
3
+ visible: 0 | 1;
4
+ kind?: "open" | "close" | "self";
5
+ name?: string;
6
+ }
7
+
8
+ /**
9
+ * Splits highlight.js output HTML into typing units: HTML tags carry zero
10
+ * visible chars and are never split; text is grouped one visible char per
11
+ * unit (a surrogate pair or an HTML entity counts as a single char).
12
+ */
13
+ export declare function tokenizeTypewriter(html: string): TypewriterUnit[];
@@ -0,0 +1,73 @@
1
+ /** Opening or closing tag name. */
2
+ const TAG_NAME = /^<\/?\s*([a-zA-Z0-9-]+)/;
3
+
4
+ /**
5
+ * @typedef {Object} TypewriterUnit
6
+ * @property {string} raw
7
+ * @property {0 | 1} visible
8
+ * @property {"open" | "close" | "self"} [kind]
9
+ * @property {string} [name]
10
+ */
11
+
12
+ /**
13
+ * Splits highlight.js output HTML into typing units for `Typewriter`. A unit
14
+ * is either an HTML tag (zero visible chars, never split) or one visible
15
+ * character: a plain character, a full surrogate pair (so a 4-byte emoji is
16
+ * never split mid-codepoint), or a single HTML entity such as `&amp;` (one
17
+ * visible char across several raw bytes).
18
+ * @param {string} html
19
+ * @returns {TypewriterUnit[]}
20
+ */
21
+ export function tokenizeTypewriter(html) {
22
+ /** @type {TypewriterUnit[]} */
23
+ const units = [];
24
+ const n = html.length;
25
+ let i = 0;
26
+
27
+ while (i < n) {
28
+ const ch = html[i];
29
+
30
+ if (ch === "<") {
31
+ const end = html.indexOf(">", i);
32
+ if (end === -1) {
33
+ // Unclosed tag: treat as text.
34
+ units.push({ raw: html.slice(i), visible: 1 });
35
+ break;
36
+ }
37
+ const raw = html.slice(i, end + 1);
38
+ const kind =
39
+ raw[1] === "/"
40
+ ? "close"
41
+ : raw[raw.length - 2] === "/"
42
+ ? "self"
43
+ : "open";
44
+ const match = TAG_NAME.exec(raw);
45
+ units.push({
46
+ raw,
47
+ visible: 0,
48
+ kind,
49
+ name: match ? (match[1] ?? "") : "",
50
+ });
51
+ i = end + 1;
52
+ } else if (ch === "&") {
53
+ // One visible char per entity.
54
+ const end = html.indexOf(";", i);
55
+ if (end !== -1 && end - i <= 10) {
56
+ units.push({ raw: html.slice(i, end + 1), visible: 1 });
57
+ i = end + 1;
58
+ } else {
59
+ units.push({ raw: ch, visible: 1 });
60
+ i += 1;
61
+ }
62
+ } else {
63
+ // A surrogate pair (e.g. most emoji) is one codepoint, one unit --
64
+ // splitting it across two ticks would render an orphan half.
65
+ const codePoint = html.codePointAt(i) ?? 0;
66
+ const length = codePoint > 0xffff ? 2 : 1;
67
+ units.push({ raw: html.slice(i, i + length), visible: 1 });
68
+ i += length;
69
+ }
70
+ }
71
+
72
+ return units;
73
+ }
package/core.js DELETED
@@ -1,22 +0,0 @@
1
- import hljs from "highlight.js/lib/core";
2
-
3
- /**
4
- * Register a highlight.js language if it hasn't been registered yet.
5
- * @param {import("./languages").LanguageType<string>} language
6
- */
7
- export function registerLanguage(language) {
8
- if (!hljs.getLanguage(language.name)) {
9
- hljs.registerLanguage(language.name, language.register);
10
- }
11
- }
12
-
13
- /**
14
- * Register (if needed) and highlight `code` with the given language.
15
- * @param {import("./languages").LanguageType<string>} language
16
- * @param {any} code
17
- * @returns {string}
18
- */
19
- export function highlightLanguage(language, code) {
20
- registerLanguage(language);
21
- return hljs.highlight(code, { language: language.name }).value;
22
- }
@@ -1,22 +0,0 @@
1
- /**
2
- * Shared reactive core behind `Highlight`, `HighlightAuto`, and
3
- * `HighlightSvelte`: derives a result from `compute` whenever its
4
- * dependencies change, and notifies `onHighlight` when that result changes.
5
- *
6
- * @template T
7
- * @param {() => T} compute
8
- * @param {(result: T) => void} [onHighlight]
9
- */
10
- export function createHighlighter(compute, onHighlight) {
11
- const result = $derived.by(compute);
12
-
13
- $effect(() => {
14
- onHighlight?.(result);
15
- });
16
-
17
- return {
18
- get value() {
19
- return result;
20
- },
21
- };
22
- }