diff --git a/src/components/AiChat.vue b/src/components/AiChat.vue index e9f9bd9..b36e121 100644 --- a/src/components/AiChat.vue +++ b/src/components/AiChat.vue @@ -197,7 +197,7 @@ class="ai-msg-bubble ai-msg-bubble--ai ai-md" :class="{ 'ai-msg-bubble--error': msg.isError }" > -
+
@@ -344,14 +344,16 @@ function escapeHtml(text: string): string { return text.replace(/&/g, '&').replace(//g, '>') } -function renderMd(text: string): string { +function renderMd(text: string, isStreaming = false): string { if (!text) return '' + // 流式态纯文本(AR-1):避免每 delta 全量 marked.parse+sanitize 致主线程阻塞掉帧; + // 不缓存流式文本(每 delta 变,缓存无收益且污染),仅 escapeHtml + 换行转
;完成后再走 markdown + if (isStreaming || !mdReady.value || !_marked || !_purify) { + return escapeHtml(text).replace(/\n/g, '
') + } const cached = _mdCache.get(text) if (cached !== undefined) return cached - // 依赖 mdReady 使加载完成后自动重渲染;未就绪时先以纯文本兜底 - const html = (!mdReady.value || !_marked || !_purify) - ? escapeHtml(text) - : _purify.sanitize(_marked.parse(text) as string) + const html = _purify.sanitize(_marked.parse(text) as string) if (_mdCache.size > 200) _mdCache.clear() _mdCache.set(text, html) return html