mirror of
https://github.com/open-webui/open-webui.git
synced 2026-10-01 02:01:35 +00:00
perf: cache marked.lexer output for detail tokens
This commit is contained in:
parent
cb0fd6ed41
commit
cb8d7ed114
1 changed files with 18 additions and 4 deletions
|
|
@ -1,12 +1,26 @@
|
|||
<script lang="ts">
|
||||
<script context="module" lang="ts">
|
||||
import { marked, type Token } from 'marked';
|
||||
import { decode } from 'html-entities';
|
||||
|
||||
const _detailTokensCache = new Map<string, Token[]>();
|
||||
|
||||
function getCachedDetailTokens(text: string, done: boolean = true): Token[] {
|
||||
if (!done) return marked.lexer(decode(text));
|
||||
const cached = _detailTokensCache.get(text);
|
||||
if (cached) return cached;
|
||||
const tokens = marked.lexer(decode(text));
|
||||
_detailTokensCache.set(text, tokens);
|
||||
return tokens;
|
||||
}
|
||||
</script>
|
||||
|
||||
<script lang="ts">
|
||||
import { onMount, getContext } from 'svelte';
|
||||
const i18n = getContext('i18n');
|
||||
|
||||
import fileSaver from 'file-saver';
|
||||
const { saveAs } = fileSaver;
|
||||
|
||||
import { marked, type Token } from 'marked';
|
||||
import { copyToClipboard, unescapeHtml } from '$lib/utils';
|
||||
|
||||
import { WEBUI_BASE_URL } from '$lib/constants';
|
||||
|
|
@ -396,7 +410,7 @@
|
|||
<div class="mb-1.5" slot="content">
|
||||
<svelte:self
|
||||
id={`${id}-${tokenIdx}-${detailIdx}-d`}
|
||||
tokens={marked.lexer(decode(detailToken.text))}
|
||||
tokens={getCachedDetailTokens(detailToken.text, done)}
|
||||
attributes={detailToken?.attributes}
|
||||
{done}
|
||||
{editCodeBlock}
|
||||
|
|
@ -443,7 +457,7 @@
|
|||
<div class=" mb-1.5" slot="content">
|
||||
<svelte:self
|
||||
id={`${id}-${tokenIdx}-d`}
|
||||
tokens={marked.lexer(decode(token.text))}
|
||||
tokens={getCachedDetailTokens(token.text, done)}
|
||||
attributes={token?.attributes}
|
||||
{done}
|
||||
{editCodeBlock}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue