Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(markdown): 修复小概率情况下公式加载失败问题

bingxiang.chenga3c3ba5acf0f
diff --git a/frontend/src/components/doc-content.vue b/frontend/src/components/doc-content.vue
index fdd661775..6ea7bb2a8 100644
--- a/frontend/src/components/doc-content.vue
+++ b/frontend/src/components/doc-content.vue
@@ -55,7 +55,7 @@ marked.use({
breaks: true, // 启用单行换行转 <br>
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 8afdb0715..a3c863099 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 39d04fcd5..b0effd405 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 047bec12f..d4df70a62 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;
}
-</style>
\ No newline at end of file
+</style>
diff --git a/frontend/src/views/dev/MarkdownTestPage.vue b/frontend/src/views/dev/MarkdownTestPage.vue
index 9434b8290..89438e6bf 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');