@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,200 @@
1
+ <script lang="ts">
2
+ import { useStreamdown } from '../context.svelte.js';
3
+ import { applyMarkdownUrlTransform, createMarkdownElement } from '../markdown.js';
4
+ import { save } from '../utils/save.js';
5
+ import { isPathRelativeUrl, transformUrl } from '../utils/url.js';
6
+ import Slot from './Slot.svelte';
7
+ import { downloadIcon, resolveIcon } from './icons.js';
8
+ import type { Tokens } from 'marked';
9
+ import type { Snippet } from 'svelte';
10
+
11
+ const streamdown = useStreamdown();
12
+
13
+ const {
14
+ children,
15
+ token,
16
+ id
17
+ }: {
18
+ children: Snippet;
19
+ token: Tokens.Image;
20
+ id: string;
21
+ } = $props();
22
+
23
+ const imageSource = $derived.by(() => {
24
+ if (token.href === 'streamdown:incomplete-image') {
25
+ return null;
26
+ }
27
+
28
+ const transformedHref = applyMarkdownUrlTransform(
29
+ token.href,
30
+ 'src',
31
+ createMarkdownElement('img', {
32
+ alt: token.text,
33
+ src: token.href,
34
+ title: token.title ?? undefined
35
+ }),
36
+ streamdown.urlTransform
37
+ );
38
+
39
+ if (typeof transformedHref !== 'string' || transformedHref.length === 0) {
40
+ return null;
41
+ }
42
+
43
+ if (isPathRelativeUrl(transformedHref)) {
44
+ return transformedHref;
45
+ }
46
+
47
+ return (
48
+ transformUrl(
49
+ transformedHref,
50
+ streamdown.allowedImagePrefixes ?? [],
51
+ streamdown.defaultOrigin,
52
+ {
53
+ kind: 'image'
54
+ }
55
+ ) ?? null
56
+ );
57
+ });
58
+
59
+ let isLoaded = $state(false);
60
+ let hasError = $state(false);
61
+ let imageElement = $state<HTMLImageElement | null>(null);
62
+
63
+ function getImageSource(): string | null {
64
+ return imageSource;
65
+ }
66
+
67
+ function getExtensionFromBlob(blob: Blob): string {
68
+ const type = blob.type.toLowerCase();
69
+ if (type === 'image/svg+xml') return 'svg';
70
+ if (type === 'image/jpeg') return 'jpg';
71
+ if (type === 'image/gif') return 'gif';
72
+ if (type === 'image/webp') return 'webp';
73
+ return 'png';
74
+ }
75
+
76
+ function getFilename(source: string, blob: Blob): string {
77
+ const cleanUrl = source.split('#')[0].split('?')[0];
78
+ const lastSegment = cleanUrl.split('/').filter(Boolean).at(-1);
79
+ if (lastSegment && /\.[a-z0-9]+$/i.test(lastSegment)) {
80
+ return lastSegment;
81
+ }
82
+
83
+ const baseName = token.text?.trim() || 'image';
84
+ return `${baseName}.${getExtensionFromBlob(blob)}`;
85
+ }
86
+
87
+ async function downloadImage(): Promise<void> {
88
+ const source = getImageSource();
89
+ if (!source) {
90
+ return;
91
+ }
92
+
93
+ const response = await fetch(source);
94
+ const blob = await response.blob();
95
+ save(getFilename(source, blob), blob, blob.type || 'application/octet-stream');
96
+ }
97
+
98
+ function handleLoad(): void {
99
+ isLoaded = true;
100
+ hasError = false;
101
+ }
102
+
103
+ function handleError(): void {
104
+ hasError = true;
105
+ isLoaded = false;
106
+ }
107
+
108
+ $effect(() => {
109
+ const source = getImageSource();
110
+ if (!imageElement || !source) {
111
+ return;
112
+ }
113
+
114
+ if (!imageElement.complete) {
115
+ return;
116
+ }
117
+
118
+ if (imageElement.naturalWidth > 0) {
119
+ handleLoad();
120
+ return;
121
+ }
122
+
123
+ handleError();
124
+ });
125
+ </script>
126
+
127
+ {#if token.href !== 'streamdown:incomplete-image'}
128
+ {#if imageSource}
129
+ <Slot
130
+ props={{
131
+ src: imageSource,
132
+ alt: token.text,
133
+ class: streamdown.theme.image.image,
134
+ onload: handleLoad,
135
+ onerror: handleError,
136
+ children,
137
+ token
138
+ }}
139
+ render={streamdown.snippets.image}
140
+ >
141
+ <span
142
+ data-streamdown-image={id}
143
+ style={streamdown.isMounted ? streamdown.animationBlockStyle : ''}
144
+ class={streamdown.theme.image.base}
145
+ >
146
+ {#if hasError}
147
+ <span data-streamdown-image-fallback={id}>
148
+ {streamdown.translations.imageNotAvailable}
149
+ </span>
150
+ {:else}
151
+ <Slot
152
+ props={{
153
+ src: imageSource,
154
+ alt: token.text,
155
+ class: streamdown.theme.image.image,
156
+ onload: handleLoad,
157
+ onerror: handleError,
158
+ children,
159
+ token
160
+ }}
161
+ component={streamdown.components?.img}
162
+ >
163
+ <img
164
+ bind:this={imageElement}
165
+ class={streamdown.theme.image.image}
166
+ src={imageSource}
167
+ alt={token.text}
168
+ onload={handleLoad}
169
+ onerror={handleError}
170
+ />
171
+ </Slot>
172
+ {/if}
173
+ <div
174
+ aria-hidden="true"
175
+ class="pointer-events-none absolute inset-0 hidden rounded-lg bg-black/10 group-hover:block"
176
+ ></div>
177
+ {#if isLoaded && !hasError}
178
+ <button
179
+ class="absolute right-2 bottom-2 flex h-8 w-8 cursor-pointer items-center justify-center rounded-md border border-border bg-background/90 text-muted-foreground opacity-0 shadow-sm backdrop-blur-sm transition-all duration-200 hover:bg-background hover:text-foreground group-hover:opacity-100"
180
+ type="button"
181
+ title={streamdown.translations.downloadImage}
182
+ aria-label={streamdown.translations.downloadImage}
183
+ onclick={downloadImage}
184
+ >
185
+ {@render resolveIcon(streamdown.icons, 'download', downloadIcon)()}
186
+ </button>
187
+ {/if}
188
+ </span>
189
+ </Slot>
190
+ {:else}
191
+ <p>
192
+ <span
193
+ data-streamdown-image-blocked={id}
194
+ class="inline-block rounded bg-gray-200 px-3 py-1 text-sm text-gray-600 dark:bg-gray-700 dark:text-gray-400"
195
+ >
196
+ [Image blocked: {token.text || 'No description'}]
197
+ </span>
198
+ </p>
199
+ {/if}
200
+ {/if}
@@ -0,0 +1,10 @@
1
+ import type { Tokens } from 'marked';
2
+ import type { Snippet } from 'svelte';
3
+ type $$ComponentProps = {
4
+ children: Snippet;
5
+ token: Tokens.Image;
6
+ id: string;
7
+ };
8
+ declare const Image: import("svelte").Component<$$ComponentProps, {}, "">;
9
+ type Image = ReturnType<typeof Image>;
10
+ export default Image;
@@ -0,0 +1,220 @@
1
+ <script lang="ts">
2
+ import { useStreamdown, type LinkSafetyModalProps } from '../context.svelte.js';
3
+ import { applyMarkdownUrlTransform, createMarkdownElement } from '../markdown.js';
4
+ import { isPathRelativeUrl, transformUrl } from '../utils/url.js';
5
+ import Slot from './Slot.svelte';
6
+ import LinkSafetyModal from './LinkSafetyModal.svelte';
7
+ import type { Tokens } from 'marked';
8
+ import type { Snippet } from 'svelte';
9
+
10
+ const streamdown = useStreamdown();
11
+
12
+ const {
13
+ children,
14
+ token,
15
+ id
16
+ }: {
17
+ children: Snippet;
18
+ token: Tokens.Link;
19
+ id: string;
20
+ } = $props();
21
+
22
+ const resolvedLink = $derived.by(() => {
23
+ if (token.href === 'streamdown:incomplete-link') {
24
+ return {
25
+ href: undefined,
26
+ rel: undefined,
27
+ state: 'blocked' as const,
28
+ target: undefined,
29
+ isIncomplete: true,
30
+ isRelative: false
31
+ };
32
+ }
33
+
34
+ const transformedHref = applyMarkdownUrlTransform(
35
+ token.href,
36
+ 'href',
37
+ createMarkdownElement('a', {
38
+ href: token.href,
39
+ title: token.title ?? undefined
40
+ }),
41
+ streamdown.urlTransform
42
+ );
43
+
44
+ if (transformedHref == null) {
45
+ return {
46
+ href: undefined,
47
+ rel: undefined,
48
+ state: 'literal' as const,
49
+ target: undefined,
50
+ isIncomplete: false,
51
+ isRelative: false
52
+ };
53
+ }
54
+
55
+ if (transformedHref === '') {
56
+ return {
57
+ href: '',
58
+ rel: undefined,
59
+ state: 'anchor' as const,
60
+ target: undefined,
61
+ isIncomplete: false,
62
+ isRelative: false
63
+ };
64
+ }
65
+
66
+ if (isPathRelativeUrl(transformedHref)) {
67
+ return {
68
+ href: transformedHref,
69
+ rel: undefined,
70
+ state: 'anchor' as const,
71
+ target: undefined,
72
+ isIncomplete: false,
73
+ isRelative: true
74
+ };
75
+ }
76
+
77
+ const safeHref = transformUrl(
78
+ transformedHref,
79
+ streamdown.allowedLinkPrefixes ?? [],
80
+ streamdown.defaultOrigin,
81
+ {
82
+ kind: 'link'
83
+ }
84
+ );
85
+
86
+ if (safeHref) {
87
+ return {
88
+ href: safeHref,
89
+ rel: 'noopener noreferrer',
90
+ state: 'anchor' as const,
91
+ target: '_blank',
92
+ isIncomplete: false,
93
+ isRelative: false
94
+ };
95
+ }
96
+
97
+ return {
98
+ href: undefined,
99
+ rel: undefined,
100
+ state: 'blocked' as const,
101
+ target: undefined,
102
+ isIncomplete: false,
103
+ isRelative: false
104
+ };
105
+ });
106
+
107
+ const shouldIntercept = $derived(
108
+ Boolean(
109
+ streamdown.linkSafety?.enabled &&
110
+ resolvedLink.state === 'anchor' &&
111
+ resolvedLink.href &&
112
+ !resolvedLink.isRelative &&
113
+ !resolvedLink.isIncomplete
114
+ )
115
+ );
116
+ const splitMailtoLabel = $derived.by(() => {
117
+ if (!token.href.startsWith('mailto:')) {
118
+ return null;
119
+ }
120
+
121
+ const text = typeof token.text === 'string' ? token.text : '';
122
+ if (!text.startsWith('mailto:')) {
123
+ return null;
124
+ }
125
+
126
+ return {
127
+ prefix: 'mailto:',
128
+ label: text.slice('mailto:'.length)
129
+ };
130
+ });
131
+ const customModal = $derived(
132
+ streamdown.linkSafety?.renderModal as Snippet<[LinkSafetyModalProps]> | undefined
133
+ );
134
+ let isModalOpen = $state(false);
135
+
136
+ const openHref = () => {
137
+ if (typeof window === 'undefined' || !resolvedLink.href || resolvedLink.isIncomplete) {
138
+ return;
139
+ }
140
+
141
+ window.open(resolvedLink.href, '_blank', 'noreferrer');
142
+ };
143
+
144
+ const handleInterceptedClick = async (event: MouseEvent) => {
145
+ if (!shouldIntercept || resolvedLink.isIncomplete || !resolvedLink.href) {
146
+ return;
147
+ }
148
+
149
+ event.preventDefault();
150
+
151
+ try {
152
+ if (streamdown.linkSafety?.onLinkCheck) {
153
+ const isAllowed = await streamdown.linkSafety.onLinkCheck(resolvedLink.href);
154
+ if (isAllowed) {
155
+ openHref();
156
+ return;
157
+ }
158
+ }
159
+ } catch {
160
+ // Fall through to the confirmation modal when the checker fails closed.
161
+ }
162
+
163
+ isModalOpen = true;
164
+ };
165
+
166
+ const modalProps = $derived({
167
+ url: resolvedLink.href ?? '',
168
+ isOpen: isModalOpen,
169
+ onClose: () => {
170
+ isModalOpen = false;
171
+ },
172
+ onConfirm: openHref
173
+ } satisfies LinkSafetyModalProps);
174
+ </script>
175
+
176
+ {#if resolvedLink.state === 'anchor'}
177
+ {#if shouldIntercept}
178
+ {#if splitMailtoLabel}{splitMailtoLabel.prefix}{/if}
179
+ <button
180
+ type="button"
181
+ data-streamdown="link"
182
+ data-streamdown-link={id}
183
+ data-incomplete={resolvedLink.isIncomplete ? 'true' : undefined}
184
+ class={`${streamdown.theme.link.base} appearance-none border-none bg-transparent p-0 text-left`}
185
+ onclick={(event) => void handleInterceptedClick(event)}
186
+ >{#if splitMailtoLabel}{splitMailtoLabel.label}{:else}{@render children()}{/if}</button>{#if customModal}{@render customModal(modalProps)}{:else}<LinkSafetyModal {...modalProps} />{/if}
187
+ {:else}
188
+ <Slot
189
+ props={{
190
+ href: resolvedLink.href,
191
+ target: resolvedLink.target,
192
+ rel: resolvedLink.rel,
193
+ title: token.title,
194
+ class: streamdown.theme.link.base,
195
+ children,
196
+ token
197
+ }}
198
+ render={streamdown.snippets.link}
199
+ component={streamdown.components?.a}
200
+ >
201
+ <a
202
+ data-streamdown="link"
203
+ data-streamdown-link={id}
204
+ data-incomplete={resolvedLink.isIncomplete ? 'true' : undefined}
205
+ class={streamdown.theme.link.base}
206
+ href={resolvedLink.href}
207
+ target={resolvedLink.target}
208
+ rel={resolvedLink.rel}
209
+ >{#if splitMailtoLabel}{splitMailtoLabel.prefix}{splitMailtoLabel.label}{:else}{@render children()}{/if}</a>
210
+ </Slot>
211
+ {/if}
212
+ {:else if resolvedLink.state === 'literal'}
213
+ {'['}{@render children()}{']'}
214
+ {:else}
215
+ <span
216
+ data-streamdown-link-blocked={id}
217
+ class={streamdown.theme.link.blocked}
218
+ title={`Blocked URL: ${resolvedLink.href}`}
219
+ >{@render children()} [blocked]</span>
220
+ {/if}
@@ -0,0 +1,10 @@
1
+ import type { Tokens } from 'marked';
2
+ import type { Snippet } from 'svelte';
3
+ type $$ComponentProps = {
4
+ children: Snippet;
5
+ token: Tokens.Link;
6
+ id: string;
7
+ };
8
+ declare const Link: import("svelte").Component<$$ComponentProps, {}, "">;
9
+ type Link = ReturnType<typeof Link>;
10
+ export default Link;
@@ -0,0 +1,121 @@
1
+ <script lang="ts">
2
+ import { useStreamdown, type LinkSafetyModalProps } from '../context.svelte.js';
3
+ import { useCopy } from '../utils/copy.svelte.js';
4
+ import { lockBodyScroll } from '../utils/scroll-lock.js';
5
+ import { checkIcon, copyIcon, resolveIcon } from './icons.js';
6
+
7
+ let { url, isOpen, onClose, onConfirm }: LinkSafetyModalProps = $props();
8
+
9
+ const streamdown = useStreamdown();
10
+ const copy = useCopy({
11
+ get content() {
12
+ return url;
13
+ }
14
+ });
15
+
16
+ const handleCopy = async () => {
17
+ await copy.copy();
18
+ };
19
+
20
+ const handleConfirm = () => {
21
+ onConfirm();
22
+ onClose();
23
+ };
24
+
25
+ $effect(() => {
26
+ if (!isOpen || typeof document === 'undefined') {
27
+ return;
28
+ }
29
+
30
+ const releaseBodyScroll = lockBodyScroll(document);
31
+
32
+ const handleEscape = (event: KeyboardEvent) => {
33
+ if (event.key === 'Escape') {
34
+ onClose();
35
+ }
36
+ };
37
+
38
+ document.addEventListener('keydown', handleEscape);
39
+
40
+ return () => {
41
+ document.removeEventListener('keydown', handleEscape);
42
+ releaseBodyScroll();
43
+ };
44
+ });
45
+ </script>
46
+
47
+ {#if isOpen}
48
+ <div
49
+ data-streamdown="link-safety-modal"
50
+ class="fixed inset-0 z-50 flex items-center justify-center bg-black/45 px-4 backdrop-blur-sm"
51
+ role="button"
52
+ tabindex="0"
53
+ aria-label={streamdown.translations.close}
54
+ onclick={onClose}
55
+ onkeydown={(event) => {
56
+ if (event.key === 'Escape') {
57
+ onClose();
58
+ }
59
+ }}
60
+ >
61
+ <div
62
+ role="presentation"
63
+ class="relative flex w-full max-w-md flex-col gap-4 rounded-xl border border-gray-200 bg-white p-6 shadow-xl"
64
+ onclick={(event) => event.stopPropagation()}
65
+ onkeydown={(event) => event.stopPropagation()}
66
+ >
67
+ <button
68
+ type="button"
69
+ class="absolute top-4 right-4 rounded-md px-2 py-1 text-sm text-gray-500 transition-colors hover:bg-gray-100 hover:text-gray-900"
70
+ title={streamdown.translations.close}
71
+ aria-label={streamdown.translations.close}
72
+ onclick={onClose}
73
+ >
74
+ {streamdown.translations.close}
75
+ </button>
76
+
77
+ <div class="flex flex-col gap-2 pr-12">
78
+ <h2 class="text-lg font-semibold text-gray-900">
79
+ {streamdown.translations.openExternalLink}
80
+ </h2>
81
+ <p class="text-sm text-gray-600">{streamdown.translations.externalLinkWarning}</p>
82
+ </div>
83
+
84
+ <div
85
+ class={`rounded-md border border-gray-200 bg-gray-50 p-3 font-mono text-sm break-all text-gray-700 ${
86
+ url.length > 100 ? 'max-h-32 overflow-y-auto' : ''
87
+ }`}
88
+ >
89
+ {url}
90
+ </div>
91
+
92
+ <div class="flex gap-2">
93
+ <button
94
+ type="button"
95
+ class="flex flex-1 items-center justify-center gap-2 rounded-md border border-gray-200 bg-white px-4 py-2 text-sm font-medium text-gray-700 transition-colors hover:bg-gray-50"
96
+ onclick={() => void handleCopy()}
97
+ >
98
+ <span class="h-4 w-4">
99
+ {#if copy.isCopied}
100
+ {@render resolveIcon(streamdown.icons, 'check', checkIcon)()}
101
+ {:else}
102
+ {@render resolveIcon(streamdown.icons, 'copy', copyIcon)()}
103
+ {/if}
104
+ </span>
105
+ <span
106
+ >{copy.isCopied
107
+ ? streamdown.translations.copied
108
+ : streamdown.translations.copyLink}</span
109
+ >
110
+ </button>
111
+ <button
112
+ type="button"
113
+ class="flex flex-1 items-center justify-center rounded-md bg-gray-900 px-4 py-2 text-sm font-medium text-white transition-colors hover:bg-black"
114
+ onclick={handleConfirm}
115
+ >
116
+ {streamdown.translations.openLink}
117
+ </button>
118
+ </div>
119
+ </div>
120
+ </div>
121
+ {/if}
@@ -0,0 +1,4 @@
1
+ import { type LinkSafetyModalProps } from '../context.svelte.js';
2
+ declare const LinkSafetyModal: import("svelte").Component<LinkSafetyModalProps, {}, "">;
3
+ type LinkSafetyModal = ReturnType<typeof LinkSafetyModal>;
4
+ export default LinkSafetyModal;
@@ -0,0 +1,100 @@
1
+ <script lang="ts">
2
+ import { BROWSER } from 'esm-env';
3
+ import { onMount, untrack } from 'svelte';
4
+ import { useStreamdown } from '../context.svelte.js';
5
+ import type { MathToken } from '../marked/index.js';
6
+ import { getMathPluginOptions } from '../plugins.js';
7
+ import { isTestMode } from '../utils/runtime-env.js';
8
+ import type { KatexOptions } from 'katex';
9
+ import 'katex/dist/katex.min.css';
10
+
11
+ type KatexRenderer = Pick<typeof import('katex'), 'renderToString'>;
12
+
13
+ const {
14
+ token,
15
+ id
16
+ }: {
17
+ token: MathToken;
18
+ id: string;
19
+ } = $props();
20
+
21
+ const streamdown = useStreamdown();
22
+ const mathPluginOptions = $derived(getMathPluginOptions(streamdown.plugins?.math));
23
+ let katexInstance = $state<KatexRenderer | null>(null);
24
+ const loadKatex = async (): Promise<KatexRenderer | null> => {
25
+ if (!BROWSER) {
26
+ return null;
27
+ }
28
+
29
+ return await import('katex');
30
+ };
31
+ const escapeHtml = (value: string): string =>
32
+ value
33
+ .replaceAll('&', '&amp;')
34
+ .replaceAll('<', '&lt;')
35
+ .replaceAll('>', '&gt;')
36
+ .replaceAll('"', '&quot;')
37
+ .replaceAll("'", '&#39;');
38
+
39
+ onMount(() => {
40
+ void loadKatex().then((instance) => {
41
+ if (instance) {
42
+ katexInstance = instance;
43
+ }
44
+ });
45
+ });
46
+
47
+ let inner = $state<HTMLElement | null>(null);
48
+ const html = $derived.by(() => {
49
+ const code = token.text;
50
+ if (!katexInstance) {
51
+ return '';
52
+ }
53
+ const config: KatexOptions = {
54
+ output: 'htmlAndMathml',
55
+ displayMode: !token.isInline,
56
+ throwOnError: false,
57
+ errorColor: mathPluginOptions.errorColor,
58
+ ...(typeof streamdown.katexConfig === 'function'
59
+ ? streamdown.katexConfig(token.isInline)
60
+ : streamdown.katexConfig || {})
61
+ };
62
+
63
+ if (config.strict === undefined && isTestMode()) {
64
+ config.strict = 'ignore';
65
+ }
66
+
67
+ try {
68
+ return katexInstance.renderToString(code, config);
69
+ } catch (error) {
70
+ const message = error instanceof Error ? error.message : 'KaTeX render failed';
71
+ return untrack(() => {
72
+ return `<span title="${escapeHtml(`ParseError: ${message}`)}">${inner?.innerHTML || escapeHtml(code)}</span>`;
73
+ });
74
+ }
75
+ });
76
+ </script>
77
+
78
+ {#if token.isInline}
79
+ <span
80
+ data-streamdown-inline-math={id}
81
+ style={streamdown.isMounted ? streamdown.animationBlockStyle : ''}
82
+ bind:this={inner}
83
+ class={streamdown.theme.math.inline}
84
+ >
85
+ {@html html}
86
+ </span>
87
+ {:else}
88
+ <div
89
+ data-streamdown-block-math={id}
90
+ style={streamdown.isMounted ? streamdown.animationBlockStyle : ''}
91
+ style:height="fit-content"
92
+ style:width="100%"
93
+ >
94
+ <div class="overflow-x-auto">
95
+ <div bind:this={inner} class={streamdown.theme.math.block}>
96
+ {@html html}
97
+ </div>
98
+ </div>
99
+ </div>
100
+ {/if}
@@ -0,0 +1,9 @@
1
+ import type { MathToken } from '../marked/index.js';
2
+ import 'katex/dist/katex.min.css';
3
+ type $$ComponentProps = {
4
+ token: MathToken;
5
+ id: string;
6
+ };
7
+ declare const Math: import("svelte").Component<$$ComponentProps, {}, "">;
8
+ type Math = ReturnType<typeof Math>;
9
+ export default Math;