fix(markdown): 修复小概率情况下公式加载失败问题
This commit is contained in:
committed by
lyingbug
parent
a232b12ff0
commit
a3c3ba5acf
@@ -55,7 +55,7 @@ marked.use({
|
|||||||
breaks: true, // 启用单行换行转 <br>
|
breaks: true, // 启用单行换行转 <br>
|
||||||
gfm: true, // 启用 GitHub Flavored Markdown
|
gfm: true, // 启用 GitHub Flavored Markdown
|
||||||
});
|
});
|
||||||
marked.use(markedKatex({ throwOnError: false }));
|
marked.use(markedKatex({ throwOnError: false, nonStandard: true }));
|
||||||
|
|
||||||
const preprocessMathDelimiters = (rawText: string): string => {
|
const preprocessMathDelimiters = (rawText: string): string => {
|
||||||
if (!rawText || typeof rawText !== 'string') {
|
if (!rawText || typeof rawText !== 'string') {
|
||||||
|
|||||||
@@ -218,7 +218,7 @@ async function renderMarkdown(blob: Blob) {
|
|||||||
breaks: true,
|
breaks: true,
|
||||||
gfm: true,
|
gfm: true,
|
||||||
});
|
});
|
||||||
marked.use(markedKatex({ throwOnError: false }));
|
marked.use(markedKatex({ throwOnError: false, nonStandard: true }));
|
||||||
const renderer = new marked.Renderer();
|
const renderer = new marked.Renderer();
|
||||||
renderer.code = function ({text, lang}) {
|
renderer.code = function ({text, lang}) {
|
||||||
// 空值校验:防止 text 为 undefined 或 null
|
// 空值校验:防止 text 为 undefined 或 null
|
||||||
|
|||||||
@@ -736,7 +736,7 @@ const props = defineProps<{
|
|||||||
|
|
||||||
// Configure marked for security
|
// Configure marked for security
|
||||||
marked.use({});
|
marked.use({});
|
||||||
marked.use(markedKatex({ throwOnError: false }));
|
marked.use(markedKatex({ throwOnError: false, nonStandard: true }));
|
||||||
|
|
||||||
const preprocessMathDelimiters = (rawText: string): string => {
|
const preprocessMathDelimiters = (rawText: string): string => {
|
||||||
if (!rawText || typeof rawText !== '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
|
// 自定义渲染器 - 支持 Mermaid
|
||||||
const agentRenderer = new marked.Renderer();
|
const agentRenderer = new marked.Renderer();
|
||||||
agentRenderer.code = createMermaidCodeRenderer('mermaid-agent');
|
agentRenderer.code = createMermaidCodeRenderer('mermaid-agent');
|
||||||
@@ -1730,9 +1752,12 @@ const renderMarkdownContent = (content: any): string => {
|
|||||||
// Restore preserved tags
|
// Restore preserved tags
|
||||||
sanitized = sanitized.replace(/\x00TAG(\d+)\x00/g, (_, idx) => tagPlaceholders[Number(idx)]);
|
sanitized = sanitized.replace(/\x00TAG(\d+)\x00/g, (_, idx) => tagPlaceholders[Number(idx)]);
|
||||||
|
|
||||||
const processed = preprocessMarkdown(preprocessMathDelimiters(sanitized));
|
const mathSafe = preprocessMathDelimiters(sanitized);
|
||||||
const html = marked.parse(processed, { renderer: agentRenderer }) as string;
|
const imageSafe = replaceIncompleteImageWithPlaceholder(mathSafe);
|
||||||
const protectedHTML = protectProviderImageSrcInHTML(html);
|
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);
|
return DOMPurify.sanitize(protectedHTML, DOMPurifyConfig);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -1742,11 +1767,14 @@ const renderMarkdown = (content: any): string => {
|
|||||||
if (!contentStr.trim()) return '';
|
if (!contentStr.trim()) return '';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const processed = preprocessMarkdown(preprocessMathDelimiters(contentStr));
|
const mathSafe = preprocessMathDelimiters(contentStr);
|
||||||
const html = marked.parse(processed, { renderer: agentRenderer }) as string;
|
const imageSafe = replaceIncompleteImageWithPlaceholder(mathSafe);
|
||||||
|
const { content: markdownWithPlaceholders, htmlSnippets } = extractRenderableHtmlPlaceholders(imageSafe);
|
||||||
|
const html = marked.parse(markdownWithPlaceholders, { renderer: agentRenderer }) as string;
|
||||||
if (!html) return '';
|
if (!html) return '';
|
||||||
|
|
||||||
const protectedHTML = protectProviderImageSrcInHTML(html);
|
const htmlWithCitations = restoreRenderableHtmlPlaceholders(html, htmlSnippets);
|
||||||
|
const protectedHTML = protectProviderImageSrcInHTML(htmlWithCitations);
|
||||||
return DOMPurify.sanitize(protectedHTML, DOMPurifyConfig);
|
return DOMPurify.sanitize(protectedHTML, DOMPurifyConfig);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Markdown rendering error:', e, 'Content:', contentStr.substring(0, 100));
|
console.error('Markdown rendering error:', e, 'Content:', contentStr.substring(0, 100));
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ marked.use({
|
|||||||
breaks: true, // 全局启用单个换行支持
|
breaks: true, // 全局启用单个换行支持
|
||||||
});
|
});
|
||||||
|
|
||||||
marked.use(markedKatex({ throwOnError: false }));
|
marked.use(markedKatex({ throwOnError: false, nonStandard: true }));
|
||||||
|
|
||||||
const preprocessMathDelimiters = (rawText) => {
|
const preprocessMathDelimiters = (rawText) => {
|
||||||
if (!rawText || typeof rawText !== 'string') {
|
if (!rawText || typeof rawText !== 'string') {
|
||||||
@@ -586,4 +586,4 @@ onBeforeUnmount(() => {
|
|||||||
background: conic-gradient(from 90deg at 50% 50%, #fff 0deg, #676767 360deg) !important;
|
background: conic-gradient(from 90deg at 50% 50%, #fff 0deg, #676767 360deg) !important;
|
||||||
|
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@@ -107,7 +107,7 @@ import {
|
|||||||
|
|
||||||
// Configure marked
|
// Configure marked
|
||||||
marked.use({ breaks: true, gfm: true });
|
marked.use({ breaks: true, gfm: true });
|
||||||
marked.use(markedKatex({ throwOnError: false }));
|
marked.use(markedKatex({ throwOnError: false, nonStandard: true }));
|
||||||
|
|
||||||
const mermaidRenderer = new marked.Renderer();
|
const mermaidRenderer = new marked.Renderer();
|
||||||
mermaidRenderer.code = createMermaidCodeRenderer('mermaid-test');
|
mermaidRenderer.code = createMermaidCodeRenderer('mermaid-test');
|
||||||
|
|||||||
Reference in New Issue
Block a user