Skip to content

Commit 2ef2b8b

Browse files
committed
perf: move marked extension registration to module-level and remove unnecessary async wrapper
- Move marked.use() calls to <script context="module"> so extensions are registered once per module load instead of once per component instance, preventing duplicate extension stacking on the global marked singleton - Remove unnecessary async IIFE around the reactive lexer statement since marked.lexer() is synchronous, eliminating needless Promise/microtask overhead
1 parent 547c587 commit 2ef2b8b

1 file changed

Lines changed: 27 additions & 26 deletions

File tree

src/lib/components/chat/Messages/Markdown.svelte

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,34 @@
1-
<script>
1+
<script context="module">
22
import { marked } from 'marked';
3-
import { replaceTokens, processResponseContent } from '$lib/utils';
4-
import { user } from '$lib/stores';
53
64
import markedExtension from '$lib/utils/marked/extension';
75
import markedKatexExtension from '$lib/utils/marked/katex-extension';
86
import { disableSingleTilde } from '$lib/utils/marked/strikethrough-extension';
97
import { mentionExtension } from '$lib/utils/marked/mention-extension';
10-
11-
import MarkdownTokens from './Markdown/MarkdownTokens.svelte';
128
import footnoteExtension from '$lib/utils/marked/footnote-extension';
139
import citationExtension from '$lib/utils/marked/citation-extension';
1410
11+
const options = {
12+
throwOnError: false,
13+
breaks: true
14+
};
15+
16+
marked.use(markedKatexExtension(options));
17+
marked.use(markedExtension(options));
18+
marked.use(citationExtension(options));
19+
marked.use(footnoteExtension(options));
20+
marked.use(disableSingleTilde);
21+
marked.use({
22+
extensions: [mentionExtension({ triggerChar: '@' }), mentionExtension({ triggerChar: '#' })]
23+
});
24+
</script>
25+
26+
<script>
27+
import { replaceTokens, processResponseContent } from '$lib/utils';
28+
import { user } from '$lib/stores';
29+
30+
import MarkdownTokens from './Markdown/MarkdownTokens.svelte';
31+
1532
export let id = '';
1633
export let content;
1734
export let done = true;
@@ -35,27 +52,11 @@
3552
3653
let tokens = [];
3754
38-
const options = {
39-
throwOnError: false,
40-
breaks: true
41-
};
42-
43-
marked.use(markedKatexExtension(options));
44-
marked.use(markedExtension(options));
45-
marked.use(citationExtension(options));
46-
marked.use(footnoteExtension(options));
47-
marked.use(disableSingleTilde);
48-
marked.use({
49-
extensions: [mentionExtension({ triggerChar: '@' }), mentionExtension({ triggerChar: '#' })]
50-
});
51-
52-
$: (async () => {
53-
if (content) {
54-
tokens = marked.lexer(
55-
replaceTokens(processResponseContent(content), model?.name, $user?.name)
56-
);
57-
}
58-
})();
55+
$: if (content) {
56+
tokens = marked.lexer(
57+
replaceTokens(processResponseContent(content), model?.name, $user?.name)
58+
);
59+
}
5960
</script>
6061
6162
{#key id}

0 commit comments

Comments
 (0)