From a3c3ba5acf0f96b1f61c8bf51d516a86aef9e8e9 Mon Sep 17 00:00:00 2001 From: "bingxiang.cheng" Date: Sun, 3 May 2026 23:14:02 +0800 Subject: [PATCH] =?UTF-8?q?fix(markdown):=20=E4=BF=AE=E5=A4=8D=E5=B0=8F?= =?UTF-8?q?=E6=A6=82=E7=8E=87=E6=83=85=E5=86=B5=E4=B8=8B=E5=85=AC=E5=BC=8F?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E5=A4=B1=E8=B4=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/components/doc-content.vue | 2 +- frontend/src/components/document-preview.vue | 2 +- .../chat/components/AgentStreamDisplay.vue | 42 +++++++++++++++---- frontend/src/views/chat/components/botmsg.vue | 4 +- frontend/src/views/dev/MarkdownTestPage.vue | 2 +- 5 files changed, 40 insertions(+), 12 deletions(-) diff --git a/frontend/src/components/doc-content.vue b/frontend/src/components/doc-content.vue index fdd66177..6ea7bb2a 100644 --- a/frontend/src/components/doc-content.vue +++ b/frontend/src/components/doc-content.vue @@ -55,7 +55,7 @@ marked.use({ breaks: true, // 启用单行换行转
gfm: true, // 启用 GitHub Flavored Markdown }); -marked.use(markedKatex({ throwOnError: false })); +marked.use(markedKatex({ throwOnError: false, nonStandard: true })); const preprocessMathDelimiters = (rawText: string): string => { if (!rawText || typeof rawText !== 'string') { diff --git a/frontend/src/components/document-preview.vue b/frontend/src/components/document-preview.vue index 8afdb071..a3c86309 100644 --- a/frontend/src/components/document-preview.vue +++ b/frontend/src/components/document-preview.vue @@ -218,7 +218,7 @@ async function renderMarkdown(blob: Blob) { breaks: true, gfm: true, }); - marked.use(markedKatex({ throwOnError: false })); + marked.use(markedKatex({ throwOnError: false, nonStandard: true })); const renderer = new marked.Renderer(); renderer.code = function ({text, lang}) { // 空值校验:防止 text 为 undefined 或 null diff --git a/frontend/src/views/chat/components/AgentStreamDisplay.vue b/frontend/src/views/chat/components/AgentStreamDisplay.vue index 39d04fcd..b0effd40 100644 --- a/frontend/src/views/chat/components/AgentStreamDisplay.vue +++ b/frontend/src/views/chat/components/AgentStreamDisplay.vue @@ -736,7 +736,7 @@ const props = defineProps<{ // Configure marked for security marked.use({}); -marked.use(markedKatex({ throwOnError: false })); +marked.use(markedKatex({ throwOnError: false, nonStandard: true })); const preprocessMathDelimiters = (rawText: string): string => { if (!rawText || typeof rawText !== 'string') { @@ -1684,6 +1684,28 @@ const preprocessMarkdown = (contentStr: string): string => { ); }; +const HTML_PLACEHOLDER_RE = /@@WEKNORA_HTML_PLACEHOLDER_(\d+)@@/g; + +const extractRenderableHtmlPlaceholders = (contentStr: string): { content: string; htmlSnippets: string[] } => { + const htmlSnippets: string[] = []; + const storeHtml = (html: string): string => { + const idx = htmlSnippets.length; + htmlSnippets.push(html); + return `@@WEKNORA_HTML_PLACEHOLDER_${idx}@@`; + }; + + const content = contentStr + .replace(/<(?:kb|web)\b[^>]*\/>/g, (match) => storeHtml(preprocessMarkdown(match))) + .replace(/\[\[([^\]]+)\]\]/g, (match) => storeHtml(preprocessMarkdown(match))); + + return { content, htmlSnippets }; +}; + +const restoreRenderableHtmlPlaceholders = (html: string, htmlSnippets: string[]): string => { + if (!htmlSnippets.length) return html; + return html.replace(HTML_PLACEHOLDER_RE, (_match, idx) => htmlSnippets[Number(idx)] || ''); +}; + // 自定义渲染器 - 支持 Mermaid const agentRenderer = new marked.Renderer(); agentRenderer.code = createMermaidCodeRenderer('mermaid-agent'); @@ -1730,9 +1752,12 @@ const renderMarkdownContent = (content: any): string => { // Restore preserved tags sanitized = sanitized.replace(/\x00TAG(\d+)\x00/g, (_, idx) => tagPlaceholders[Number(idx)]); - const processed = preprocessMarkdown(preprocessMathDelimiters(sanitized)); - const html = marked.parse(processed, { renderer: agentRenderer }) as string; - const protectedHTML = protectProviderImageSrcInHTML(html); + const mathSafe = preprocessMathDelimiters(sanitized); + const imageSafe = replaceIncompleteImageWithPlaceholder(mathSafe); + const { content: markdownWithPlaceholders, htmlSnippets } = extractRenderableHtmlPlaceholders(imageSafe); + const html = marked.parse(markdownWithPlaceholders, { renderer: agentRenderer }) as string; + const htmlWithCitations = restoreRenderableHtmlPlaceholders(html, htmlSnippets); + const protectedHTML = protectProviderImageSrcInHTML(htmlWithCitations); return DOMPurify.sanitize(protectedHTML, DOMPurifyConfig); }; @@ -1742,11 +1767,14 @@ const renderMarkdown = (content: any): string => { if (!contentStr.trim()) return ''; try { - const processed = preprocessMarkdown(preprocessMathDelimiters(contentStr)); - const html = marked.parse(processed, { renderer: agentRenderer }) as string; + const mathSafe = preprocessMathDelimiters(contentStr); + const imageSafe = replaceIncompleteImageWithPlaceholder(mathSafe); + const { content: markdownWithPlaceholders, htmlSnippets } = extractRenderableHtmlPlaceholders(imageSafe); + const html = marked.parse(markdownWithPlaceholders, { renderer: agentRenderer }) as string; if (!html) return ''; - const protectedHTML = protectProviderImageSrcInHTML(html); + const htmlWithCitations = restoreRenderableHtmlPlaceholders(html, htmlSnippets); + const protectedHTML = protectProviderImageSrcInHTML(htmlWithCitations); return DOMPurify.sanitize(protectedHTML, DOMPurifyConfig); } catch (e) { console.error('Markdown rendering error:', e, 'Content:', contentStr.substring(0, 100)); diff --git a/frontend/src/views/chat/components/botmsg.vue b/frontend/src/views/chat/components/botmsg.vue index 047bec12..d4df70a6 100644 --- a/frontend/src/views/chat/components/botmsg.vue +++ b/frontend/src/views/chat/components/botmsg.vue @@ -87,7 +87,7 @@ marked.use({ breaks: true, // 全局启用单个换行支持 }); -marked.use(markedKatex({ throwOnError: false })); +marked.use(markedKatex({ throwOnError: false, nonStandard: true })); const preprocessMathDelimiters = (rawText) => { if (!rawText || typeof rawText !== 'string') { @@ -586,4 +586,4 @@ onBeforeUnmount(() => { background: conic-gradient(from 90deg at 50% 50%, #fff 0deg, #676767 360deg) !important; } - \ No newline at end of file + diff --git a/frontend/src/views/dev/MarkdownTestPage.vue b/frontend/src/views/dev/MarkdownTestPage.vue index 9434b829..89438e6b 100644 --- a/frontend/src/views/dev/MarkdownTestPage.vue +++ b/frontend/src/views/dev/MarkdownTestPage.vue @@ -107,7 +107,7 @@ import { // Configure marked marked.use({ breaks: true, gfm: true }); -marked.use(markedKatex({ throwOnError: false })); +marked.use(markedKatex({ throwOnError: false, nonStandard: true })); const mermaidRenderer = new marked.Renderer(); mermaidRenderer.code = createMermaidCodeRenderer('mermaid-test');