svelte-streamdown 2.2.3 → 2.2.5
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 +1 -0
- package/dist/AnimatedBlock.svelte +1 -3
- package/dist/AnimatedText.svelte +2 -3
- package/dist/Elements/Code.svelte +31 -28
- package/dist/Elements/Element.svelte +1 -3
- package/dist/Elements/FootnoteRef.svelte +1 -2
- package/dist/Elements/Math.svelte +1 -3
- package/dist/Elements/Mermaid.svelte +0 -3
- package/dist/Streamdown.svelte +1 -0
- package/dist/context.svelte.d.ts +3 -2
- package/dist/context.svelte.js +12 -5
- package/dist/marked/index.js +1 -1
- package/dist/theme.d.ts +3 -0
- package/dist/theme.js +4 -2
- package/dist/utils/hightlighter.svelte.d.ts +2 -2
- package/dist/utils/hightlighter.svelte.js +4 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -386,6 +386,7 @@ This heading will use a custom component!`;
|
|
|
386
386
|
| `animation.duration` | `number` | `500` | Animation duration in milliseconds |
|
|
387
387
|
| `animation.timingFunction`| `'ease' \| 'ease-in' \| 'ease-out' \| 'ease-in-out' \| 'linear'` | `'ease-in'` | CSS timing function for animations |
|
|
388
388
|
| `animation.tokenize` | `'word' \| 'char'` | `'word'` | Tokenization method for text animations |
|
|
389
|
+
| `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 |
|
|
389
390
|
|
|
390
391
|
### Custom Component Props
|
|
391
392
|
|
|
@@ -5,11 +5,9 @@
|
|
|
5
5
|
let { children }: { children: Snippet } = $props();
|
|
6
6
|
|
|
7
7
|
const streamdown = useStreamdown();
|
|
8
|
-
|
|
9
|
-
const isMounted = streamdown.isMounted;
|
|
10
8
|
</script>
|
|
11
9
|
|
|
12
|
-
{#if isMounted}
|
|
10
|
+
{#if streamdown.isMounted}
|
|
13
11
|
<div style={streamdown.animationBlockStyle}>
|
|
14
12
|
{@render children()}
|
|
15
13
|
</div>
|
package/dist/AnimatedText.svelte
CHANGED
|
@@ -16,12 +16,11 @@
|
|
|
16
16
|
|
|
17
17
|
return text.split(splitRegex).filter((token) => token.length > 0);
|
|
18
18
|
};
|
|
19
|
+
|
|
19
20
|
const tokens = $derived(tokenizeNewContent(text));
|
|
20
|
-
|
|
21
|
-
const isMounted = streamdown.isMounted;
|
|
22
21
|
</script>
|
|
23
22
|
|
|
24
|
-
{#if isMounted}
|
|
23
|
+
{#if streamdown.isMounted}
|
|
25
24
|
<span>
|
|
26
25
|
{#each tokens as token}
|
|
27
26
|
<span style={streamdown.animationTextStyle}>
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
import { useCopy } from '../utils/copy.svelte.js';
|
|
5
5
|
import { HighlighterManager, languageExtensionMap } from '../utils/hightlighter.svelte.js';
|
|
6
6
|
import type { Tokens } from 'marked';
|
|
7
|
+
import { type ThemedToken } from 'shiki';
|
|
7
8
|
|
|
8
9
|
const {
|
|
9
10
|
token
|
|
@@ -41,6 +42,8 @@
|
|
|
41
42
|
$effect(() => {
|
|
42
43
|
void highlighter.load(theme, language as any);
|
|
43
44
|
});
|
|
45
|
+
|
|
46
|
+
const isMounted = streamdown.isMounted;
|
|
44
47
|
</script>
|
|
45
48
|
|
|
46
49
|
<div class={streamdown.theme.code.base} data-language={language}>
|
|
@@ -65,37 +68,37 @@
|
|
|
65
68
|
{/if}
|
|
66
69
|
</div>
|
|
67
70
|
<div style="height: fit-content; width: 100%;" class={streamdown.theme.code.container}>
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
{
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
language as any,
|
|
76
|
-
theme,
|
|
77
|
-
streamdown.theme.code.pre
|
|
78
|
-
)}
|
|
79
|
-
{@html code}
|
|
80
|
-
{:else}
|
|
81
|
-
{@render Skeleton()}
|
|
82
|
-
{/if}
|
|
83
|
-
</div>
|
|
71
|
+
{#if highlighter.isReady(theme, language as any)}
|
|
72
|
+
{@const tokens = highlighter.highlightCode(codeContent, language as any, theme)}
|
|
73
|
+
<pre class={streamdown.theme.code.pre}><code>{@render Tokens(tokens)}</code></pre>
|
|
74
|
+
{:else}
|
|
75
|
+
<pre class={streamdown.theme.code.pre}><code>{@render Skeleton(token.text.split('\n'))}</code
|
|
76
|
+
></pre>
|
|
77
|
+
{/if}
|
|
84
78
|
</div>
|
|
85
79
|
</div>
|
|
86
80
|
|
|
87
|
-
{#snippet
|
|
88
|
-
{
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
>
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
81
|
+
{#snippet Tokens(tokens: ThemedToken[][])}
|
|
82
|
+
{#each tokens as line}
|
|
83
|
+
<span class={streamdown.theme.code.line}>
|
|
84
|
+
{#each line as token}
|
|
85
|
+
<span
|
|
86
|
+
style={isMounted ? streamdown.animationTextStyle : undefined}
|
|
87
|
+
style:color={token.color}
|
|
88
|
+
style:background-color={token.bgColor}
|
|
89
|
+
>
|
|
90
|
+
{token.content}
|
|
91
|
+
</span>
|
|
92
|
+
{/each}
|
|
93
|
+
</span>
|
|
94
|
+
{/each}
|
|
95
|
+
{/snippet}
|
|
96
|
+
{#snippet Skeleton(lines: string[])}
|
|
97
|
+
{#each lines as line}
|
|
98
|
+
<span class={streamdown.theme.code.line}>
|
|
99
|
+
{line.trim().length > 0 ? line : '\u200B'}
|
|
100
|
+
</span>
|
|
101
|
+
{/each}
|
|
99
102
|
{/snippet}
|
|
100
103
|
{#snippet copyIcon()}
|
|
101
104
|
<svg
|
|
@@ -12,8 +12,6 @@
|
|
|
12
12
|
import FootnoteRef from './FootnoteRef.svelte';
|
|
13
13
|
let { token, children }: { token: StreamdownToken; children: Snippet } = $props();
|
|
14
14
|
const streamdown = useStreamdown();
|
|
15
|
-
|
|
16
|
-
const isMounted = streamdown.isMounted;
|
|
17
15
|
</script>
|
|
18
16
|
|
|
19
17
|
{#if token.type === 'heading'}
|
|
@@ -93,7 +91,7 @@
|
|
|
93
91
|
{:else if token.type === 'list_item'}
|
|
94
92
|
<Slot props={{ children, token }} render={streamdown.snippets.li}>
|
|
95
93
|
<li
|
|
96
|
-
style={isMounted ? streamdown.animationBlockStyle : undefined}
|
|
94
|
+
style={streamdown.isMounted ? streamdown.animationBlockStyle : undefined}
|
|
97
95
|
style:list-style-type={token.task ? 'none' : undefined}
|
|
98
96
|
{...token.value && !token.task ? { value: token.value } : {}}
|
|
99
97
|
class={streamdown.theme.li.base}
|
|
@@ -79,7 +79,6 @@
|
|
|
79
79
|
off();
|
|
80
80
|
};
|
|
81
81
|
};
|
|
82
|
-
const isMounted = streamdown.isMounted;
|
|
83
82
|
</script>
|
|
84
83
|
|
|
85
84
|
{#if isOpen}
|
|
@@ -113,7 +112,7 @@
|
|
|
113
112
|
render={streamdown.snippets.footnoteRef}
|
|
114
113
|
>
|
|
115
114
|
<button
|
|
116
|
-
style={isMounted ? streamdown.animationBlockStyle : ''}
|
|
115
|
+
style={streamdown.isMounted ? streamdown.animationBlockStyle : ''}
|
|
117
116
|
bind:this={reference}
|
|
118
117
|
class={streamdown.theme.footnoteRef.base}
|
|
119
118
|
onclick={() => (isOpen = !isOpen)}
|
|
@@ -45,13 +45,11 @@
|
|
|
45
45
|
});
|
|
46
46
|
}
|
|
47
47
|
});
|
|
48
|
-
|
|
49
|
-
const isMounted = streamdown.isMounted;
|
|
50
48
|
</script>
|
|
51
49
|
|
|
52
50
|
{#if isInline}
|
|
53
51
|
<span
|
|
54
|
-
style={isMounted ? streamdown.animationBlockStyle : ''}
|
|
52
|
+
style={streamdown.isMounted ? streamdown.animationBlockStyle : ''}
|
|
55
53
|
bind:this={inner}
|
|
56
54
|
class={streamdown.theme.math.inline}
|
|
57
55
|
>
|
|
@@ -197,9 +197,6 @@
|
|
|
197
197
|
panzoom.zoomToFit();
|
|
198
198
|
} catch (err) {
|
|
199
199
|
const sanitizedCode = sanitizeMermaidCode(code);
|
|
200
|
-
console.error('Mermaid rendering error:', err);
|
|
201
|
-
console.error('Original code:', code);
|
|
202
|
-
console.error('Sanitized code:', sanitizedCode);
|
|
203
200
|
}
|
|
204
201
|
};
|
|
205
202
|
</script>
|
package/dist/Streamdown.svelte
CHANGED
package/dist/context.svelte.d.ts
CHANGED
|
@@ -21,8 +21,8 @@ export declare class StreamdownContext {
|
|
|
21
21
|
footnotes: Map<string, Footnote>;
|
|
22
22
|
};
|
|
23
23
|
isMounted: boolean;
|
|
24
|
-
animationTextStyle: string;
|
|
25
|
-
animationBlockStyle: string;
|
|
24
|
+
animationTextStyle: string | undefined;
|
|
25
|
+
animationBlockStyle: string | undefined;
|
|
26
26
|
constructor(props: Omit<StreamdownProps, keyof Snippets | 'class'> & {
|
|
27
27
|
snippets: Snippets;
|
|
28
28
|
});
|
|
@@ -103,6 +103,7 @@ export type StreamdownProps = {
|
|
|
103
103
|
};
|
|
104
104
|
renderHtml?: boolean | ((token: Tokens.HTML | Tokens.Tag) => string);
|
|
105
105
|
animation?: {
|
|
106
|
+
animateOnMount?: boolean;
|
|
106
107
|
enabled?: boolean;
|
|
107
108
|
type?: 'fade' | 'blur' | 'typewriter' | 'slideUp' | 'slideDown';
|
|
108
109
|
duration?: number;
|
package/dist/context.svelte.js
CHANGED
|
@@ -4,22 +4,29 @@ export class StreamdownContext {
|
|
|
4
4
|
refs: new Map(),
|
|
5
5
|
footnotes: new Map()
|
|
6
6
|
};
|
|
7
|
-
isMounted =
|
|
8
|
-
animationTextStyle = $derived(
|
|
7
|
+
isMounted = false;
|
|
8
|
+
animationTextStyle = $derived(this.animation.enabled
|
|
9
|
+
? `animation-name: sd-${this.animation.type};
|
|
9
10
|
animation-duration: ${this.animation.duration}ms;
|
|
10
11
|
animation-timing-function: ${this.animation.timingFunction};
|
|
11
12
|
animation-iteration-count: 1;
|
|
12
13
|
animation-fill-mode: forwards;
|
|
13
14
|
white-space: pre-wrap;
|
|
14
|
-
display: inline-block;`
|
|
15
|
-
|
|
15
|
+
display: inline-block;`
|
|
16
|
+
: undefined);
|
|
17
|
+
animationBlockStyle = $derived(this.animation.enabled
|
|
18
|
+
? `animation-name: sd-${this.animation.type};
|
|
16
19
|
animation-duration: ${this.animation.duration}ms;
|
|
17
20
|
animation-timing-function: ${this.animation.timingFunction};
|
|
18
21
|
animation-iteration-count: 1;
|
|
19
|
-
animation-fill-mode: forwards;`
|
|
22
|
+
animation-fill-mode: forwards;`
|
|
23
|
+
: undefined);
|
|
20
24
|
constructor(props) {
|
|
21
25
|
bind(this, props);
|
|
22
26
|
setContext('streamdown', this);
|
|
27
|
+
if (this.animation.animateOnMount) {
|
|
28
|
+
this.isMounted = true;
|
|
29
|
+
}
|
|
23
30
|
onMount(() => {
|
|
24
31
|
this.isMounted = true;
|
|
25
32
|
});
|
package/dist/marked/index.js
CHANGED
|
@@ -49,7 +49,7 @@ const parseExtensions = (...ext) => {
|
|
|
49
49
|
};
|
|
50
50
|
const blockLexer = new Lexer(parseExtensions(markedFootnote(), markedTable()));
|
|
51
51
|
export const lex = (markdown) => {
|
|
52
|
-
return new Lexer(parseExtensions(
|
|
52
|
+
return new Lexer(parseExtensions(markedTable(), markedFootnote(), markedAlert(), markedMath(), markedSubSup(), markedList()))
|
|
53
53
|
.lex(markdown)
|
|
54
54
|
.filter((token) => token.type !== 'space' && token.type !== 'footnote');
|
|
55
55
|
};
|
package/dist/theme.d.ts
CHANGED
|
@@ -44,6 +44,7 @@ export declare const theme: {
|
|
|
44
44
|
language: string;
|
|
45
45
|
skeleton: string;
|
|
46
46
|
pre: string;
|
|
47
|
+
line: string;
|
|
47
48
|
};
|
|
48
49
|
codespan: {
|
|
49
50
|
base: string;
|
|
@@ -171,6 +172,7 @@ export declare const shadcnTheme: {
|
|
|
171
172
|
language: string;
|
|
172
173
|
skeleton: string;
|
|
173
174
|
pre: string;
|
|
175
|
+
line: string;
|
|
174
176
|
};
|
|
175
177
|
codespan: {
|
|
176
178
|
base: string;
|
|
@@ -299,6 +301,7 @@ export declare const mergeTheme: (customTheme?: Partial<Theme>, baseTheme?: "tai
|
|
|
299
301
|
language: string;
|
|
300
302
|
skeleton: string;
|
|
301
303
|
pre: string;
|
|
304
|
+
line: string;
|
|
302
305
|
};
|
|
303
306
|
codespan: {
|
|
304
307
|
base: string;
|
package/dist/theme.js
CHANGED
|
@@ -44,7 +44,8 @@ export const theme = {
|
|
|
44
44
|
button: 'cursor-pointer size-6 p-1 text-gray-600 transition-all hover:text-gray-900 rounded hover:bg-gray-100',
|
|
45
45
|
language: 'ml-1 font-mono lowercase',
|
|
46
46
|
skeleton: 'rounded-md font-mono text-transparent bg-gray-200 scale-y-90 animate-pulse whitespace-nowrap inline-block',
|
|
47
|
-
pre: 'overflow-x-auto font-mono p-0 bg-gray-100/40'
|
|
47
|
+
pre: 'overflow-x-auto font-mono p-0 bg-gray-100/40',
|
|
48
|
+
line: 'block'
|
|
48
49
|
},
|
|
49
50
|
codespan: {
|
|
50
51
|
base: 'bg-gray-100 rounded px-1.5 py-0.5 font-mono text-[0.9em]'
|
|
@@ -171,7 +172,8 @@ export const shadcnTheme = {
|
|
|
171
172
|
button: 'cursor-pointer size-6 p-1 text-muted-foreground transition-all hover:text-foreground rounded hover:bg-muted',
|
|
172
173
|
language: 'ml-1 font-mono lowercase',
|
|
173
174
|
skeleton: 'rounded-md font-mono text-transparent bg-border/80 scale-y-90 w-fit animate-pulse whitespace-nowrap inline-block',
|
|
174
|
-
pre: 'overflow-x-auto font-mono p-0 bg-muted/40'
|
|
175
|
+
pre: 'overflow-x-auto font-mono p-0 bg-muted/40',
|
|
176
|
+
line: 'block '
|
|
175
177
|
},
|
|
176
178
|
codespan: {
|
|
177
179
|
base: 'bg-muted rounded px-1.5 py-0.5 font-mono text-foreground text-[0.9em]'
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type BundledLanguage, type BundledTheme } from 'shiki';
|
|
1
|
+
import { type BundledLanguage, type BundledTheme, type ThemedToken } from 'shiki';
|
|
2
2
|
import { SvelteSet } from 'svelte/reactivity';
|
|
3
3
|
export declare const loadShiki: () => Promise<[any, import("shiki").CreateHighlighterFactory<BundledLanguage, BundledTheme>]>;
|
|
4
4
|
declare class HighlighterManager {
|
|
@@ -23,7 +23,7 @@ declare class HighlighterManager {
|
|
|
23
23
|
/**
|
|
24
24
|
* Highlights code synchronously. Must call isReady() first.
|
|
25
25
|
*/
|
|
26
|
-
highlightCode(code: string, language: BundledLanguage, theme: BundledTheme
|
|
26
|
+
highlightCode(code: string, language: BundledLanguage, theme: BundledTheme): ThemedToken[][];
|
|
27
27
|
static create(preloadedThemes: BundledTheme[]): HighlighterManager;
|
|
28
28
|
}
|
|
29
29
|
export { HighlighterManager };
|
|
@@ -4,10 +4,6 @@ import { SvelteMap, SvelteSet } from 'svelte/reactivity';
|
|
|
4
4
|
const isLanguageSupported = (language) => {
|
|
5
5
|
return Object.hasOwn(bundledLanguages, language);
|
|
6
6
|
};
|
|
7
|
-
// Remove background styles from <pre> tags (inline style)
|
|
8
|
-
const removePreBackground = (html) => {
|
|
9
|
-
return html.replace(/<pre[^>]*style="[^"]*background[^";]*;?[^"]*"[^>]*>/g, (match) => match.replace(/style="[^"]*background[^";]*;?[^"]*"/, ''));
|
|
10
|
-
};
|
|
11
7
|
export const loadShiki = async () => {
|
|
12
8
|
return Promise.all([
|
|
13
9
|
import('shiki/engine/javascript').then((mod) => mod.createJavaScriptRegexEngine({ forgiving: true })),
|
|
@@ -79,21 +75,16 @@ class HighlighterManager {
|
|
|
79
75
|
/**
|
|
80
76
|
* Highlights code synchronously. Must call isReady() first.
|
|
81
77
|
*/
|
|
82
|
-
highlightCode(code, language, theme
|
|
78
|
+
highlightCode(code, language, theme) {
|
|
83
79
|
const highlighter = this.highlighters.get(`${theme}:${language}`);
|
|
84
80
|
if (!highlighter) {
|
|
85
|
-
return
|
|
81
|
+
return [];
|
|
86
82
|
}
|
|
87
|
-
|
|
83
|
+
const tokens = highlighter.codeToTokensBase(code, {
|
|
88
84
|
lang: isLanguageSupported(language) ? language : 'text',
|
|
89
85
|
theme: theme
|
|
90
86
|
});
|
|
91
|
-
|
|
92
|
-
html = removePreBackground(html);
|
|
93
|
-
if (preClassName) {
|
|
94
|
-
html = html.replace(/<pre(\s|>)/, `<pre class="${preClassName}"$1`);
|
|
95
|
-
}
|
|
96
|
-
return html;
|
|
87
|
+
return tokens;
|
|
97
88
|
}
|
|
98
89
|
static create(preloadedThemes) {
|
|
99
90
|
if (typeof window !== 'undefined' && 'STREAMDOWN_HIGHLIGHTER' in window) {
|