svelte-highlight 7.23.2 → 7.24.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/AnsiOutput.svelte +33 -6
  2. package/AnsiOutput.svelte.d.ts +8 -0
  3. package/CodeToolbar.svelte +144 -0
  4. package/CodeToolbar.svelte.d.ts +125 -0
  5. package/CodeWindow.svelte +19 -0
  6. package/CodeWindow.svelte.d.ts +10 -0
  7. package/CopyButton.svelte +107 -3
  8. package/CopyButton.svelte.d.ts +17 -1
  9. package/DiffFileList.svelte +523 -0
  10. package/DiffFileList.svelte.d.ts +94 -0
  11. package/DiffFileListItem.svelte +44 -0
  12. package/DiffMinimap.svelte +99 -0
  13. package/DiffMinimap.svelte.d.ts +17 -0
  14. package/DiffStats.svelte +43 -0
  15. package/DiffStats.svelte.d.ts +17 -0
  16. package/DiffView.svelte +1213 -0
  17. package/DiffView.svelte.d.ts +76 -0
  18. package/FileTabs.svelte +2 -5
  19. package/Highlight.svelte +158 -2
  20. package/Highlight.svelte.d.ts +50 -2
  21. package/HighlightAuto.svelte.d.ts +3 -2
  22. package/HighlightAutoLazy.svelte +112 -0
  23. package/HighlightAutoLazy.svelte.d.ts +66 -0
  24. package/HighlightDiff.svelte +162 -0
  25. package/HighlightDiff.svelte.d.ts +102 -0
  26. package/HighlightEditable.svelte +65 -756
  27. package/HighlightEditable.svelte.d.ts +15 -5
  28. package/HighlightStream.svelte +117 -218
  29. package/HighlightStream.svelte.d.ts +8 -0
  30. package/HighlightSvelte.svelte.d.ts +3 -2
  31. package/HighlightVirtual.svelte +41 -8
  32. package/HighlightVirtual.svelte.d.ts +8 -0
  33. package/LangTag.svelte +33 -4
  34. package/LangTag.svelte.d.ts +6 -2
  35. package/LineNumbers.svelte +25 -4
  36. package/LineNumbers.svelte.d.ts +8 -0
  37. package/MarkdownStream.svelte +1 -0
  38. package/README.md +735 -18
  39. package/ToolbarButton.svelte +90 -0
  40. package/ToolbarButton.svelte.d.ts +106 -0
  41. package/Typewriter.svelte +59 -10
  42. package/Typewriter.svelte.d.ts +7 -0
  43. package/WrapToggle.svelte +34 -0
  44. package/WrapToggle.svelte.d.ts +39 -0
  45. package/action.js +7 -3
  46. package/ansi.d.ts +1 -3
  47. package/auto-detect.d.ts +52 -0
  48. package/auto-detect.js +208 -0
  49. package/code-region.d.ts +7 -0
  50. package/code-region.js +20 -0
  51. package/css-highlights.js +115 -0
  52. package/css-walk.js +50 -0
  53. package/diff-controller.d.ts +120 -0
  54. package/diff-controller.js +721 -0
  55. package/diff-edits.d.ts +101 -0
  56. package/diff-edits.js +890 -0
  57. package/diff-html.d.ts +8 -0
  58. package/diff-html.js +60 -0
  59. package/diff-languages.d.ts +4 -0
  60. package/diff-languages.js +73 -0
  61. package/diff.d.ts +161 -0
  62. package/diff.js +1111 -0
  63. package/editable-commands.js +81 -0
  64. package/editable-css-paint.js +5 -3
  65. package/editable-dom-paint.js +1 -1
  66. package/editable-history.js +199 -0
  67. package/editable-line-dom.js +176 -1
  68. package/editable-painter.js +356 -0
  69. package/editable.d.ts +118 -0
  70. package/editable.js +513 -0
  71. package/engine.d.ts +1 -1
  72. package/fence.js +1 -1
  73. package/highlight-cascade.js +679 -0
  74. package/highlight-theme.js +6 -47
  75. package/incremental-tokenize.js +1 -1
  76. package/index.d.ts +39 -6
  77. package/index.js +30 -0
  78. package/languages/detect-index.d.ts +16 -0
  79. package/languages/detect-index.js +6 -0
  80. package/languages/loaders.d.ts +8 -0
  81. package/languages/loaders.js +370 -0
  82. package/package.json +50 -2
  83. package/preprocessor.js +20 -5
  84. package/search.js +2 -2
  85. package/stream-highlighter.js +198 -0
  86. package/stream.css +16 -0
  87. package/stream.d.ts +60 -0
  88. package/stream.js +283 -0
  89. package/style-registry.js +34 -3
  90. package/themes/_base-css.d.ts +1 -0
  91. package/themes/_base-css.js +1 -0
  92. package/typewriter-units.d.ts +3 -0
  93. package/typewriter-units.js +43 -0
  94. package/virtual-window.js +9 -4
package/code-region.js ADDED
@@ -0,0 +1,20 @@
1
+ // CodeWindow shares its `title || label` with the code block inside it, so
2
+ // the block's scrollable region takes the window's name. The block keeps the
3
+ // only tab stop; a second, focusable wrapper would scroll nothing.
4
+ //
5
+ // This module imports nothing, so components call `getContext` and
6
+ // `setContext` themselves with the key.
7
+
8
+ /** Context key for the store that holds the CodeWindow label. */
9
+ export const CODE_REGION = Symbol("svelte-highlight:code-region");
10
+
11
+ /**
12
+ * Store for code blocks outside a CodeWindow. It always holds `undefined`.
13
+ * @type {import("svelte/store").Readable<string | undefined>}
14
+ */
15
+ export const NO_REGION_LABEL = {
16
+ subscribe(run) {
17
+ run(undefined);
18
+ return () => {};
19
+ },
20
+ };
@@ -0,0 +1,115 @@
1
+ /**
2
+ * The `"css-highlights"` engine's DOM side, shared by Highlight and
3
+ * HighlightEditable: feature detection, one component's scope ->
4
+ * `Highlight` registrations on `CSS.highlights`, and painting token ranges
5
+ * over text nodes. The `::highlight()` rules come from
6
+ * `highlight-theme.js`. Framework-free.
7
+ */
8
+
9
+ /** @typedef {"dom" | "css-highlights"} PaintEngine */
10
+
11
+ /** True when this runtime supports the CSS Custom Highlight API. */
12
+ export function supportsCssHighlights() {
13
+ return typeof CSS !== "undefined" && typeof CSS.highlights !== "undefined";
14
+ }
15
+
16
+ /**
17
+ * The engine to use for a requested one: "css-highlights" falls back to
18
+ * "dom" where `CSS.highlights` is missing (SSR, older browsers).
19
+ * @param {PaintEngine} engine
20
+ * @returns {PaintEngine}
21
+ */
22
+ export function resolveEngine(engine) {
23
+ return engine === "css-highlights" && supportsCssHighlights()
24
+ ? "css-highlights"
25
+ : "dom";
26
+ }
27
+
28
+ /**
29
+ * One `Highlight` per scope, registered lazily as `${prefix}-${scope}`.
30
+ * @param {string} prefix Unique per component instance, e.g. `hljs-3`.
31
+ */
32
+ export function createScopeHighlights(prefix) {
33
+ /** @type {Map<string, Highlight>} */
34
+ let highlights = new Map();
35
+
36
+ return {
37
+ /**
38
+ * @param {string} scope
39
+ * @param {Range} range
40
+ */
41
+ add(scope, range) {
42
+ let highlight = highlights.get(scope);
43
+ if (!highlight) {
44
+ highlight = new Highlight();
45
+ highlights.set(scope, highlight);
46
+ CSS.highlights.set(`${prefix}-${scope}`, highlight);
47
+ }
48
+ highlight.add(range);
49
+ },
50
+
51
+ /**
52
+ * @param {string} scope
53
+ * @param {Range} range
54
+ */
55
+ delete(scope, range) {
56
+ highlights.get(scope)?.delete(range);
57
+ },
58
+
59
+ /** Unregisters every scope. */
60
+ clear() {
61
+ for (const scope of highlights.keys()) {
62
+ CSS.highlights.delete(`${prefix}-${scope}`);
63
+ }
64
+ highlights = new Map();
65
+ },
66
+ };
67
+ }
68
+
69
+ /**
70
+ * Adds one range per token over the text nodes under `root`, in document
71
+ * order. `tokenRanges` are offsets into `root.textContent`, sorted and
72
+ * disjoint, as `toRanges` returns them. A token split across text nodes
73
+ * gets a range in each.
74
+ * @param {ReturnType<typeof createScopeHighlights>} highlights
75
+ * @param {Element} root
76
+ * @param {import("./engine.d.ts").TokenRange[]} tokenRanges
77
+ */
78
+ export function paintTokenRanges(highlights, root, tokenRanges) {
79
+ const doc = /** @type {Document} */ (root.ownerDocument);
80
+ const walker = doc.createTreeWalker(root, NodeFilter.SHOW_TEXT);
81
+ let tokenIndex = 0;
82
+ let nodeStart = 0;
83
+ for (
84
+ let node = walker.nextNode();
85
+ node && tokenIndex < tokenRanges.length;
86
+ node = walker.nextNode()
87
+ ) {
88
+ const text = /** @type {Text} */ (node);
89
+ const nodeEnd = nodeStart + text.length;
90
+ while (
91
+ tokenIndex < tokenRanges.length &&
92
+ /** @type {{ end: number }} */ (tokenRanges[tokenIndex]).end <= nodeStart
93
+ ) {
94
+ tokenIndex++;
95
+ }
96
+ for (
97
+ let j = tokenIndex;
98
+ j < tokenRanges.length &&
99
+ /** @type {{ start: number }} */ (tokenRanges[j]).start < nodeEnd;
100
+ j++
101
+ ) {
102
+ const token = /** @type {(typeof tokenRanges)[number]} */ (
103
+ tokenRanges[j]
104
+ );
105
+ const start = Math.max(token.start, nodeStart) - nodeStart;
106
+ const end = Math.min(token.end, nodeEnd) - nodeStart;
107
+ if (start === end) continue;
108
+ const range = new Range();
109
+ range.setStart(text, start);
110
+ range.setEnd(text, end);
111
+ highlights.add(token.scope, range);
112
+ }
113
+ nodeStart = nodeEnd;
114
+ }
115
+ }
package/css-walk.js CHANGED
@@ -86,3 +86,53 @@ export function splitTopLevel(str, delim) {
86
86
  parts.push(str.slice(partStart));
87
87
  return parts;
88
88
  }
89
+
90
+ /**
91
+ * Calls `visit(prelude, body)` for each top-level style rule in `css`, in
92
+ * source order. At-rules (`@media`, `@import`, ...) are skipped. A
93
+ * `<style>` wrapper is stripped first.
94
+ * @param {string} css
95
+ * @param {(prelude: string, body: string) => void} visit
96
+ */
97
+ export function forEachRule(css, visit) {
98
+ const match = STYLE_TAG.exec(css);
99
+ const source = match ? (match[2] ?? "") : css;
100
+
101
+ // Prelude is built from slices between comments, not char by char.
102
+ let prelude = "";
103
+ let runStart = 0;
104
+ let i = 0;
105
+ while (i < source.length) {
106
+ const ch = source[i];
107
+ if (ch === "/" && source[i + 1] === "*") {
108
+ prelude += source.slice(runStart, i);
109
+ const end = source.indexOf("*/", i + 2);
110
+ i = end === -1 ? source.length : end + 2;
111
+ runStart = i;
112
+ continue;
113
+ }
114
+ if (ch === '"' || ch === "'") {
115
+ i = findStringEnd(source, i);
116
+ continue;
117
+ }
118
+ if (ch === "{") {
119
+ prelude += source.slice(runStart, i);
120
+ const bodyEnd = findBlockEnd(source, i + 1);
121
+ if (!prelude.trim().startsWith("@")) {
122
+ visit(prelude, source.slice(i + 1, bodyEnd));
123
+ }
124
+ prelude = "";
125
+ i = bodyEnd + 1;
126
+ runStart = i;
127
+ continue;
128
+ }
129
+ if (ch === ";") {
130
+ // Blockless at-rule (@import, etc.).
131
+ prelude = "";
132
+ i += 1;
133
+ runStart = i;
134
+ continue;
135
+ }
136
+ i += 1;
137
+ }
138
+ }
@@ -0,0 +1,120 @@
1
+ import type { DiffState, diffStats, Row } from "./diff";
2
+ import type { FilePatch } from "./diff-edits";
3
+ import type { LanguageType } from "./languages";
4
+
5
+ export interface Annotation {
6
+ side: "old" | "new";
7
+ /** 1-based line on that side. */
8
+ line: number;
9
+ body: string;
10
+ author?: string;
11
+ tone?: "info" | "warning" | "error" | "suggestion";
12
+ }
13
+
14
+ export type ViewRow = Row & { note?: Annotation };
15
+
16
+ export interface DiffControllerOptions {
17
+ language?: LanguageType<string>;
18
+ /** @default "unified" */
19
+ view: "unified" | "split";
20
+ /** Unchanged lines kept around each change. @default 3 */
21
+ context: number;
22
+ /** @default true */
23
+ wordDiff: boolean;
24
+ /** @default false */
25
+ ignoreWhitespace: boolean;
26
+ /** @default true */
27
+ detectMoves: boolean;
28
+ /** @default [] */
29
+ annotations: Annotation[];
30
+ /** @default 4 */
31
+ tabSize: number;
32
+ }
33
+
34
+ export interface RenderedRow {
35
+ row: ViewRow;
36
+ index: number;
37
+ oldHtml: string;
38
+ newHtml: string;
39
+ }
40
+
41
+ export interface DiffMark {
42
+ id: number;
43
+ rowIndex: number;
44
+ /** Fraction of the row count. */
45
+ top: number;
46
+ height: number;
47
+ kind: "add" | "del" | "mod";
48
+ decision: "accepted" | "rejected" | undefined;
49
+ }
50
+
51
+ export interface DiffEvents {
52
+ /** A view should scroll row `row` into view. */
53
+ reveal: { row: number; align: "start" | "center" | "third" };
54
+ /** Rows `[start, end)` a view shows, out of `count`. */
55
+ viewport: { start: number; end: number; count: number };
56
+ navigate: { change: number; index: number; count: number };
57
+ review: { decisions: Map<number, "accepted" | "rejected">; text: string };
58
+ options: DiffControllerOptions;
59
+ }
60
+
61
+ export interface DiffController {
62
+ /** Svelte store contract: `$diff` updates on every change. */
63
+ subscribe(run: (value: DiffController) => void): () => void;
64
+ on<K extends keyof DiffEvents>(
65
+ type: K,
66
+ listener: (detail: DiffEvents[K]) => void,
67
+ ): () => void;
68
+ /** Bumped on every change. */
69
+ readonly version: number;
70
+
71
+ /**
72
+ * Diffs two texts. With `streaming`, `after` may still be growing.
73
+ * Replacing (not growing) the texts resets folds and review.
74
+ */
75
+ update(
76
+ before: string,
77
+ after: string,
78
+ options?: { streaming?: boolean },
79
+ ): void;
80
+ /** Shows a parsed file patch instead of two texts. */
81
+ setPatch(patch: FilePatch | null): void;
82
+ /** Changes options; a no-op if nothing differs. */
83
+ setOptions(options: Partial<DiffControllerOptions>): void;
84
+ options(): DiffControllerOptions;
85
+
86
+ state(): DiffState & { beforeText?: string; afterText?: string };
87
+ rows(): ViewRow[];
88
+ /** Rows `[start, end)` with highlighted HTML and word diffs. */
89
+ renderRows(start: number, end: number): RenderedRow[];
90
+ stats(): ReturnType<typeof diffStats>;
91
+ /** Widest line per side in columns, and the gutter width in characters. */
92
+ columns(): { old: number; new: number; gutter: number };
93
+ marks(): DiffMark[];
94
+
95
+ /** The change last navigated to or decided, or -1. */
96
+ current(): number;
97
+ nextChange(): DiffEvents["navigate"] | undefined;
98
+ prevChange(): DiffEvents["navigate"] | undefined;
99
+ /** Asks views to scroll a row into view. */
100
+ reveal(row: number, align?: "start" | "center" | "third"): void;
101
+ /** Views report the rows `[start, end)` they show. */
102
+ setViewport(start: number, end: number): void;
103
+
104
+ isExpanded(key: string): boolean;
105
+ toggleFold(key: string): void;
106
+ expandAll(): void;
107
+ collapseAll(): void;
108
+
109
+ decisions(): Map<number, "accepted" | "rejected">;
110
+ decide(change: number, decision: "accepted" | "rejected" | undefined): void;
111
+ decideAll(decision: "accepted" | "rejected"): void;
112
+ /** The text with rejected changes reverted. */
113
+ result(): string;
114
+ /** A unified patch of the diff. */
115
+ patch(paths?: { oldPath?: string; newPath?: string }): string;
116
+ }
117
+
118
+ export function createDiffController(
119
+ options?: Partial<DiffControllerOptions>,
120
+ ): DiffController;