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

This commit is contained in:
bingxiang.cheng
2026-05-03 23:14:02 +08:00
committed by lyingbug
parent a232b12ff0
commit a3c3ba5acf
5 changed files with 40 additions and 12 deletions
+1 -1
View File
@@ -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') {
+1 -1
View File
@@ -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
@@ -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));
@@ -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>
</style>
+1 -1
View File
@@ -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');