@ai-markdown/core 3.4.1 → 3.5.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/dist/components/index.cjs +515 -30
- package/dist/components/index.cjs.map +1 -1
- package/dist/components/index.d.cts +369 -170
- package/dist/components/index.d.ts +369 -170
- package/dist/components/index.js +504 -32
- package/dist/components/index.js.map +1 -1
- package/package.json +4 -4
|
@@ -1,6 +1,236 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
let _ai_markdown_code_language_detector = require("@ai-markdown/code-language-detector");
|
|
4
|
+
let CodeLanguageFormat = /* @__PURE__ */ function(CodeLanguageFormat) {
|
|
5
|
+
CodeLanguageFormat["HighlightJs"] = "highlight-js";
|
|
6
|
+
CodeLanguageFormat["Shiki"] = "shiki";
|
|
7
|
+
CodeLanguageFormat["Pierre"] = "pierre";
|
|
8
|
+
return CodeLanguageFormat;
|
|
9
|
+
}({});
|
|
10
|
+
function resolveCodeHighlightLanguage(language, format = "highlight-js") {
|
|
11
|
+
if (format === "pierre") {
|
|
12
|
+
const name = (0, _ai_markdown_code_language_detector.normalizeShikiLanguage)(language);
|
|
13
|
+
return name === "common-lisp" ? "lisp" : name;
|
|
14
|
+
}
|
|
15
|
+
return typeof format === "function" ? format(language) : format === "shiki" ? (0, _ai_markdown_code_language_detector.normalizeShikiLanguage)(language) : (0, _ai_markdown_code_language_detector.normalizeHighlightJsLanguage)(language);
|
|
16
|
+
}
|
|
17
|
+
function resolveCodeHighlightInterval(value, fallback = 50) {
|
|
18
|
+
return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : fallback;
|
|
19
|
+
}
|
|
20
|
+
function createCodeHighlightSession(onChange) {
|
|
21
|
+
let committed = false;
|
|
22
|
+
let prepared;
|
|
23
|
+
let cached;
|
|
24
|
+
let fault;
|
|
25
|
+
let epoch = 0;
|
|
26
|
+
let backend;
|
|
27
|
+
const release = () => {
|
|
28
|
+
const previous = backend;
|
|
29
|
+
backend = void 0;
|
|
30
|
+
try {
|
|
31
|
+
previous?.value.dispose();
|
|
32
|
+
} catch {}
|
|
33
|
+
};
|
|
34
|
+
const sameInput = (a, b) => a?.adapter === b.adapter && a?.code === b.code && a?.language === b.language && a?.colorScheme === b.colorScheme && a?.generation === b.generation && a?.streaming === b.streaming;
|
|
35
|
+
const commitHighlight = (input, request) => {
|
|
36
|
+
const adapter = input.adapter;
|
|
37
|
+
if (!adapter.createSession || sameInput(cached?.input, input)) return;
|
|
38
|
+
let snapshot;
|
|
39
|
+
try {
|
|
40
|
+
backend ??= {
|
|
41
|
+
adapter,
|
|
42
|
+
language: request.language,
|
|
43
|
+
colorScheme: request.colorScheme,
|
|
44
|
+
generation: input.generation,
|
|
45
|
+
value: adapter.createSession()
|
|
46
|
+
};
|
|
47
|
+
const result = backend.value.highlight(request);
|
|
48
|
+
snapshot = {
|
|
49
|
+
language: request.language,
|
|
50
|
+
status: result ? "ready" : "plain",
|
|
51
|
+
result
|
|
52
|
+
};
|
|
53
|
+
} catch (error) {
|
|
54
|
+
release();
|
|
55
|
+
snapshot = {
|
|
56
|
+
language: request.language,
|
|
57
|
+
status: "error",
|
|
58
|
+
error
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
cached = {
|
|
62
|
+
input: { ...input },
|
|
63
|
+
snapshot
|
|
64
|
+
};
|
|
65
|
+
onChange();
|
|
66
|
+
};
|
|
67
|
+
const matches = (adapter, request) => prepared?.adapter === adapter && prepared.language === request.language && prepared.colorScheme === request.colorScheme;
|
|
68
|
+
const requestOf = (input) => ({
|
|
69
|
+
code: input.code,
|
|
70
|
+
language: input.adapter ? resolveCodeHighlightLanguage(input.language, input.adapter.languageFormat) : input.language,
|
|
71
|
+
colorScheme: input.colorScheme,
|
|
72
|
+
streaming: input.streaming
|
|
73
|
+
});
|
|
74
|
+
return {
|
|
75
|
+
read(input) {
|
|
76
|
+
const adapter = input.adapter;
|
|
77
|
+
if (!adapter || input.active === false) return {
|
|
78
|
+
language: input.language,
|
|
79
|
+
status: "plain"
|
|
80
|
+
};
|
|
81
|
+
if (adapter.prepare && !committed) return {
|
|
82
|
+
language: input.language,
|
|
83
|
+
status: "loading"
|
|
84
|
+
};
|
|
85
|
+
if (fault && sameInput(fault.input, input)) return {
|
|
86
|
+
language: input.language,
|
|
87
|
+
status: "error",
|
|
88
|
+
error: fault.error
|
|
89
|
+
};
|
|
90
|
+
let request;
|
|
91
|
+
try {
|
|
92
|
+
request = requestOf(input);
|
|
93
|
+
if (adapter.supports?.(request.language) === false) return {
|
|
94
|
+
language: request.language,
|
|
95
|
+
status: "plain"
|
|
96
|
+
};
|
|
97
|
+
if (matches(adapter, request) && prepared?.failed) return {
|
|
98
|
+
language: request.language,
|
|
99
|
+
status: "error",
|
|
100
|
+
error: prepared.error
|
|
101
|
+
};
|
|
102
|
+
if (adapter.prepare && !adapter.isReady?.(request) && !(matches(adapter, request) && prepared?.ready)) return {
|
|
103
|
+
language: request.language,
|
|
104
|
+
status: "loading"
|
|
105
|
+
};
|
|
106
|
+
if (sameInput(cached?.input, input)) return cached.snapshot;
|
|
107
|
+
if (adapter.createSession) return {
|
|
108
|
+
language: request.language,
|
|
109
|
+
status: "loading"
|
|
110
|
+
};
|
|
111
|
+
const result = adapter.highlight(request);
|
|
112
|
+
const snapshot = {
|
|
113
|
+
language: request.language,
|
|
114
|
+
status: result ? "ready" : "plain",
|
|
115
|
+
result
|
|
116
|
+
};
|
|
117
|
+
cached = {
|
|
118
|
+
input: { ...input },
|
|
119
|
+
snapshot
|
|
120
|
+
};
|
|
121
|
+
return snapshot;
|
|
122
|
+
} catch (error) {
|
|
123
|
+
return {
|
|
124
|
+
language: input.language,
|
|
125
|
+
status: "error",
|
|
126
|
+
error
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
commit(input) {
|
|
131
|
+
const firstCommit = !committed;
|
|
132
|
+
committed = true;
|
|
133
|
+
if (input.adapter && input.active !== false && fault && sameInput(fault.input, input)) return;
|
|
134
|
+
fault = void 0;
|
|
135
|
+
const adapter = input.adapter;
|
|
136
|
+
if (!adapter || input.active === false) {
|
|
137
|
+
epoch++;
|
|
138
|
+
prepared = void 0;
|
|
139
|
+
cached = void 0;
|
|
140
|
+
release();
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
let request;
|
|
144
|
+
try {
|
|
145
|
+
request = requestOf(input);
|
|
146
|
+
if (backend && (backend.adapter !== adapter || backend.language !== request.language || backend.colorScheme !== request.colorScheme || backend.generation !== input.generation)) {
|
|
147
|
+
release();
|
|
148
|
+
cached = void 0;
|
|
149
|
+
}
|
|
150
|
+
if (adapter.supports?.(request.language) === false) {
|
|
151
|
+
if (firstCommit && adapter.prepare) onChange();
|
|
152
|
+
epoch++;
|
|
153
|
+
prepared = void 0;
|
|
154
|
+
cached = void 0;
|
|
155
|
+
release();
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
if (matches(adapter, request)) {
|
|
159
|
+
if (!prepared?.failed && (!adapter.prepare || adapter.isReady?.(request) || prepared?.ready)) commitHighlight(input, request);
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
const ticket = ++epoch;
|
|
163
|
+
prepared = {
|
|
164
|
+
adapter,
|
|
165
|
+
language: request.language,
|
|
166
|
+
colorScheme: request.colorScheme
|
|
167
|
+
};
|
|
168
|
+
if (!adapter.prepare || adapter.isReady?.(request)) {
|
|
169
|
+
if (firstCommit && adapter.prepare && !adapter.createSession) onChange();
|
|
170
|
+
commitHighlight(input, request);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
Promise.resolve().then(() => {
|
|
174
|
+
if (ticket !== epoch) return;
|
|
175
|
+
return adapter.prepare(request);
|
|
176
|
+
}).then(() => {
|
|
177
|
+
if (ticket !== epoch) return;
|
|
178
|
+
prepared.ready = true;
|
|
179
|
+
cached = void 0;
|
|
180
|
+
onChange();
|
|
181
|
+
}, (error) => {
|
|
182
|
+
if (ticket !== epoch) return;
|
|
183
|
+
prepared.failed = true;
|
|
184
|
+
prepared.error = error;
|
|
185
|
+
onChange();
|
|
186
|
+
});
|
|
187
|
+
} catch (error) {
|
|
188
|
+
epoch++;
|
|
189
|
+
release();
|
|
190
|
+
prepared = void 0;
|
|
191
|
+
cached = void 0;
|
|
192
|
+
fault = {
|
|
193
|
+
input: { ...input },
|
|
194
|
+
error
|
|
195
|
+
};
|
|
196
|
+
onChange();
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
retry() {
|
|
200
|
+
fault = void 0;
|
|
201
|
+
release();
|
|
202
|
+
epoch++;
|
|
203
|
+
prepared = void 0;
|
|
204
|
+
cached = void 0;
|
|
205
|
+
onChange();
|
|
206
|
+
},
|
|
207
|
+
dispose() {
|
|
208
|
+
committed = false;
|
|
209
|
+
fault = void 0;
|
|
210
|
+
release();
|
|
211
|
+
epoch++;
|
|
212
|
+
prepared = void 0;
|
|
213
|
+
cached = void 0;
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
const defaultCodeBlockPolicy = Object.freeze({
|
|
218
|
+
defaultExpanded: true,
|
|
219
|
+
autoDetectUnknownLanguage: true,
|
|
220
|
+
formatJson: false,
|
|
221
|
+
expandNestedJson: false,
|
|
222
|
+
highlightIntervalMs: 50
|
|
223
|
+
});
|
|
224
|
+
function resolveCodeBlockPolicy(overrides, inherited, defaults = defaultCodeBlockPolicy) {
|
|
225
|
+
const value = (key) => overrides?.[key] ?? inherited?.[key] ?? defaults[key] ?? defaultCodeBlockPolicy[key];
|
|
226
|
+
return {
|
|
227
|
+
defaultExpanded: value("defaultExpanded"),
|
|
228
|
+
autoDetectUnknownLanguage: value("autoDetectUnknownLanguage"),
|
|
229
|
+
formatJson: value("formatJson"),
|
|
230
|
+
expandNestedJson: value("expandNestedJson"),
|
|
231
|
+
highlightIntervalMs: resolveCodeHighlightInterval(value("highlightIntervalMs"), defaults.highlightIntervalMs ?? 50)
|
|
232
|
+
};
|
|
233
|
+
}
|
|
4
234
|
function extractCode(node) {
|
|
5
235
|
if (node?.tagName !== "pre" || node.children.length !== 1 || Object.keys(node.properties).length) return null;
|
|
6
236
|
const child = node.children[0];
|
|
@@ -411,6 +641,25 @@ const ensureMermaidInitialized = (mermaid, isDark) => {
|
|
|
411
641
|
}
|
|
412
642
|
mermaid.initialize(next);
|
|
413
643
|
};
|
|
644
|
+
var UnsupportedDiagramError = class extends Error {
|
|
645
|
+
diagramType;
|
|
646
|
+
constructor(diagramType) {
|
|
647
|
+
super(`Unsupported diagram type: ${diagramType}`);
|
|
648
|
+
this.diagramType = diagramType;
|
|
649
|
+
this.name = "UnsupportedDiagramError";
|
|
650
|
+
}
|
|
651
|
+
};
|
|
652
|
+
const resolveDiagramInterval = (value) => typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : 300;
|
|
653
|
+
function createCachedDiagramLoader(load) {
|
|
654
|
+
let pending;
|
|
655
|
+
return () => pending ??= Promise.resolve().then(load).catch((error) => {
|
|
656
|
+
pending = void 0;
|
|
657
|
+
throw error;
|
|
658
|
+
});
|
|
659
|
+
}
|
|
660
|
+
function preloadDiagramEngine(load) {
|
|
661
|
+
return Promise.resolve().then(load).then(() => void 0);
|
|
662
|
+
}
|
|
414
663
|
function diagramErrorText(error, labels) {
|
|
415
664
|
if (error.reason === "tooLarge") return labels.tooLarge(error.length, error.limit);
|
|
416
665
|
return error.message || labels.renderFailed;
|
|
@@ -429,7 +678,7 @@ var DiagramFailure = class {
|
|
|
429
678
|
}
|
|
430
679
|
};
|
|
431
680
|
const diagramId = () => `mermaid-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`;
|
|
432
|
-
function createDiagramController(load, publish) {
|
|
681
|
+
function createDiagramController(load, publish, fallback) {
|
|
433
682
|
const owner = {};
|
|
434
683
|
let request;
|
|
435
684
|
let current;
|
|
@@ -445,6 +694,7 @@ function createDiagramController(load, publish) {
|
|
|
445
694
|
let nonce = 0;
|
|
446
695
|
let ran;
|
|
447
696
|
let epoch = 0;
|
|
697
|
+
let disposed = false;
|
|
448
698
|
let retryTimer;
|
|
449
699
|
const sameError = (a, b) => a.reason === "tooLarge" ? b.reason === "tooLarge" && a.length === b.length && a.limit === b.limit : b.reason === a.reason && a.message === b.message;
|
|
450
700
|
const emit = () => publish({
|
|
@@ -465,13 +715,10 @@ function createDiagramController(load, publish) {
|
|
|
465
715
|
};
|
|
466
716
|
const attempt = async (code, dark, streaming, generation) => {
|
|
467
717
|
const stale = () => generation !== epoch;
|
|
468
|
-
|
|
469
|
-
try {
|
|
470
|
-
engine = await load();
|
|
471
|
-
} catch {
|
|
718
|
+
const loadFailedAttempt = () => {
|
|
472
719
|
if (stale()) return;
|
|
473
720
|
if (loadFailures < LOAD_RETRIES) {
|
|
474
|
-
loadFailures
|
|
721
|
+
loadFailures++;
|
|
475
722
|
retryTimer = setTimeout(() => {
|
|
476
723
|
retryTimer = void 0;
|
|
477
724
|
nonce++;
|
|
@@ -481,42 +728,95 @@ function createDiagramController(load, publish) {
|
|
|
481
728
|
loadFailed = true;
|
|
482
729
|
emit();
|
|
483
730
|
}
|
|
731
|
+
};
|
|
732
|
+
let engine;
|
|
733
|
+
try {
|
|
734
|
+
engine = await load();
|
|
735
|
+
} catch {
|
|
736
|
+
loadFailedAttempt();
|
|
484
737
|
return;
|
|
485
738
|
}
|
|
486
|
-
loadFailures = 0;
|
|
487
739
|
let reason = "render";
|
|
488
740
|
try {
|
|
489
741
|
if (stale()) return;
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
kind
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
742
|
+
const render = async (target) => {
|
|
743
|
+
if (target.kind !== "svg") ensureMermaidInitialized(target, dark);
|
|
744
|
+
const maxTextSize = target.kind === "svg" ? target.maxTextSize ?? 5e4 : getMermaidMaxTextSize(target);
|
|
745
|
+
if (code.length > maxTextSize) throw new DiagramFailure({
|
|
746
|
+
kind: "error",
|
|
747
|
+
reason: "tooLarge",
|
|
748
|
+
length: code.length,
|
|
749
|
+
limit: maxTextSize
|
|
750
|
+
});
|
|
751
|
+
reason = "parse";
|
|
752
|
+
if (target.kind !== "svg" && !await target.parse(code)) throw new DiagramFailure({
|
|
753
|
+
kind: "error",
|
|
754
|
+
reason: "parse",
|
|
755
|
+
message: ""
|
|
756
|
+
});
|
|
757
|
+
reason = "render";
|
|
758
|
+
if (stale()) return void 0;
|
|
759
|
+
return target.kind === "svg" ? target.render(diagramId(), code, dark) : target.render(diagramId(), code);
|
|
760
|
+
};
|
|
761
|
+
let result;
|
|
762
|
+
try {
|
|
763
|
+
result = await render(engine);
|
|
764
|
+
} catch (error) {
|
|
765
|
+
if (!(error instanceof UnsupportedDiagramError) || !fallback || stale()) throw error;
|
|
766
|
+
try {
|
|
767
|
+
engine = await fallback();
|
|
768
|
+
} catch {
|
|
769
|
+
loadFailedAttempt();
|
|
770
|
+
return;
|
|
771
|
+
}
|
|
772
|
+
if (stale()) return;
|
|
773
|
+
result = await render(engine);
|
|
774
|
+
}
|
|
775
|
+
if (!result) return;
|
|
776
|
+
const { svg, bindFunctions, diagramType } = result;
|
|
507
777
|
if (stale()) return;
|
|
778
|
+
loadFailures = 0;
|
|
779
|
+
const previousView = view.kind === "diagram" ? {
|
|
780
|
+
...view,
|
|
781
|
+
onBindError: void 0
|
|
782
|
+
} : view;
|
|
783
|
+
if (previousView.kind === "diagram") previousView.onBindError = (error) => {
|
|
784
|
+
if (disposed || view !== previousView) return;
|
|
785
|
+
lastSuccess = void 0;
|
|
786
|
+
show({
|
|
787
|
+
kind: "error",
|
|
788
|
+
reason: "render",
|
|
789
|
+
message: engineMessage(error)
|
|
790
|
+
});
|
|
791
|
+
};
|
|
792
|
+
const corrective = needsCorrective;
|
|
508
793
|
needsCorrective = false;
|
|
509
794
|
lastSuccess = {
|
|
510
795
|
code,
|
|
511
796
|
dark
|
|
512
797
|
};
|
|
513
798
|
loadFailed = false;
|
|
514
|
-
|
|
799
|
+
const nextView = {
|
|
515
800
|
kind: "diagram",
|
|
516
801
|
svg,
|
|
517
802
|
chartType: diagramType ?? "",
|
|
518
|
-
bind: bindFunctions
|
|
519
|
-
|
|
803
|
+
bind: bindFunctions,
|
|
804
|
+
...engine.kind === "svg" ? {
|
|
805
|
+
palette: engine.palette,
|
|
806
|
+
dark
|
|
807
|
+
} : {},
|
|
808
|
+
onBindError(error) {
|
|
809
|
+
if (disposed || view !== nextView) return;
|
|
810
|
+
lastSuccess = void 0;
|
|
811
|
+
if (streaming || !corrective && previousView.kind === "diagram") show(previousView);
|
|
812
|
+
else show({
|
|
813
|
+
kind: "error",
|
|
814
|
+
reason: "render",
|
|
815
|
+
message: engineMessage(error)
|
|
816
|
+
});
|
|
817
|
+
}
|
|
818
|
+
};
|
|
819
|
+
show(nextView);
|
|
520
820
|
} catch (error) {
|
|
521
821
|
if (stale()) return;
|
|
522
822
|
if (streaming) return;
|
|
@@ -558,7 +858,17 @@ function createDiagramController(load, publish) {
|
|
|
558
858
|
}
|
|
559
859
|
} else if (!streaming && previousStreaming) needsCorrective = true;
|
|
560
860
|
previousStreaming = streaming;
|
|
561
|
-
if (!code
|
|
861
|
+
if (!code) {
|
|
862
|
+
lastSuccess = void 0;
|
|
863
|
+
needsCorrective = false;
|
|
864
|
+
if (view.kind !== "source" || loadFailed) {
|
|
865
|
+
view = { kind: "source" };
|
|
866
|
+
loadFailed = false;
|
|
867
|
+
emit();
|
|
868
|
+
}
|
|
869
|
+
return;
|
|
870
|
+
}
|
|
871
|
+
if (!active) return;
|
|
562
872
|
if (lastSuccess?.code === code && lastSuccess.dark === dark) {
|
|
563
873
|
needsCorrective = false;
|
|
564
874
|
return;
|
|
@@ -568,7 +878,11 @@ function createDiagramController(load, publish) {
|
|
|
568
878
|
}
|
|
569
879
|
return {
|
|
570
880
|
update(next) {
|
|
571
|
-
|
|
881
|
+
disposed = false;
|
|
882
|
+
request = {
|
|
883
|
+
...next,
|
|
884
|
+
interval: resolveDiagramInterval(next.interval)
|
|
885
|
+
};
|
|
572
886
|
updating = true;
|
|
573
887
|
try {
|
|
574
888
|
if (!frames || !current) {
|
|
@@ -578,22 +892,25 @@ function createDiagramController(load, publish) {
|
|
|
578
892
|
if (!updating) run();
|
|
579
893
|
});
|
|
580
894
|
} else current = nextCodeFrame(current, next.code, "");
|
|
581
|
-
frames.update(current, next.streaming,
|
|
895
|
+
frames.update(current, next.streaming, request.interval);
|
|
582
896
|
} finally {
|
|
583
897
|
updating = false;
|
|
584
898
|
}
|
|
585
899
|
run();
|
|
586
900
|
},
|
|
587
901
|
retry() {
|
|
902
|
+
if (disposed) return;
|
|
588
903
|
loadFailures = 0;
|
|
589
904
|
if (loadFailed) {
|
|
590
905
|
loadFailed = false;
|
|
591
906
|
emit();
|
|
592
907
|
}
|
|
908
|
+
lastSuccess = void 0;
|
|
593
909
|
nonce++;
|
|
594
910
|
run();
|
|
595
911
|
},
|
|
596
912
|
dispose() {
|
|
913
|
+
disposed = true;
|
|
597
914
|
cancel();
|
|
598
915
|
frames?.dispose();
|
|
599
916
|
ran = void 0;
|
|
@@ -1217,13 +1534,176 @@ const imageActionNames = Object.freeze([
|
|
|
1217
1534
|
"flipX",
|
|
1218
1535
|
"flipY"
|
|
1219
1536
|
]);
|
|
1537
|
+
function createSvgLoader(load, palette, options) {
|
|
1538
|
+
const renderOptions = { ...options?.renderOptions };
|
|
1539
|
+
const limit = renderOptions.limit ?? 5e4;
|
|
1540
|
+
if (!Number.isFinite(limit) || limit < 0) throw new RangeError("Diagram limit must be a finite non-negative number");
|
|
1541
|
+
return createCachedDiagramLoader(async () => {
|
|
1542
|
+
const renderer = await load();
|
|
1543
|
+
return {
|
|
1544
|
+
kind: "svg",
|
|
1545
|
+
palette,
|
|
1546
|
+
maxTextSize: limit,
|
|
1547
|
+
async render(id, code) {
|
|
1548
|
+
try {
|
|
1549
|
+
const result = await renderer.render(code, {
|
|
1550
|
+
...renderOptions,
|
|
1551
|
+
idPrefix: id,
|
|
1552
|
+
limit
|
|
1553
|
+
});
|
|
1554
|
+
return {
|
|
1555
|
+
svg: result.svg,
|
|
1556
|
+
diagramType: result.type
|
|
1557
|
+
};
|
|
1558
|
+
} catch (error) {
|
|
1559
|
+
const diagnostic = error;
|
|
1560
|
+
if (diagnostic?.code === "unsupported-diagram" && typeof diagnostic.type === "string" && diagnostic.type) throw new UnsupportedDiagramError(diagnostic.type);
|
|
1561
|
+
throw error;
|
|
1562
|
+
}
|
|
1563
|
+
}
|
|
1564
|
+
};
|
|
1565
|
+
});
|
|
1566
|
+
}
|
|
1567
|
+
function createPeleLoader(load, options) {
|
|
1568
|
+
return createSvgLoader(load, "pele", options);
|
|
1569
|
+
}
|
|
1570
|
+
function createOndineLoader(load, options) {
|
|
1571
|
+
return createSvgLoader(load, "ondine", options);
|
|
1572
|
+
}
|
|
1573
|
+
function convert(token, foreground) {
|
|
1574
|
+
return {
|
|
1575
|
+
text: token.content,
|
|
1576
|
+
color: token.color ?? foreground,
|
|
1577
|
+
backgroundColor: token.bgColor,
|
|
1578
|
+
fontStyle: token.fontStyle
|
|
1579
|
+
};
|
|
1580
|
+
}
|
|
1581
|
+
function createPierreHighlightAdapter(options) {
|
|
1582
|
+
let runtime;
|
|
1583
|
+
let pending;
|
|
1584
|
+
const adapter = {
|
|
1585
|
+
languageFormat: "pierre",
|
|
1586
|
+
supports: (language) => ![
|
|
1587
|
+
"",
|
|
1588
|
+
"plain",
|
|
1589
|
+
"plaintext",
|
|
1590
|
+
"text",
|
|
1591
|
+
"txt"
|
|
1592
|
+
].includes(language) && (!runtime || runtime.isSupportedLanguage(language)),
|
|
1593
|
+
isReady: () => Boolean(runtime),
|
|
1594
|
+
prepare: () => {
|
|
1595
|
+
if (runtime) return;
|
|
1596
|
+
return pending ??= Promise.resolve().then(options.load).then((loaded) => {
|
|
1597
|
+
runtime = loaded;
|
|
1598
|
+
}).finally(() => {
|
|
1599
|
+
pending = void 0;
|
|
1600
|
+
});
|
|
1601
|
+
},
|
|
1602
|
+
highlight: ({ code, language, colorScheme }) => {
|
|
1603
|
+
if (!runtime?.isSupportedLanguage(language)) return;
|
|
1604
|
+
const result = runtime.codeToTokens(code, {
|
|
1605
|
+
lang: language,
|
|
1606
|
+
theme: runtime.themes[colorScheme]
|
|
1607
|
+
});
|
|
1608
|
+
const tokens = [];
|
|
1609
|
+
let cursor = 0;
|
|
1610
|
+
for (const line of result.tokens) for (const token of line) {
|
|
1611
|
+
if (token.offset > cursor) tokens.push({
|
|
1612
|
+
text: code.slice(cursor, token.offset),
|
|
1613
|
+
color: result.fg
|
|
1614
|
+
});
|
|
1615
|
+
tokens.push(convert(token, result.fg));
|
|
1616
|
+
cursor = token.offset + token.content.length;
|
|
1617
|
+
}
|
|
1618
|
+
if (cursor < code.length) tokens.push({
|
|
1619
|
+
text: code.slice(cursor),
|
|
1620
|
+
color: result.fg
|
|
1621
|
+
});
|
|
1622
|
+
return { tokens };
|
|
1623
|
+
}
|
|
1624
|
+
};
|
|
1625
|
+
if (options.incremental !== false) adapter.createSession = () => {
|
|
1626
|
+
let live;
|
|
1627
|
+
let previous = "", previousLanguage = "", previousScheme = "";
|
|
1628
|
+
let lines = [];
|
|
1629
|
+
const dispose = () => {
|
|
1630
|
+
live?.dispose();
|
|
1631
|
+
live = void 0;
|
|
1632
|
+
lines = [];
|
|
1633
|
+
previous = "";
|
|
1634
|
+
};
|
|
1635
|
+
return {
|
|
1636
|
+
dispose,
|
|
1637
|
+
highlight(request) {
|
|
1638
|
+
const { code, language, colorScheme } = request;
|
|
1639
|
+
if (!runtime?.isSupportedLanguage(language)) return;
|
|
1640
|
+
if (!request.streaming || !runtime.LiveTokenizer) {
|
|
1641
|
+
dispose();
|
|
1642
|
+
return adapter.highlight(request);
|
|
1643
|
+
}
|
|
1644
|
+
const appended = startsWithText(code, previous);
|
|
1645
|
+
const terminatorLength = previous.endsWith("\r\n") ? 2 : previous.endsWith("\n") ? 1 : 0;
|
|
1646
|
+
const suffixStart = previous.length - terminatorLength;
|
|
1647
|
+
const replacesTerminator = !appended && terminatorLength > 0 && startsWithText(code, previous.slice(0, suffixStart));
|
|
1648
|
+
if (!live || language !== previousLanguage || colorScheme !== previousScheme || !appended && !replacesTerminator) {
|
|
1649
|
+
dispose();
|
|
1650
|
+
live = new runtime.LiveTokenizer({
|
|
1651
|
+
code,
|
|
1652
|
+
lang: language,
|
|
1653
|
+
theme: runtime.themes[colorScheme]
|
|
1654
|
+
});
|
|
1655
|
+
lines = Array.from({ length: live.lineCount }, (_, line) => live.getLineTokens(line).tokens.map((token) => convert(token)));
|
|
1656
|
+
} else if (code !== previous) {
|
|
1657
|
+
const line = live.lineCount - 1;
|
|
1658
|
+
const end = {
|
|
1659
|
+
line,
|
|
1660
|
+
character: live.getLineLength(line)
|
|
1661
|
+
};
|
|
1662
|
+
const start = replacesTerminator ? {
|
|
1663
|
+
line: line - 1,
|
|
1664
|
+
character: live.getLineLength(line - 1)
|
|
1665
|
+
} : end;
|
|
1666
|
+
const update = live.applyEdits([{
|
|
1667
|
+
range: {
|
|
1668
|
+
start,
|
|
1669
|
+
end
|
|
1670
|
+
},
|
|
1671
|
+
newText: code.slice(replacesTerminator ? suffixStart : previous.length)
|
|
1672
|
+
}]);
|
|
1673
|
+
for (const change of [...update.lineChanges].reverse()) {
|
|
1674
|
+
const replacement = Array.from({ length: change.newEndLine - change.newStartLine }, (_, index) => live.getLineTokens(change.newStartLine + index).tokens.map((token) => convert(token)));
|
|
1675
|
+
lines = [
|
|
1676
|
+
...lines.slice(0, change.oldStartLine),
|
|
1677
|
+
...replacement,
|
|
1678
|
+
...lines.slice(change.oldEndLine)
|
|
1679
|
+
];
|
|
1680
|
+
}
|
|
1681
|
+
}
|
|
1682
|
+
previous = code;
|
|
1683
|
+
previousLanguage = language;
|
|
1684
|
+
previousScheme = colorScheme;
|
|
1685
|
+
const breaks = code.match(/\r\n|\r|\n/g) ?? [];
|
|
1686
|
+
return { tokens: lines.flatMap((line, index) => index < breaks.length ? [...line, { text: breaks[index] }] : line) };
|
|
1687
|
+
}
|
|
1688
|
+
};
|
|
1689
|
+
};
|
|
1690
|
+
return adapter;
|
|
1691
|
+
}
|
|
1692
|
+
exports.CodeLanguageFormat = CodeLanguageFormat;
|
|
1693
|
+
exports.UnsupportedDiagramError = UnsupportedDiagramError;
|
|
1694
|
+
exports.createCachedDiagramLoader = createCachedDiagramLoader;
|
|
1220
1695
|
exports.createCodeBlockSession = createCodeBlockSession;
|
|
1221
1696
|
exports.createCodeFrame = createCodeFrame;
|
|
1697
|
+
exports.createCodeHighlightSession = createCodeHighlightSession;
|
|
1222
1698
|
exports.createCodeLanguageTracker = createCodeLanguageTracker;
|
|
1223
1699
|
exports.createDiagramController = createDiagramController;
|
|
1224
1700
|
exports.createImagePreviewController = createImagePreviewController;
|
|
1225
1701
|
exports.createJsonCompletenessScanner = createJsonCompletenessScanner;
|
|
1702
|
+
exports.createOndineLoader = createOndineLoader;
|
|
1703
|
+
exports.createPeleLoader = createPeleLoader;
|
|
1704
|
+
exports.createPierreHighlightAdapter = createPierreHighlightAdapter;
|
|
1226
1705
|
exports.defaultCodeBlockLabels = defaultCodeBlockLabels;
|
|
1706
|
+
exports.defaultCodeBlockPolicy = defaultCodeBlockPolicy;
|
|
1227
1707
|
exports.defaultDiagramLabels = defaultDiagramLabels;
|
|
1228
1708
|
exports.defaultImageLabels = defaultImageLabels;
|
|
1229
1709
|
exports.defaultTableLabels = defaultTableLabels;
|
|
@@ -1244,9 +1724,14 @@ exports.lockImagePreviewScroll = lockImagePreviewScroll;
|
|
|
1244
1724
|
exports.mermaidRenderQueue = mermaidRenderQueue;
|
|
1245
1725
|
exports.nextCodeFrame = nextCodeFrame;
|
|
1246
1726
|
exports.normalizeRenderers = normalizeRenderers;
|
|
1727
|
+
exports.preloadDiagramEngine = preloadDiagramEngine;
|
|
1247
1728
|
exports.prettyPrintJson = prettyPrintJson;
|
|
1248
1729
|
exports.projectTable = projectTable;
|
|
1249
1730
|
exports.reboundImage = reboundImage;
|
|
1731
|
+
exports.resolveCodeBlockPolicy = resolveCodeBlockPolicy;
|
|
1732
|
+
exports.resolveCodeHighlightInterval = resolveCodeHighlightInterval;
|
|
1733
|
+
exports.resolveCodeHighlightLanguage = resolveCodeHighlightLanguage;
|
|
1734
|
+
exports.resolveDiagramInterval = resolveDiagramInterval;
|
|
1250
1735
|
exports.resolveLabels = resolveLabels;
|
|
1251
1736
|
exports.serializeTable = serializeTable;
|
|
1252
1737
|
exports.zoomImage = zoomImage;
|