|
1 | | -<script lang="ts"> |
| 1 | +<script context="module" lang="ts"> |
| 2 | + import { marked, type Token } from 'marked'; |
2 | 3 | import { decode } from 'html-entities'; |
| 4 | +
|
| 5 | + const _detailTokensCache = new Map<string, Token[]>(); |
| 6 | +
|
| 7 | + function getCachedDetailTokens(text: string, done: boolean = true): Token[] { |
| 8 | + if (!done) return marked.lexer(decode(text)); |
| 9 | + const cached = _detailTokensCache.get(text); |
| 10 | + if (cached) return cached; |
| 11 | + const tokens = marked.lexer(decode(text)); |
| 12 | + _detailTokensCache.set(text, tokens); |
| 13 | + return tokens; |
| 14 | + } |
| 15 | +</script> |
| 16 | + |
| 17 | +<script lang="ts"> |
3 | 18 | import { onMount, getContext } from 'svelte'; |
4 | 19 | const i18n = getContext('i18n'); |
5 | 20 |
|
6 | 21 | import fileSaver from 'file-saver'; |
7 | 22 | const { saveAs } = fileSaver; |
8 | 23 |
|
9 | | - import { marked, type Token } from 'marked'; |
10 | 24 | import { copyToClipboard, unescapeHtml } from '$lib/utils'; |
11 | 25 |
|
12 | 26 | import { WEBUI_BASE_URL } from '$lib/constants'; |
|
398 | 412 | <div class="mb-1.5" slot="content"> |
399 | 413 | <svelte:self |
400 | 414 | id={`${id}-${tokenIdx}-${detailIdx}-d`} |
401 | | - tokens={marked.lexer(decode(detailToken.text))} |
| 415 | + tokens={getCachedDetailTokens(detailToken.text, done)} |
402 | 416 | attributes={detailToken?.attributes} |
403 | 417 | {done} |
404 | 418 | {editCodeBlock} |
|
445 | 459 | <div class=" mb-1.5" slot="content"> |
446 | 460 | <svelte:self |
447 | 461 | id={`${id}-${tokenIdx}-d`} |
448 | | - tokens={marked.lexer(decode(token.text))} |
| 462 | + tokens={getCachedDetailTokens(token.text, done)} |
449 | 463 | attributes={token?.attributes} |
450 | 464 | {done} |
451 | 465 | {editCodeBlock} |
|
0 commit comments