@svadmin/ai-elements 0.7.4 → 0.8.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 (214) hide show
  1. package/README.md +20 -4
  2. package/THIRD_PARTY_NOTICES.md +67 -6
  3. package/dist/ai.css +1 -5
  4. package/dist/ai.theme.css +1 -0
  5. package/dist/class-properties.generated.d.ts +1 -0
  6. package/dist/class-properties.generated.js +2 -0
  7. package/dist/classnames.d.ts +8 -0
  8. package/dist/classnames.js +26 -0
  9. package/dist/components/Response.svelte +2 -2
  10. package/dist/components/Response.svelte.d.ts +1 -1
  11. package/dist/components/code/index.d.ts +9 -18
  12. package/dist/components/code/index.js +8 -11
  13. package/dist/components/code-block/highlight.js +1 -1
  14. package/dist/utilities.css +3172 -0
  15. package/dist/utils.d.ts +1 -1
  16. package/dist/utils.js +1 -1
  17. package/package.json +36 -11
  18. package/vendor/streamdown/AnimatedText.svelte +93 -0
  19. package/vendor/streamdown/AnimatedText.svelte.d.ts +6 -0
  20. package/vendor/streamdown/Block.svelte +96 -0
  21. package/vendor/streamdown/Block.svelte.d.ts +11 -0
  22. package/vendor/streamdown/Elements/Alert.svelte +69 -0
  23. package/vendor/streamdown/Elements/Alert.svelte.d.ts +10 -0
  24. package/vendor/streamdown/Elements/Citation.svelte +260 -0
  25. package/vendor/streamdown/Elements/Citation.svelte.d.ts +7 -0
  26. package/vendor/streamdown/Elements/Code.svelte +226 -0
  27. package/vendor/streamdown/Elements/Code.svelte.d.ts +9 -0
  28. package/vendor/streamdown/Elements/Element.svelte +574 -0
  29. package/vendor/streamdown/Elements/Element.svelte.d.ts +10 -0
  30. package/vendor/streamdown/Elements/FootnoteRef.svelte +30 -0
  31. package/vendor/streamdown/Elements/FootnoteRef.svelte.d.ts +7 -0
  32. package/vendor/streamdown/Elements/Footnotes.svelte +54 -0
  33. package/vendor/streamdown/Elements/Footnotes.svelte.d.ts +7 -0
  34. package/vendor/streamdown/Elements/Image.svelte +200 -0
  35. package/vendor/streamdown/Elements/Image.svelte.d.ts +10 -0
  36. package/vendor/streamdown/Elements/Link.svelte +220 -0
  37. package/vendor/streamdown/Elements/Link.svelte.d.ts +10 -0
  38. package/vendor/streamdown/Elements/LinkSafetyModal.svelte +121 -0
  39. package/vendor/streamdown/Elements/LinkSafetyModal.svelte.d.ts +4 -0
  40. package/vendor/streamdown/Elements/Math.svelte +100 -0
  41. package/vendor/streamdown/Elements/Math.svelte.d.ts +9 -0
  42. package/vendor/streamdown/Elements/Mermaid.svelte +455 -0
  43. package/vendor/streamdown/Elements/Mermaid.svelte.d.ts +9 -0
  44. package/vendor/streamdown/Elements/MermaidDownload.svelte +145 -0
  45. package/vendor/streamdown/Elements/MermaidDownload.svelte.d.ts +8 -0
  46. package/vendor/streamdown/Elements/Slot.svelte +23 -0
  47. package/vendor/streamdown/Elements/Slot.svelte.d.ts +10 -0
  48. package/vendor/streamdown/Elements/Table.svelte +195 -0
  49. package/vendor/streamdown/Elements/Table.svelte.d.ts +9 -0
  50. package/vendor/streamdown/Elements/TableDownload.svelte +221 -0
  51. package/vendor/streamdown/Elements/TableDownload.svelte.d.ts +8 -0
  52. package/vendor/streamdown/Elements/fallbacks/CodeFallback.svelte +41 -0
  53. package/vendor/streamdown/Elements/fallbacks/CodeFallback.svelte.d.ts +9 -0
  54. package/vendor/streamdown/Elements/fallbacks/MathFallback.svelte +35 -0
  55. package/vendor/streamdown/Elements/fallbacks/MathFallback.svelte.d.ts +8 -0
  56. package/vendor/streamdown/Elements/fallbacks/MermaidFallback.svelte +36 -0
  57. package/vendor/streamdown/Elements/fallbacks/MermaidFallback.svelte.d.ts +9 -0
  58. package/vendor/streamdown/Elements/fallbacks/index.d.ts +3 -0
  59. package/vendor/streamdown/Elements/fallbacks/index.js +3 -0
  60. package/vendor/streamdown/Elements/icons.d.ts +31 -0
  61. package/vendor/streamdown/Elements/icons.js +200 -0
  62. package/vendor/streamdown/Elements/index.d.ts +8 -0
  63. package/vendor/streamdown/Elements/index.js +8 -0
  64. package/vendor/streamdown/Elements/popover.svelte.d.ts +8 -0
  65. package/vendor/streamdown/Elements/popover.svelte.js +45 -0
  66. package/vendor/streamdown/Elements/stepperState.svelte.d.ts +36 -0
  67. package/vendor/streamdown/Elements/stepperState.svelte.js +92 -0
  68. package/vendor/streamdown/IconProvider.svelte +11 -0
  69. package/vendor/streamdown/IconProvider.svelte.d.ts +30 -0
  70. package/vendor/streamdown/LICENSE +202 -0
  71. package/vendor/streamdown/PluginProvider.svelte +11 -0
  72. package/vendor/streamdown/PluginProvider.svelte.d.ts +30 -0
  73. package/vendor/streamdown/README.svadmin.md +5 -0
  74. package/vendor/streamdown/Streamdown.svelte +447 -0
  75. package/vendor/streamdown/Streamdown.svelte.d.ts +25 -0
  76. package/vendor/streamdown/block/BlockTokens.svelte +45 -0
  77. package/vendor/streamdown/block/BlockTokens.svelte.d.ts +9 -0
  78. package/vendor/streamdown/block/html.d.ts +14 -0
  79. package/vendor/streamdown/block/html.js +27 -0
  80. package/vendor/streamdown/block/text.d.ts +10 -0
  81. package/vendor/streamdown/block/text.js +31 -0
  82. package/vendor/streamdown/context-key.d.ts +1 -0
  83. package/vendor/streamdown/context-key.js +1 -0
  84. package/vendor/streamdown/context.svelte.d.ts +24 -0
  85. package/vendor/streamdown/context.svelte.js +75 -0
  86. package/vendor/streamdown/contracts/plugins.d.ts +26 -0
  87. package/vendor/streamdown/contracts/plugins.js +1 -0
  88. package/vendor/streamdown/contracts/streamdown.d.ts +374 -0
  89. package/vendor/streamdown/contracts/streamdown.js +29 -0
  90. package/vendor/streamdown/detect-direction.d.ts +1 -0
  91. package/vendor/streamdown/detect-direction.js +1 -0
  92. package/vendor/streamdown/icon-context.d.ts +36 -0
  93. package/vendor/streamdown/icon-context.js +52 -0
  94. package/vendor/streamdown/incomplete-code.d.ts +6 -0
  95. package/vendor/streamdown/incomplete-code.js +23 -0
  96. package/vendor/streamdown/index.d.ts +22 -0
  97. package/vendor/streamdown/index.js +20 -0
  98. package/vendor/streamdown/markdown-parse-cache.d.ts +37 -0
  99. package/vendor/streamdown/markdown-parse-cache.js +109 -0
  100. package/vendor/streamdown/markdown.d.ts +21 -0
  101. package/vendor/streamdown/markdown.js +214 -0
  102. package/vendor/streamdown/marked/index.d.ts +44 -0
  103. package/vendor/streamdown/marked/index.js +228 -0
  104. package/vendor/streamdown/marked/marked-alert.d.ts +13 -0
  105. package/vendor/streamdown/marked/marked-alert.js +47 -0
  106. package/vendor/streamdown/marked/marked-align.d.ts +10 -0
  107. package/vendor/streamdown/marked/marked-align.js +45 -0
  108. package/vendor/streamdown/marked/marked-br.d.ts +6 -0
  109. package/vendor/streamdown/marked/marked-br.js +15 -0
  110. package/vendor/streamdown/marked/marked-citations.d.ts +8 -0
  111. package/vendor/streamdown/marked/marked-citations.js +53 -0
  112. package/vendor/streamdown/marked/marked-cjk.d.ts +2 -0
  113. package/vendor/streamdown/marked/marked-cjk.js +165 -0
  114. package/vendor/streamdown/marked/marked-dl.d.ts +23 -0
  115. package/vendor/streamdown/marked/marked-dl.js +44 -0
  116. package/vendor/streamdown/marked/marked-footnotes.d.ts +26 -0
  117. package/vendor/streamdown/marked/marked-footnotes.js +101 -0
  118. package/vendor/streamdown/marked/marked-hr.d.ts +6 -0
  119. package/vendor/streamdown/marked/marked-hr.js +18 -0
  120. package/vendor/streamdown/marked/marked-list.d.ts +31 -0
  121. package/vendor/streamdown/marked/marked-list.js +330 -0
  122. package/vendor/streamdown/marked/marked-math.d.ts +13 -0
  123. package/vendor/streamdown/marked/marked-math.js +129 -0
  124. package/vendor/streamdown/marked/marked-mdx.d.ts +12 -0
  125. package/vendor/streamdown/marked/marked-mdx.js +124 -0
  126. package/vendor/streamdown/marked/marked-subsup.d.ts +22 -0
  127. package/vendor/streamdown/marked/marked-subsup.js +40 -0
  128. package/vendor/streamdown/marked/marked-table.d.ts +80 -0
  129. package/vendor/streamdown/marked/marked-table.js +527 -0
  130. package/vendor/streamdown/plugin-context.d.ts +19 -0
  131. package/vendor/streamdown/plugin-context.js +40 -0
  132. package/vendor/streamdown/plugins/cjk-shared.d.ts +1 -0
  133. package/vendor/streamdown/plugins/cjk-shared.js +1 -0
  134. package/vendor/streamdown/plugins.d.ts +19 -0
  135. package/vendor/streamdown/plugins.js +107 -0
  136. package/vendor/streamdown/provenance.json +229 -0
  137. package/vendor/streamdown/remend.d.ts +2 -0
  138. package/vendor/streamdown/remend.js +1 -0
  139. package/vendor/streamdown/security/html.d.ts +19 -0
  140. package/vendor/streamdown/security/html.js +329 -0
  141. package/vendor/streamdown/security/preprocess-custom-tags.d.ts +1 -0
  142. package/vendor/streamdown/security/preprocess-custom-tags.js +20 -0
  143. package/vendor/streamdown/security/preprocess-literal-tag-content.d.ts +1 -0
  144. package/vendor/streamdown/security/preprocess-literal-tag-content.js +16 -0
  145. package/vendor/streamdown/security/types.d.ts +1 -0
  146. package/vendor/streamdown/security/types.js +1 -0
  147. package/vendor/streamdown/site/SiteHeader.svelte +94 -0
  148. package/vendor/streamdown/site/SiteHeader.svelte.d.ts +6 -0
  149. package/vendor/streamdown/site/home/HomeDemo.svelte +122 -0
  150. package/vendor/streamdown/site/home/HomeDemo.svelte.d.ts +3 -0
  151. package/vendor/streamdown/site/home/HomePage.svelte +293 -0
  152. package/vendor/streamdown/site/home/HomePage.svelte.d.ts +3 -0
  153. package/vendor/streamdown/site/home/content.d.ts +69 -0
  154. package/vendor/streamdown/site/home/content.js +144 -0
  155. package/vendor/streamdown/site/metadata.d.ts +4 -0
  156. package/vendor/streamdown/site/metadata.js +4 -0
  157. package/vendor/streamdown/site/navigation.d.ts +6 -0
  158. package/vendor/streamdown/site/navigation.js +22 -0
  159. package/vendor/streamdown/streamdown/config.d.ts +41 -0
  160. package/vendor/streamdown/streamdown/config.js +125 -0
  161. package/vendor/streamdown/streamdown/context.d.ts +12 -0
  162. package/vendor/streamdown/streamdown/context.js +2 -0
  163. package/vendor/streamdown/streamdown/incomplete-markdown.d.ts +5 -0
  164. package/vendor/streamdown/streamdown/incomplete-markdown.js +79 -0
  165. package/vendor/streamdown/streamdown/pipeline.d.ts +95 -0
  166. package/vendor/streamdown/streamdown/pipeline.js +138 -0
  167. package/vendor/streamdown/streamdown/runtime.d.ts +59 -0
  168. package/vendor/streamdown/streamdown/runtime.js +112 -0
  169. package/vendor/streamdown/streaming.d.ts +5 -0
  170. package/vendor/streamdown/streaming.js +5 -0
  171. package/vendor/streamdown/theme.d.ts +486 -0
  172. package/vendor/streamdown/theme.js +370 -0
  173. package/vendor/streamdown/translations.d.ts +43 -0
  174. package/vendor/streamdown/translations.js +54 -0
  175. package/vendor/streamdown/url-policy.d.ts +1 -0
  176. package/vendor/streamdown/url-policy.js +1 -0
  177. package/vendor/streamdown/utils/bind.d.ts +8 -0
  178. package/vendor/streamdown/utils/bind.js +26 -0
  179. package/vendor/streamdown/utils/bundledLanguages.d.ts +1 -0
  180. package/vendor/streamdown/utils/bundledLanguages.js +1 -0
  181. package/vendor/streamdown/utils/code-block.d.ts +8 -0
  182. package/vendor/streamdown/utils/code-block.js +38 -0
  183. package/vendor/streamdown/utils/copy.svelte.d.ts +7 -0
  184. package/vendor/streamdown/utils/copy.svelte.js +38 -0
  185. package/vendor/streamdown/utils/darkMode.svelte.d.ts +3 -0
  186. package/vendor/streamdown/utils/darkMode.svelte.js +49 -0
  187. package/vendor/streamdown/utils/detectDirection.d.ts +1 -0
  188. package/vendor/streamdown/utils/detectDirection.js +19 -0
  189. package/vendor/streamdown/utils/get.d.ts +1 -0
  190. package/vendor/streamdown/utils/get.js +25 -0
  191. package/vendor/streamdown/utils/hightlighter.svelte.d.ts +30 -0
  192. package/vendor/streamdown/utils/hightlighter.svelte.js +524 -0
  193. package/vendor/streamdown/utils/index.d.ts +3 -0
  194. package/vendor/streamdown/utils/index.js +3 -0
  195. package/vendor/streamdown/utils/mermaid.d.ts +13 -0
  196. package/vendor/streamdown/utils/mermaid.js +49 -0
  197. package/vendor/streamdown/utils/panzoom.svelte.d.ts +35 -0
  198. package/vendor/streamdown/utils/panzoom.svelte.js +556 -0
  199. package/vendor/streamdown/utils/parse-incomplete-markdown.d.ts +2 -0
  200. package/vendor/streamdown/utils/parse-incomplete-markdown.js +2 -0
  201. package/vendor/streamdown/utils/runtime-env.d.ts +1 -0
  202. package/vendor/streamdown/utils/runtime-env.js +13 -0
  203. package/vendor/streamdown/utils/save.d.ts +1 -0
  204. package/vendor/streamdown/utils/save.js +14 -0
  205. package/vendor/streamdown/utils/scroll-lock.d.ts +2 -0
  206. package/vendor/streamdown/utils/scroll-lock.js +35 -0
  207. package/vendor/streamdown/utils/table.d.ts +8 -0
  208. package/vendor/streamdown/utils/table.js +176 -0
  209. package/vendor/streamdown/utils/url.d.ts +22 -0
  210. package/vendor/streamdown/utils/url.js +98 -0
  211. package/vendor/streamdown/utils/useClickOutside.svelte.d.ts +8 -0
  212. package/vendor/streamdown/utils/useClickOutside.svelte.js +41 -0
  213. package/vendor/streamdown/utils/useKeyDown.svelte.d.ts +5 -0
  214. package/vendor/streamdown/utils/useKeyDown.svelte.js +23 -0
@@ -0,0 +1,455 @@
1
+ <script lang="ts">
2
+ import { BROWSER } from 'esm-env';
3
+ import { onMount, tick } from 'svelte';
4
+ import { useStreamdown } from '../context.svelte.js';
5
+ import type { Tokens } from 'marked';
6
+ import type { MermaidConfig } from 'mermaid';
7
+ import { cn } from '../theme.js';
8
+ import { usePanzoom } from '../utils/panzoom.svelte';
9
+ import { useCopy } from '../utils/copy.svelte.js';
10
+ import { isTestMode } from '../utils/runtime-env.js';
11
+ import {
12
+ checkIcon,
13
+ copyIcon,
14
+ fitViewIcon,
15
+ fullscreenIcon,
16
+ resolveIcon,
17
+ zoomInIcon,
18
+ zoomOutIcon
19
+ } from './icons.js';
20
+ import MermaidDownload from './MermaidDownload.svelte';
21
+
22
+ let mermaidLibrary: any | null = null;
23
+ let mermaidLibraryPromise: Promise<any | null> | null = null;
24
+
25
+ const streamdown = useStreamdown();
26
+
27
+ const {
28
+ token,
29
+ id,
30
+ isIncomplete = false
31
+ }: {
32
+ token: Tokens.Code;
33
+ id: string;
34
+ isIncomplete?: boolean;
35
+ } = $props();
36
+
37
+ const mermaidPlugin = $derived(streamdown.plugins?.mermaid ?? null);
38
+ let container: HTMLDivElement | null = $state(null);
39
+ let mermaid = $state<any>(null);
40
+ let svgContent = $state('');
41
+ let lastValidSvg = $state('');
42
+ let error = $state<string | null>(null);
43
+ let retryCount = $state(0);
44
+ let shouldRender = $state(false);
45
+ const loadMermaidLibrary = async (): Promise<any | null> => {
46
+ if (!BROWSER) {
47
+ return null;
48
+ }
49
+
50
+ if (mermaidLibrary) {
51
+ return mermaidLibrary;
52
+ }
53
+
54
+ // Share the same client-side import across instances so concurrent Mermaid
55
+ // mounts do not race Vitest's browser-module route mocking in CI.
56
+ if (!mermaidLibraryPromise) {
57
+ mermaidLibraryPromise = import('mermaid')
58
+ .then((mod) => {
59
+ mermaidLibrary = mod.default;
60
+ return mermaidLibrary;
61
+ })
62
+ .catch((error) => {
63
+ mermaidLibraryPromise = null;
64
+ throw error;
65
+ });
66
+ }
67
+
68
+ return mermaidLibraryPromise;
69
+ };
70
+
71
+ onMount(() => {
72
+ const requestIdleCallbackWrapper =
73
+ typeof window !== 'undefined' && 'requestIdleCallback' in window
74
+ ? (callback: IdleRequestCallback, options?: IdleRequestOptions): number =>
75
+ window.requestIdleCallback(callback, options)
76
+ : (callback: IdleRequestCallback): number => {
77
+ const start = Date.now();
78
+ return window.setTimeout(() => {
79
+ callback({
80
+ didTimeout: false,
81
+ timeRemaining: () => Math.max(0, 50 - (Date.now() - start))
82
+ });
83
+ }, 1);
84
+ };
85
+ const cancelIdleCallbackWrapper =
86
+ typeof window !== 'undefined' && 'cancelIdleCallback' in window
87
+ ? (id: number) => window.cancelIdleCallback(id)
88
+ : (id: number) => window.clearTimeout(id);
89
+
90
+ if (!container) {
91
+ return;
92
+ }
93
+
94
+ let renderTimeout: number | null = null;
95
+ let idleCallback: number | null = null;
96
+ const clearPendingRenders = () => {
97
+ if (renderTimeout !== null) {
98
+ window.clearTimeout(renderTimeout);
99
+ renderTimeout = null;
100
+ }
101
+ if (idleCallback !== null) {
102
+ cancelIdleCallbackWrapper(idleCallback);
103
+ idleCallback = null;
104
+ }
105
+ };
106
+ const observer = new IntersectionObserver(
107
+ (entries) => {
108
+ for (const entry of entries) {
109
+ if (!entry.isIntersecting) {
110
+ clearPendingRenders();
111
+ continue;
112
+ }
113
+
114
+ clearPendingRenders();
115
+ renderTimeout = window.setTimeout(() => {
116
+ const records = observer.takeRecords();
117
+ const isStillInView = records.length === 0 || (records.at(-1)?.isIntersecting ?? false);
118
+ if (!isStillInView) {
119
+ return;
120
+ }
121
+
122
+ idleCallback = requestIdleCallbackWrapper(
123
+ (deadline) => {
124
+ if (deadline.timeRemaining() > 0 || deadline.didTimeout) {
125
+ shouldRender = true;
126
+ observer.disconnect();
127
+ return;
128
+ }
129
+
130
+ idleCallback = requestIdleCallbackWrapper(() => {
131
+ shouldRender = true;
132
+ observer.disconnect();
133
+ });
134
+ },
135
+ { timeout: 500 }
136
+ );
137
+ }, 300);
138
+ }
139
+ },
140
+ {
141
+ rootMargin: '300px',
142
+ threshold: 0
143
+ }
144
+ );
145
+
146
+ void (async () => {
147
+ if (!mermaidPlugin) {
148
+ try {
149
+ mermaid = await loadMermaidLibrary();
150
+ } catch (err) {
151
+ error = err instanceof Error ? err.message : 'Mermaid library not available';
152
+ }
153
+ }
154
+
155
+ if (isTestMode()) {
156
+ shouldRender = true;
157
+ return;
158
+ }
159
+
160
+ observer.observe(container);
161
+ })();
162
+
163
+ return () => {
164
+ clearPendingRenders();
165
+ observer.disconnect();
166
+ };
167
+ });
168
+
169
+ const MermaidErrorComponent = $derived(
170
+ streamdown.components?.mermaidError ?? streamdown.mermaid?.errorComponent
171
+ );
172
+ const renderedSvg = $derived(svgContent || lastValidSvg);
173
+ const chartSource = $derived(
174
+ isIncomplete && token.text.length > 0 && !token.text.endsWith('\n')
175
+ ? `${token.text}\n`
176
+ : token.text
177
+ );
178
+ const controls = $derived(streamdown.controls.mermaid);
179
+ const panzoom = usePanzoom({
180
+ enabled: () => controls.panZoom,
181
+ activateMouseWheel: () => controls.panZoom && controls.mouseWheelZoom,
182
+ minZoom: 0.5,
183
+ maxZoom: 4,
184
+ zoomSpeed: 1
185
+ });
186
+ const showCopy = $derived(streamdown.controls.code && streamdown.codeControls.copy);
187
+ const actionButtonDisabled = $derived(streamdown.isAnimating);
188
+ const showHeaderActions = $derived(
189
+ controls.enabled && (controls.download || controls.fullscreen || showCopy)
190
+ );
191
+ const showPanzoomControls = $derived(controls.enabled && controls.panZoom && !!renderedSvg);
192
+ const panzoomControlsClass = $derived(
193
+ cn(
194
+ 'absolute z-10 flex flex-col gap-1 rounded-md border border-border bg-background/80 p-1 supports-[backdrop-filter]:bg-background/70 supports-[backdrop-filter]:backdrop-blur-sm',
195
+ panzoom.expanded ? 'bottom-4 left-4' : 'bottom-2 left-2'
196
+ )
197
+ );
198
+ const mermaidSurfaceClass = $derived(
199
+ cn(
200
+ streamdown.theme.mermaid.base,
201
+ 'my-0 rounded-none border-0 bg-transparent',
202
+ panzoom.expanded ? 'min-h-0' : null
203
+ )
204
+ );
205
+ const copy = useCopy({
206
+ get content() {
207
+ return chartSource;
208
+ }
209
+ });
210
+
211
+ const createRenderId = (chart: string) => {
212
+ const chartHash = chart.split('').reduce((acc, char) => {
213
+ return ((acc << 5) - acc + char.charCodeAt(0)) | 0;
214
+ }, 0);
215
+
216
+ return `mermaid-${Math.abs(chartHash)}-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
217
+ };
218
+
219
+ const renderSvgMarkup = async (
220
+ chart: string,
221
+ mermaidConfig: MermaidConfig | undefined = streamdown.mermaidConfig,
222
+ mermaidInstance: any = mermaid
223
+ ): Promise<string> => {
224
+ const defaultConfig: MermaidConfig = {
225
+ theme: 'base',
226
+ startOnLoad: false,
227
+ securityLevel: 'strict',
228
+ fontFamily: 'monospace',
229
+ suppressErrorRendering: true,
230
+ flowchart: {
231
+ useMaxWidth: true,
232
+ htmlLabels: true,
233
+ curve: 'basis'
234
+ },
235
+ ...(mermaidConfig || {})
236
+ };
237
+
238
+ const resolvedMermaidInstance = mermaidPlugin
239
+ ? mermaidPlugin.getMermaid(defaultConfig)
240
+ : mermaidInstance;
241
+
242
+ if (!resolvedMermaidInstance) {
243
+ throw new Error('Mermaid library not available');
244
+ }
245
+
246
+ resolvedMermaidInstance.initialize(defaultConfig);
247
+
248
+ const { svg } = await resolvedMermaidInstance.render(createRenderId(chart), chart);
249
+ return svg;
250
+ };
251
+
252
+ const retryRender = () => {
253
+ retryCount += 1;
254
+ };
255
+
256
+ $effect(() => {
257
+ if (panzoom.expanded) {
258
+ shouldRender = true;
259
+ }
260
+ });
261
+
262
+ $effect(() => {
263
+ const chart = chartSource;
264
+ const currentRetry = retryCount;
265
+ const currentMermaid = mermaid;
266
+ const currentConfig = streamdown.mermaidConfig;
267
+ const scheduledToRender = shouldRender;
268
+
269
+ if (!scheduledToRender) {
270
+ return;
271
+ }
272
+
273
+ if (!mermaidPlugin && !currentMermaid) {
274
+ return;
275
+ }
276
+
277
+ let cancelled = false;
278
+
279
+ void (async () => {
280
+ try {
281
+ const svg = await renderSvgMarkup(chart, currentConfig, currentMermaid);
282
+ if (cancelled) {
283
+ return;
284
+ }
285
+
286
+ error = null;
287
+ svgContent = svg;
288
+ lastValidSvg = svg;
289
+ } catch (err) {
290
+ if (cancelled) {
291
+ return;
292
+ }
293
+ if (!(lastValidSvg || svgContent)) {
294
+ error = err instanceof Error ? err.message : 'Failed to render Mermaid chart';
295
+ }
296
+ }
297
+ })();
298
+
299
+ return () => {
300
+ cancelled = true;
301
+ };
302
+ });
303
+
304
+ $effect(() => {
305
+ const currentSvg = renderedSvg;
306
+
307
+ if (!currentSvg) {
308
+ return;
309
+ }
310
+
311
+ void tick().then(() => {
312
+ panzoom.zoomToFit();
313
+ });
314
+ });
315
+ </script>
316
+
317
+ <div bind:this={container} data-streamdown-mermaid={id}>
318
+ <div
319
+ style={streamdown.isMounted ? streamdown.animationBlockStyle : ''}
320
+ class={streamdown.theme.code.base}
321
+ data-expanded={panzoom.expanded ? 'true' : 'false'}
322
+ >
323
+ <div data-streamdown="code-block-header" class={streamdown.theme.code.header}>
324
+ <span class={streamdown.theme.code.language}>mermaid</span>
325
+ {#if showHeaderActions}
326
+ <div data-streamdown="code-block-actions" class={streamdown.theme.code.buttons}>
327
+ {#if controls.download}
328
+ <MermaidDownload
329
+ {id}
330
+ chart={chartSource}
331
+ renderSvg={() => renderSvgMarkup(chartSource)}
332
+ />
333
+ {/if}
334
+ {#if showCopy}
335
+ <button
336
+ class={streamdown.theme.components.button}
337
+ title={streamdown.translations.copyCode}
338
+ type="button"
339
+ onclick={() => {
340
+ if (!actionButtonDisabled) {
341
+ void copy.copy();
342
+ }
343
+ }}
344
+ disabled={actionButtonDisabled}
345
+ data-panzoom-ignore
346
+ >
347
+ {#if copy.isCopied}
348
+ {@render resolveIcon(streamdown.icons, 'check', checkIcon)()}
349
+ {:else}
350
+ {@render resolveIcon(streamdown.icons, 'copy', copyIcon)()}
351
+ {/if}
352
+ </button>
353
+ {/if}
354
+ {#if controls.fullscreen}
355
+ <button
356
+ class={streamdown.theme.components.button}
357
+ title={panzoom.expanded
358
+ ? streamdown.translations.exitFullscreen
359
+ : streamdown.translations.viewFullscreen}
360
+ type="button"
361
+ onclick={() => panzoom.toggleExpand()}
362
+ disabled={actionButtonDisabled}
363
+ data-panzoom-ignore
364
+ >
365
+ {@render resolveIcon(streamdown.icons, 'fullscreen', fullscreenIcon)()}
366
+ </button>
367
+ {/if}
368
+ </div>
369
+ {/if}
370
+ </div>
371
+
372
+ <div
373
+ data-streamdown="code-block-body"
374
+ data-language="mermaid"
375
+ class={streamdown.theme.code.container}
376
+ style="height: fit-content; width: 100%;"
377
+ >
378
+ <div class={mermaidSurfaceClass}>
379
+ {#if showPanzoomControls}
380
+ <div class={panzoomControlsClass}>
381
+ <button
382
+ class={streamdown.theme.components.button}
383
+ title="Zoom in"
384
+ type="button"
385
+ onclick={() => panzoom.zoomIn()}
386
+ data-panzoom-ignore
387
+ >
388
+ {@render resolveIcon(streamdown.icons, 'zoomIn', zoomInIcon)()}
389
+ </button>
390
+ <button
391
+ class={streamdown.theme.components.button}
392
+ title="Zoom out"
393
+ type="button"
394
+ onclick={() => panzoom.zoomOut()}
395
+ data-panzoom-ignore
396
+ >
397
+ {@render resolveIcon(streamdown.icons, 'zoomOut', zoomOutIcon)()}
398
+ </button>
399
+ <button
400
+ class={streamdown.theme.components.button}
401
+ title="Reset zoom and pan"
402
+ type="button"
403
+ onclick={() => panzoom.zoomToFit()}
404
+ data-panzoom-ignore
405
+ >
406
+ {@render resolveIcon(streamdown.icons, 'fitView', fitViewIcon)()}
407
+ </button>
408
+ </div>
409
+ {/if}
410
+ {#if renderedSvg}
411
+ <div {@attach panzoom.attach} role="application">
412
+ <div data-mermaid-svg aria-label="Mermaid chart" role="img">
413
+ {@html renderedSvg}
414
+ </div>
415
+ </div>
416
+ {:else if error}
417
+ {#if MermaidErrorComponent}
418
+ <MermaidErrorComponent chart={chartSource} {error} {id} retry={retryRender} />
419
+ {:else}
420
+ <div class="rounded-md bg-red-50 p-4" data-streamdown-mermaid-error>
421
+ <p class="font-mono text-sm text-red-700">Mermaid Error: {error}</p>
422
+ <details class="mt-2">
423
+ <summary class="cursor-pointer text-xs text-red-600">Show Code</summary>
424
+ <pre class="mt-2 overflow-x-auto rounded bg-red-100 p-2 text-xs text-red-800">
425
+ {chartSource}</pre>
426
+ </details>
427
+ </div>
428
+ {/if}
429
+ {:else}
430
+ <div data-mermaid-placeholder></div>
431
+ {/if}
432
+ </div>
433
+ </div>
434
+ </div>
435
+ </div>
436
+
437
+ <style>
438
+ :global([data-expanded='true']) {
439
+ position: fixed;
440
+ top: 16px;
441
+ left: 16px;
442
+ width: calc(100vw - 32px);
443
+ height: calc(100vh - 32px);
444
+ z-index: 2147483647;
445
+ margin: 0px;
446
+ }
447
+
448
+ :global(div[id^='dmermaid-']) {
449
+ position: absolute !important;
450
+ left: -9999px !important;
451
+ top: -9999px !important;
452
+ visibility: hidden !important;
453
+ pointer-events: none !important;
454
+ }
455
+ </style>
@@ -0,0 +1,9 @@
1
+ import type { Tokens } from 'marked';
2
+ type $$ComponentProps = {
3
+ token: Tokens.Code;
4
+ id: string;
5
+ isIncomplete?: boolean;
6
+ };
7
+ declare const Mermaid: import("svelte").Component<$$ComponentProps, {}, "">;
8
+ type Mermaid = ReturnType<typeof Mermaid>;
9
+ export default Mermaid;
@@ -0,0 +1,145 @@
1
+ <script lang="ts">
2
+ import { useStreamdown } from '../context.svelte.js';
3
+ import { scale } from 'svelte/transition';
4
+ import { downloadIcon, resolveIcon } from './icons.js';
5
+ import { Popover } from './popover.svelte.js';
6
+ import { useClickOutside } from '../utils/useClickOutside.svelte.js';
7
+ import { useKeyDown } from '../utils/useKeyDown.svelte.js';
8
+ import { save } from '../utils/save.js';
9
+ import { getMermaidSvgMarkup, svgToPngBlob } from '../utils/mermaid.js';
10
+
11
+ let {
12
+ id,
13
+ chart,
14
+ renderSvg
15
+ }: {
16
+ id: string;
17
+ chart: string;
18
+ renderSvg?: () => Promise<string>;
19
+ } = $props();
20
+
21
+ const streamdown = useStreamdown();
22
+ const popover = new Popover();
23
+
24
+ useKeyDown({
25
+ keys: ['Escape'],
26
+ get isActive() {
27
+ return popover.isOpen;
28
+ },
29
+ callback: () => {
30
+ popover.isOpen = false;
31
+ }
32
+ });
33
+
34
+ const clickOutside = useClickOutside({
35
+ get isActive() {
36
+ return popover.isOpen;
37
+ },
38
+ callback: () => {
39
+ popover.isOpen = false;
40
+ }
41
+ });
42
+
43
+ const getSvgMarkup = async (): Promise<string | null> => {
44
+ return getMermaidSvgMarkup({ id, renderSvg });
45
+ };
46
+
47
+ const downloadSvg = async () => {
48
+ try {
49
+ const svgString = await getSvgMarkup();
50
+ if (!svgString) return;
51
+
52
+ save('diagram.svg', svgString, 'image/svg+xml');
53
+ popover.isOpen = false;
54
+ } catch (err) {
55
+ console.error('Failed to download SVG:', err);
56
+ }
57
+ };
58
+
59
+ const downloadPng = async () => {
60
+ try {
61
+ const svgString = await getSvgMarkup();
62
+ if (!svgString) return;
63
+
64
+ const blob = await svgToPngBlob(svgString);
65
+ save('diagram.png', blob, 'image/png');
66
+ popover.isOpen = false;
67
+ } catch (err) {
68
+ console.error('Failed to download PNG:', err);
69
+ }
70
+ };
71
+
72
+ const download = async (type: 'SVG' | 'PNG') => {
73
+ if (type === 'SVG') {
74
+ await downloadSvg();
75
+ } else {
76
+ await downloadPng();
77
+ }
78
+ };
79
+ </script>
80
+
81
+ {#if popover.isOpen}
82
+ <div
83
+ id={'mermaid-download-popover'}
84
+ role="dialog"
85
+ aria-modal="false"
86
+ transition:scale|global={{ start: 0.95, duration: 100 }}
87
+ {@attach clickOutside.attachment}
88
+ {@attach popover.popoverAttachment}
89
+ style:width="fit-content !important"
90
+ style:min-width="fit-content !important"
91
+ class={streamdown.theme.components.popover}
92
+ >
93
+ {#each ['PNG', 'SVG', 'MMD'] as type}
94
+ {@const label =
95
+ type === 'PNG'
96
+ ? streamdown.translations.downloadDiagramAsPng
97
+ : type === 'SVG'
98
+ ? streamdown.translations.downloadDiagramAsSvg
99
+ : streamdown.translations.downloadDiagramAsMmd}
100
+ <button
101
+ style="width: 100%; text-align: left; justify-content: flex-start; padding: 1rem 1rem; margin: 0.2rem 0;"
102
+ onclick={async () => {
103
+ if (type === 'MMD') {
104
+ save('diagram.mmd', chart, 'text/plain');
105
+ popover.isOpen = false;
106
+ return;
107
+ }
108
+ await download(type as 'SVG' | 'PNG');
109
+ }}
110
+ class={streamdown.theme.components.button}
111
+ title={label}
112
+ type="button"
113
+ >
114
+ {type === 'PNG'
115
+ ? streamdown.translations.mermaidFormatPng
116
+ : type === 'SVG'
117
+ ? streamdown.translations.mermaidFormatSvg
118
+ : streamdown.translations.mermaidFormatMmd}
119
+ </button>
120
+ {/each}
121
+ </div>
122
+ {/if}
123
+
124
+ <button
125
+ class={streamdown.theme.components.button}
126
+ onclick={(e: MouseEvent) => {
127
+ if (streamdown.isAnimating) {
128
+ return;
129
+ }
130
+
131
+ if (popover.isOpen) {
132
+ popover.isOpen = false;
133
+ return;
134
+ }
135
+ popover.reference = e.target as HTMLButtonElement;
136
+ popover.isOpen = true;
137
+ }}
138
+ {@attach clickOutside.attachment}
139
+ title={streamdown.translations.downloadDiagram}
140
+ disabled={streamdown.isAnimating}
141
+ data-panzoom-ignore
142
+ type="button"
143
+ >
144
+ {@render resolveIcon(streamdown.icons, 'download', downloadIcon)()}
145
+ </button>
@@ -0,0 +1,8 @@
1
+ type $$ComponentProps = {
2
+ id: string;
3
+ chart: string;
4
+ renderSvg?: () => Promise<string>;
5
+ };
6
+ declare const MermaidDownload: import("svelte").Component<$$ComponentProps, {}, "">;
7
+ type MermaidDownload = ReturnType<typeof MermaidDownload>;
8
+ export default MermaidDownload;
@@ -0,0 +1,23 @@
1
+ <script lang="ts">
2
+ import type { Component, Snippet } from 'svelte';
3
+
4
+ let {
5
+ children,
6
+ render,
7
+ component: ComponentOverride,
8
+ props
9
+ }: {
10
+ children: Snippet;
11
+ render?: Snippet<[any]>;
12
+ component?: Component<any>;
13
+ props: Record<string, any>;
14
+ } = $props();
15
+ </script>
16
+
17
+ {#if render}
18
+ {@render render(props)}
19
+ {:else if ComponentOverride}
20
+ <ComponentOverride {...props} />
21
+ {:else}
22
+ {@render children()}
23
+ {/if}
@@ -0,0 +1,10 @@
1
+ import type { Component, Snippet } from 'svelte';
2
+ type $$ComponentProps = {
3
+ children: Snippet;
4
+ render?: Snippet<[any]>;
5
+ component?: Component<any>;
6
+ props: Record<string, any>;
7
+ };
8
+ declare const Slot: Component<$$ComponentProps, {}, "">;
9
+ type Slot = ReturnType<typeof Slot>;
10
+ export default Slot;