|
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 | + const DETAIL_CACHE_MAX = 500; |
| 7 | +
|
| 8 | + function getCachedDetailTokens(text: string): Token[] { |
| 9 | + if (_detailTokensCache.has(text)) return _detailTokensCache.get(text)!; |
| 10 | + const tokens = marked.lexer(decode(text)); |
| 11 | + if (_detailTokensCache.size >= DETAIL_CACHE_MAX) { |
| 12 | + _detailTokensCache.delete(_detailTokensCache.keys().next().value!); |
| 13 | + } |
| 14 | + _detailTokensCache.set(text, tokens); |
| 15 | + return tokens; |
| 16 | + } |
| 17 | +</script> |
| 18 | + |
| 19 | +<script lang="ts"> |
3 | 20 | import { onMount, getContext } from 'svelte'; |
4 | 21 | const i18n = getContext('i18n'); |
5 | 22 |
|
6 | 23 | import fileSaver from 'file-saver'; |
7 | 24 | const { saveAs } = fileSaver; |
8 | 25 |
|
9 | | - import { marked, type Token } from 'marked'; |
10 | 26 | import { copyToClipboard, unescapeHtml } from '$lib/utils'; |
11 | 27 |
|
12 | 28 | import { WEBUI_BASE_URL } from '$lib/constants'; |
|
398 | 414 | <div class="mb-1.5" slot="content"> |
399 | 415 | <svelte:self |
400 | 416 | id={`${id}-${tokenIdx}-${detailIdx}-d`} |
401 | | - tokens={marked.lexer(decode(detailToken.text))} |
| 417 | + tokens={getCachedDetailTokens(detailToken.text)} |
402 | 418 | attributes={detailToken?.attributes} |
403 | 419 | {done} |
404 | 420 | {editCodeBlock} |
|
445 | 461 | <div class=" mb-1.5" slot="content"> |
446 | 462 | <svelte:self |
447 | 463 | id={`${id}-${tokenIdx}-d`} |
448 | | - tokens={marked.lexer(decode(token.text))} |
| 464 | + tokens={getCachedDetailTokens(token.text)} |
449 | 465 | attributes={token?.attributes} |
450 | 466 | {done} |
451 | 467 | {editCodeBlock} |
|
0 commit comments