svelte-streamdown 2.6.1 → 3.0.1

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/README.md CHANGED
@@ -99,7 +99,8 @@ $$
99
99
  - Pan and Zoom
100
100
  - Full screen mode
101
101
 
102
- **Example:**
102
+
103
+ # **Example:**
103
104
 
104
105
  ```mermaid
105
106
  graph TD
@@ -462,6 +463,45 @@ This heading will use a custom component!`;
462
463
  />
463
464
  ```
464
465
 
466
+ ## 📦 Bundle Optimization
467
+
468
+ Streamdown is optimized for minimal bundle size by making heavy components **opt-in**. By default, Code blocks, Mermaid diagrams, and Math expressions render as lightweight fallbacks (plain text). To enable full functionality, import and pass the components you need:
469
+
470
+ ### Enabling Heavy Components
471
+
472
+ ```svelte
473
+ <script>
474
+ import { Streamdown } from 'svelte-streamdown';
475
+ // Import only the components you need
476
+ import Code from 'svelte-streamdown/code'; // Shiki syntax highlighting
477
+ import Mermaid from 'svelte-streamdown/mermaid'; // Mermaid diagrams
478
+ import Math from 'svelte-streamdown/math'; // KaTeX math rendering
479
+ </script>
480
+
481
+ <Streamdown
482
+ {content}
483
+ components={{ code: Code, mermaid: Mermaid, math: Math }}
484
+ />
485
+ ```
486
+
487
+ ### Component Dependencies
488
+
489
+ | Component | Import Path | Dependency | Size Impact |
490
+ |-----------|-------------|------------|-------------|
491
+ | `Code` | `svelte-streamdown/code` | Shiki | ~2MB (languages + themes) |
492
+ | `Mermaid` | `svelte-streamdown/mermaid` | Mermaid.js | ~1.5MB |
493
+ | `Math` | `svelte-streamdown/math` | KaTeX | ~300KB |
494
+
495
+ > [!TIP]
496
+ > Only import the components your application actually uses. If your content doesn't include code blocks, mermaid diagrams, or math expressions, you can skip those imports entirely for a much smaller bundle.
497
+
498
+ ### Fallback Behavior
499
+
500
+ When a heavy component is not provided:
501
+ - **Code blocks**: Render as plain `<pre><code>` without syntax highlighting
502
+ - **Mermaid**: Renders the mermaid source as a code block
503
+ - **Math**: Renders the raw LaTeX/math text
504
+
465
505
  ## 📋 Props API
466
506
 
467
507
  | Prop | Type | Default | Description |
@@ -479,7 +519,9 @@ This heading will use a custom component!`;
479
519
  | `theme` | `DeepPartial<Theme>` | - | Custom theme overrides |
480
520
  | `baseTheme` | `'tailwind' \| 'shadcn'` | `'tailwind'` | Base theme to use before applying overrides |
481
521
  | `mergeTheme` | `boolean` | `true` | Whether to merge theme with base theme |
482
- | `shikiTheme` | `BundledTheme` | `'github-light'` | Code highlighting theme |
522
+ | `shikiTheme` | `string` | `'github-light'` | Code highlighting theme (`github-dark` or `github-light` by default, or custom theme key) |
523
+ | `shikiThemes` | `Record<string, ThemeRegistration>` | - | Additional themes as pre-imported objects (e.g., `{ nord: nordTheme }`) |
524
+ | `shikiLanguages` | `LanguageInfo[]` | - | Additional syntax highlighting languages (merged with defaults) |
483
525
  | `mermaidConfig` | `MermaidConfig` | - | Mermaid diagram configuration |
484
526
  | `katexConfig` | `KatexOptions \| ((inline: boolean) => KatexOptions)` | - | KaTeX math rendering options |
485
527
  | `animation` | `AnimationConfig` | - | Animation configuration for streaming content |
@@ -491,6 +533,7 @@ This heading will use a custom component!`;
491
533
  | `animation.animateOnMount` | `boolean` | `false` | Run the token animation on mount or not, useful if you render the Streamdown component in the same time as the first token is receive from the LLM |
492
534
  | `extensions` | `Array<Extension>` | `[]` | Custom marked tokenizers to render special markdown blocks or inline tokens |
493
535
  | `mdxComponents` | `Record<string, Component>` | `{}` | Map of MDX component names to Svelte components (e.g., `{ Card, Button }`) |
536
+ | `components` | `{ code?, mermaid?, math? }` | - | Optional heavy components for syntax highlighting, diagrams, and math rendering |
494
537
  | `children` | `Snippet<[{token:GenericToken, streamdown: StreamdownContext, children: Snippet` | `undefined` | Snippet used to render elements not supported by Streamdown, custom extensions, and MDX components |
495
538
 
496
539
  #### All Available Customizable Elements:
@@ -760,7 +803,7 @@ The `mdx` snippet receives three parameters:
760
803
  - `children`: Snippet containing parsed markdown content
761
804
 
762
805
  Use `token.tagName` to determine which component is being rendered:
763
-
806
+ <Card title="Hello" count={5}>Content</Card>
764
807
  ```svelte
765
808
  <!-- Markdown: <Card title="Hello" count={5}>Content</Card> -->
766
809
  <Streamdown {content}>
package/dist/Block.svelte CHANGED
@@ -7,13 +7,17 @@
7
7
  import { getContext } from 'svelte';
8
8
 
9
9
  let {
10
- block
10
+ block,
11
+ static: isStatic = false
11
12
  }: {
12
13
  block: string;
14
+ static?: boolean;
13
15
  } = $props();
14
16
 
15
17
  const streamdown = useStreamdown();
16
- const tokens = $derived(lex(parseIncompleteMarkdown(block.trim()), streamdown.extensions));
18
+ const tokens = $derived(
19
+ lex(isStatic ? block : parseIncompleteMarkdown(block.trim()), streamdown.extensions)
20
+ );
17
21
  const insidePopover = getContext('POPOVER');
18
22
  </script>
19
23
 
@@ -24,8 +28,8 @@
24
28
  {@const isTextOnlyNode = children.length === 0}
25
29
  <Element {token}>
26
30
  {#if isTextOnlyNode}
27
- {#if streamdown.animation.enabled && !insidePopover}
28
- <AnimatedText text={'text' in token ? token.text : ''} />
31
+ {#if streamdown.animation.enabled && !insidePopover && !isStatic}
32
+ <AnimatedText text={'text' in token ? token.text || '' : ''} />
29
33
  {:else}
30
34
  {'text' in token ? token.text : ''}
31
35
  {/if}
@@ -1,5 +1,6 @@
1
1
  type $$ComponentProps = {
2
2
  block: string;
3
+ static?: boolean;
3
4
  };
4
5
  declare const Block: import("svelte").Component<$$ComponentProps, {}, "">;
5
6
  type Block = ReturnType<typeof Block>;
@@ -3,6 +3,7 @@
3
3
  import { save } from '../utils/save.js';
4
4
  import { useCopy } from '../utils/copy.svelte.js';
5
5
  import { HighlighterManager, languageExtensionMap } from '../utils/hightlighter.svelte.js';
6
+ import { bundledLanguagesInfo } from '../utils/bundledLanguages.js';
6
7
  import type { Tokens } from 'marked';
7
8
  import { type ThemedToken } from 'shiki';
8
9
  import { untrack } from 'svelte';
@@ -17,7 +18,11 @@
17
18
  } = $props();
18
19
 
19
20
  const streamdown = useStreamdown();
20
- const highlighter = HighlighterManager.create(streamdown.shikiPreloadThemes);
21
+ const highlighter = HighlighterManager.create(
22
+ bundledLanguagesInfo,
23
+ streamdown.shikiThemes,
24
+ streamdown.shikiLanguages
25
+ );
21
26
 
22
27
  const copy = useCopy({
23
28
  get content() {
@@ -1,10 +1,7 @@
1
1
  <script lang="ts">
2
2
  import type { Snippet } from 'svelte';
3
3
  import Link from './Link.svelte';
4
- import Code from './Code.svelte';
5
4
  import Image from './Image.svelte';
6
- import Mermaid from './Mermaid.svelte';
7
- import Math from './Math.svelte';
8
5
  import Alert from './Alert.svelte';
9
6
  import type { StreamdownToken } from '../marked/index.js';
10
7
  import Slot from './Slot.svelte';
@@ -12,9 +9,16 @@
12
9
  import FootnoteRef from './FootnoteRef.svelte';
13
10
  import Citation from './Citation.svelte';
14
11
  import TableDownload from './TableDownload.svelte';
12
+ // Import fallback components
13
+ import { CodeFallback, MermaidFallback, MathFallback } from './fallbacks/index.js';
15
14
  let { token, children }: { token: StreamdownToken; children: Snippet } = $props();
16
15
  const streamdown = useStreamdown();
17
16
 
17
+ // Use provided components or fallback to lightweight versions
18
+ const CodeComponent = $derived(streamdown.components?.code ?? CodeFallback);
19
+ const MermaidComponent = $derived(streamdown.components?.mermaid ?? MermaidFallback);
20
+ const MathComponent = $derived(streamdown.components?.math ?? MathFallback);
21
+
18
22
  // Only apply animation on block level elements. Leaves text elements to be animated by their text children.
19
23
  const style = $derived(streamdown.isMounted ? streamdown.animationBlockStyle : '');
20
24
  const id = $props.id();
@@ -68,11 +72,11 @@
68
72
  </Slot>
69
73
  {:else if token.type === 'code' && token.lang === 'mermaid'}
70
74
  <Slot props={{ children, token }} render={streamdown.snippets.code}>
71
- <Mermaid {id} {token} />
75
+ <MermaidComponent {id} {token} />
72
76
  </Slot>
73
77
  {:else if token.type === 'code'}
74
78
  <Slot props={{ children, token }} render={streamdown.snippets.code}>
75
- <Code {id} {token} />
79
+ <CodeComponent {id} {token} />
76
80
  </Slot>
77
81
  {:else if token.type === 'codespan'}
78
82
  <Slot props={{ children, token }} render={streamdown.snippets.codespan}>
@@ -240,7 +244,7 @@
240
244
  }}
241
245
  render={streamdown.snippets.math}
242
246
  >
243
- <Math {id} {token} />
247
+ <MathComponent {id} {token} />
244
248
  </Slot>
245
249
  {:else if token.type === 'alert'}
246
250
  <Alert {id} {token} {children} />
@@ -37,6 +37,8 @@
37
37
  popover.isOpen = false;
38
38
  }
39
39
  });
40
+
41
+ $inspect(token);
40
42
  </script>
41
43
 
42
44
  {#if popover.isOpen}
@@ -48,7 +50,7 @@
48
50
  {@attach clickOutside.attachment}
49
51
  {@attach popover.popoverAttachment}
50
52
  open
51
- class={`${streamdown.theme.components.popover}`}
53
+ class={streamdown.theme.components.popover}
52
54
  >
53
55
  <Slot
54
56
  props={{
@@ -6,6 +6,7 @@
6
6
  import { on } from 'svelte/events';
7
7
  import { usePanzoom } from '../utils/panzoom.svelte';
8
8
  import { fitViewIcon, fullscreenIcon, zoomInIcon, zoomOutIcon } from './icons.js';
9
+ import MermaidDownload from './MermaidDownload.svelte';
9
10
 
10
11
  const streamdown = useStreamdown();
11
12
 
@@ -254,6 +255,7 @@
254
255
  >
255
256
  {@render (streamdown.icons?.fullscreen || fullscreenIcon)()}
256
257
  </button>
258
+ <MermaidDownload {id} />
257
259
  </div>
258
260
  {/if}
259
261
  <svg {@attach panzoom.attach} data-mermaid-svg></svg>
@@ -0,0 +1,196 @@
1
+ <script lang="ts">
2
+ import { useStreamdown } from '../context.svelte.js';
3
+ import { scale } from 'svelte/transition';
4
+ import { downloadIcon } 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
+
10
+ let {
11
+ id
12
+ }: {
13
+ id: string;
14
+ } = $props();
15
+
16
+ const streamdown = useStreamdown();
17
+ const popover = new Popover();
18
+
19
+ useKeyDown({
20
+ keys: ['Escape'],
21
+ get isActive() {
22
+ return popover.isOpen;
23
+ },
24
+ callback: () => {
25
+ popover.isOpen = false;
26
+ }
27
+ });
28
+
29
+ const clickOutside = useClickOutside({
30
+ get isActive() {
31
+ return popover.isOpen;
32
+ },
33
+ callback: () => {
34
+ popover.isOpen = false;
35
+ }
36
+ });
37
+
38
+ const getSvgElement = (): SVGSVGElement | null => {
39
+ const container = document.querySelector(`[data-streamdown-mermaid="${id}"]`);
40
+ if (!container) return null;
41
+
42
+ const svgContainer = container.querySelector('[data-mermaid-svg]');
43
+ if (!svgContainer) return null;
44
+
45
+ // The actual SVG is rendered inside the data-mermaid-svg container
46
+ const svg = svgContainer.querySelector('svg');
47
+ return svg;
48
+ };
49
+
50
+ const downloadSvg = () => {
51
+ const svg = getSvgElement();
52
+ if (!svg) return;
53
+
54
+ // Clone the SVG to avoid modifying the original
55
+ const clonedSvg = svg.cloneNode(true) as SVGSVGElement;
56
+
57
+ // Ensure the SVG has proper xmlns
58
+ clonedSvg.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
59
+ clonedSvg.setAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink');
60
+
61
+ // Get computed styles and inline them for standalone SVG
62
+ const styles = getComputedStyle(svg);
63
+ if (!clonedSvg.getAttribute('width')) {
64
+ clonedSvg.setAttribute('width', styles.width);
65
+ }
66
+ if (!clonedSvg.getAttribute('height')) {
67
+ clonedSvg.setAttribute('height', styles.height);
68
+ }
69
+
70
+ const svgString = new XMLSerializer().serializeToString(clonedSvg);
71
+ save('mermaid-diagram.svg', svgString, 'image/svg+xml');
72
+ popover.isOpen = false;
73
+ };
74
+
75
+ const downloadPng = async () => {
76
+ const svg = getSvgElement();
77
+ if (!svg) return;
78
+
79
+ // Clone the SVG to avoid modifying the original
80
+ const clonedSvg = svg.cloneNode(true) as SVGSVGElement;
81
+
82
+ // Ensure the SVG has proper xmlns
83
+ clonedSvg.setAttribute('xmlns', 'http://www.w3.org/2000/svg');
84
+ clonedSvg.setAttribute('xmlns:xlink', 'http://www.w3.org/1999/xlink');
85
+
86
+ // Get dimensions
87
+ const bbox = svg.getBBox();
88
+ const styles = getComputedStyle(svg);
89
+ const width = parseFloat(styles.width) || bbox.width || 800;
90
+ const height = parseFloat(styles.height) || bbox.height || 600;
91
+
92
+ // Set explicit dimensions on the cloned SVG
93
+ clonedSvg.setAttribute('width', String(width));
94
+ clonedSvg.setAttribute('height', String(height));
95
+
96
+ // Serialize SVG to string
97
+ const svgString = new XMLSerializer().serializeToString(clonedSvg);
98
+
99
+ // Use data URL instead of blob URL to avoid tainted canvas issues
100
+ const svgDataUrl = 'data:image/svg+xml;base64,' + btoa(unescape(encodeURIComponent(svgString)));
101
+
102
+ // Create an image to load the SVG
103
+ const img = new Image();
104
+
105
+ img.onload = () => {
106
+ // Create a canvas with 2x scale for better quality
107
+ const scale = 2;
108
+ const canvas = document.createElement('canvas');
109
+ canvas.width = width * scale;
110
+ canvas.height = height * scale;
111
+
112
+ const ctx = canvas.getContext('2d');
113
+ if (!ctx) {
114
+ return;
115
+ }
116
+
117
+ // Fill with white background (optional, remove for transparent)
118
+ ctx.fillStyle = '#ffffff';
119
+ ctx.fillRect(0, 0, canvas.width, canvas.height);
120
+
121
+ // Scale and draw the image
122
+ ctx.scale(scale, scale);
123
+ ctx.drawImage(img, 0, 0);
124
+
125
+ // Convert to PNG and download
126
+ canvas.toBlob((blob) => {
127
+ if (blob) {
128
+ const url = URL.createObjectURL(blob);
129
+ const link = document.createElement('a');
130
+ link.href = url;
131
+ link.download = 'mermaid-diagram.png';
132
+ document.body.appendChild(link);
133
+ link.click();
134
+ document.body.removeChild(link);
135
+ URL.revokeObjectURL(url);
136
+ }
137
+ }, 'image/png');
138
+ };
139
+
140
+ img.onerror = () => {
141
+ console.error('Failed to load SVG for PNG conversion');
142
+ };
143
+
144
+ img.src = svgDataUrl;
145
+ popover.isOpen = false;
146
+ };
147
+
148
+ const download = (type: 'SVG' | 'PNG') => {
149
+ if (type === 'SVG') {
150
+ downloadSvg();
151
+ } else {
152
+ downloadPng();
153
+ }
154
+ };
155
+ </script>
156
+
157
+ {#if popover.isOpen}
158
+ <dialog
159
+ id={'mermaid-download-popover'}
160
+ aria-modal="false"
161
+ transition:scale|global={{ start: 0.95, duration: 100 }}
162
+ {@attach clickOutside.attachment}
163
+ {@attach popover.popoverAttachment}
164
+ open
165
+ style:width="fit-content !important"
166
+ style:min-width="fit-content !important"
167
+ class={streamdown.theme.components.popover}
168
+ >
169
+ {#each ['PNG', 'SVG'] as type}
170
+ <button
171
+ style="width: 100%; text-align: left; justify-content: flex-start; padding: 1rem 1rem; margin: 0.2rem 0;"
172
+ onclick={() => download(type as 'SVG' | 'PNG')}
173
+ class={streamdown.theme.components.button}
174
+ >
175
+ {type}
176
+ </button>
177
+ {/each}
178
+ </dialog>
179
+ {/if}
180
+
181
+ <button
182
+ class={streamdown.theme.components.button}
183
+ onclick={(e: MouseEvent) => {
184
+ if (popover.isOpen) {
185
+ popover.isOpen = false;
186
+ return;
187
+ }
188
+ popover.reference = e.target as HTMLButtonElement;
189
+ popover.isOpen = true;
190
+ }}
191
+ {@attach clickOutside.attachment}
192
+ title="Download diagram"
193
+ data-panzoom-ignore
194
+ >
195
+ {@render (streamdown.icons?.download || downloadIcon)()}
196
+ </button>
@@ -0,0 +1,6 @@
1
+ type $$ComponentProps = {
2
+ id: string;
3
+ };
4
+ declare const MermaidDownload: import("svelte").Component<$$ComponentProps, {}, "">;
5
+ type MermaidDownload = ReturnType<typeof MermaidDownload>;
6
+ export default MermaidDownload;
@@ -0,0 +1,33 @@
1
+ <script lang="ts">
2
+ import { useStreamdown } from '../../context.svelte.js';
3
+ import type { Tokens } from 'marked';
4
+
5
+ const {
6
+ token,
7
+ id
8
+ }: {
9
+ token: Tokens.Code;
10
+ id: string;
11
+ } = $props();
12
+
13
+ const streamdown = useStreamdown();
14
+ </script>
15
+
16
+ <div
17
+ data-streamdown-code={id}
18
+ style={streamdown.isMounted ? streamdown.animationBlockStyle : ''}
19
+ class={streamdown.theme.code.base}
20
+ >
21
+ <div class={streamdown.theme.code.header}>
22
+ <span class={streamdown.theme.code.language}>{token.lang}</span>
23
+ </div>
24
+ <div style="height: fit-content; width: 100%;" class={streamdown.theme.code.container}>
25
+ <pre class={streamdown.theme.code.pre}><code
26
+ >{#each token.text.split('\n') as line}<span class={streamdown.theme.code.line}
27
+ ><span style={streamdown.isMounted ? streamdown.animationTextStyle : ''}
28
+ >{line.trim().length > 0 ? line : '\u200B'}</span
29
+ ></span
30
+ >{/each}</code
31
+ ></pre>
32
+ </div>
33
+ </div>
@@ -0,0 +1,8 @@
1
+ import type { Tokens } from 'marked';
2
+ type $$ComponentProps = {
3
+ token: Tokens.Code;
4
+ id: string;
5
+ };
6
+ declare const CodeFallback: import("svelte").Component<$$ComponentProps, {}, "">;
7
+ type CodeFallback = ReturnType<typeof CodeFallback>;
8
+ export default CodeFallback;
@@ -0,0 +1,35 @@
1
+ <script lang="ts">
2
+ import { useStreamdown } from '../../context.svelte.js';
3
+ import type { MathToken } from '../../marked/index.js';
4
+
5
+ const {
6
+ token,
7
+ id
8
+ }: {
9
+ token: MathToken;
10
+ id: string;
11
+ } = $props();
12
+
13
+ const streamdown = useStreamdown();
14
+ </script>
15
+
16
+ {#if token.isInline}
17
+ <span
18
+ data-streamdown-inline-math={id}
19
+ style={streamdown.isMounted ? streamdown.animationBlockStyle : ''}
20
+ class={streamdown.theme.math.inline}
21
+ >
22
+ <code>{token.text}</code>
23
+ </span>
24
+ {:else}
25
+ <div
26
+ data-streamdown-block-math={id}
27
+ style={streamdown.isMounted ? streamdown.animationBlockStyle : ''}
28
+ style:height="fit-content"
29
+ style:width="100%"
30
+ >
31
+ <div class="overflow-x-auto">
32
+ <pre class={streamdown.theme.math.block}><code>{token.text}</code></pre>
33
+ </div>
34
+ </div>
35
+ {/if}
@@ -0,0 +1,8 @@
1
+ import type { MathToken } from '../../marked/index.js';
2
+ type $$ComponentProps = {
3
+ token: MathToken;
4
+ id: string;
5
+ };
6
+ declare const MathFallback: import("svelte").Component<$$ComponentProps, {}, "">;
7
+ type MathFallback = ReturnType<typeof MathFallback>;
8
+ export default MathFallback;
@@ -0,0 +1,34 @@
1
+ <script lang="ts">
2
+ import { useStreamdown } from '../../context.svelte.js';
3
+ import type { Tokens } from 'marked';
4
+
5
+ const {
6
+ token,
7
+ id
8
+ }: {
9
+ token: Tokens.Code;
10
+ id: string;
11
+ } = $props();
12
+
13
+ const streamdown = useStreamdown();
14
+ </script>
15
+
16
+ <div data-streamdown-mermaid={id}>
17
+ <div
18
+ style={streamdown.isMounted ? streamdown.animationBlockStyle : ''}
19
+ class={streamdown.theme.code.base}
20
+ >
21
+ <div class={streamdown.theme.code.header}>
22
+ <span class={streamdown.theme.code.language}>mermaid</span>
23
+ </div>
24
+ <div style="height: fit-content; width: 100%;" class={streamdown.theme.code.container}>
25
+ <pre class={streamdown.theme.code.pre}><code
26
+ >{#each token.text.split('\n') as line}<span class={streamdown.theme.code.line}
27
+ ><span style={streamdown.isMounted ? streamdown.animationTextStyle : ''}
28
+ >{line.trim().length > 0 ? line : '\u200B'}</span
29
+ ></span
30
+ >{/each}</code
31
+ ></pre>
32
+ </div>
33
+ </div>
34
+ </div>
@@ -0,0 +1,8 @@
1
+ import type { Tokens } from 'marked';
2
+ type $$ComponentProps = {
3
+ token: Tokens.Code;
4
+ id: string;
5
+ };
6
+ declare const MermaidFallback: import("svelte").Component<$$ComponentProps, {}, "">;
7
+ type MermaidFallback = ReturnType<typeof MermaidFallback>;
8
+ export default MermaidFallback;
@@ -0,0 +1,3 @@
1
+ export { default as CodeFallback } from './CodeFallback.svelte';
2
+ export { default as MermaidFallback } from './MermaidFallback.svelte';
3
+ export { default as MathFallback } from './MathFallback.svelte';
@@ -0,0 +1,3 @@
1
+ export { default as CodeFallback } from './CodeFallback.svelte';
2
+ export { default as MermaidFallback } from './MermaidFallback.svelte';
3
+ export { default as MathFallback } from './MathFallback.svelte';