feat: 优化流式渲染机制,支持Markdown内容逐token解析显示

This commit is contained in:
wizardchen
2025-12-19 22:18:08 +08:00
committed by lyingbug
parent de1ffa7f7c
commit 8ba3be7b01
2 changed files with 142 additions and 208 deletions
@@ -36,9 +36,8 @@
'thinking-last': isLastThinking(event.event_id)
}"
>
<div v-if="event.content"
class="thinking-content markdown-content"
v-html="renderMarkdown(event.content)">
<div v-if="event.content" class="thinking-content markdown-content">
<div v-for="(token, idx) in getTokens(event.content)" :key="idx" v-html="getTokenHTML(token)"></div>
</div>
</div>
</div>
@@ -53,8 +52,8 @@
'answer-done': event.done
}"
>
<div class="answer-content markdown-content"
v-html="renderMarkdown(event.content)">
<div class="answer-content markdown-content">
<div v-for="(token, idx) in getTokens(event.content)" :key="idx" v-html="getTokenHTML(token)"></div>
</div>
</div>
<div v-if="event.done" class="answer-toolbar">
@@ -126,7 +125,9 @@
<!-- Thinking tool: only render markdown thought content -->
<template v-if="event.tool_name === 'thinking' && event.tool_data?.thought">
<div class="thinking-thought-content">
<div class="thinking-thought-markdown markdown-content" v-html="renderMarkdown(event.tool_data.thought)"></div>
<div class="thinking-thought-markdown markdown-content">
<div v-for="(token, idx) in getTokens(event.tool_data.thought)" :key="idx" v-html="getTokenHTML(token)"></div>
</div>
</div>
</template>
@@ -1069,111 +1070,112 @@ const parseTagAttributes = (attrString: string): Record<string, string> => {
return attributes;
};
// Markdown rendering function
const renderMarkdown = (content: any): string => {
if (!content) return '';
// Ensure content is a string
let contentStr = typeof content === 'string' ? content : String(content || '');
// Preprocess markdown to handle incomplete images and custom citations
const preprocessMarkdown = (contentStr: string): string => {
if (!contentStr.trim()) return '';
// Handle streaming image syntax to prevent flickering
// Check if the content ends with an incomplete image markdown syntax like `![...` or `![...](...`
// This prevents the text from being rendered as plain text first and then jumping to an image
const lastImgStart = contentStr.lastIndexOf('![');
if (lastImgStart !== -1) {
// Only check the last occurrence to see if it's incomplete
// We check if the tail (from the last ![) contains a matching closing parenthesis )
// This is a heuristic: if the last ![ doesn't have a corresponding ), it's likely incomplete
const potentialImgTag = contentStr.slice(lastImgStart);
const hasClosingParen = potentialImgTag.includes(')');
const hasClosingBracket = potentialImgTag.includes(']');
// If we have ![ but missing ] or ), it's incomplete
// Note: This is a simple check. It might false positive on `![text] (note)` but that's rare in this context
if (!hasClosingBracket || !hasClosingParen) {
contentStr = contentStr.slice(0, lastImgStart);
}
}
// Preprocess custom citation tags
return contentStr
.replace(
/<web\b([^>]*)\/>/g,
(_m: string, attrString: string) => {
const attrs = parseTagAttributes(attrString);
const url = attrs.url || '';
const title = attrs.title || '';
if (!url) return '';
let domain = url;
try {
const u = new URL(url);
const host = u.hostname || '';
const parts = host.split('.');
if (parts.length >= 2) {
domain = parts.slice(-2).join('.');
} else {
domain = host || url;
}
} catch {
// keep original url text if parsing fails
}
const safeTitle = String(title || '').replace(/"/g, '&quot;');
const safeUrl = String(url || '').replace(/"/g, '&quot;');
const tipTitle = safeTitle || '';
const tipUrl = safeUrl || '';
return `<a class="citation citation-web" data-url="${safeUrl}" href="${safeUrl}" target="_blank" rel="noopener noreferrer"><span class="citation-icon web"></span><span class="citation-domain">${domain}</span><span class="citation-tip"><span class="tip-title">${tipTitle}</span><span class="tip-url">${tipUrl}</span></span></a>`;
}
)
.replace(
/<kb\b([^>]*)\/>/g,
(_m, attrString: string) => {
const attrs = parseTagAttributes(attrString);
const doc = attrs.doc || '';
const chunkId = attrs.chunk_id || attrs.chunkId || '';
const kbId = attrs.kb_id || attrs.kbId || '';
if (!doc || !chunkId) return '';
const safeDoc = escapeHtml(doc);
const safeKbId = escapeHtml(kbId);
const safeChunkId = escapeHtml(chunkId);
const truncateMiddle = (text: string, maxLength = 13): string => {
if (!text) return '';
if (text.length <= maxLength) return text;
const half = Math.floor((maxLength - 3) / 2);
const start = text.slice(0, half + ((maxLength - 3) % 2));
const end = text.slice(-half);
return `${start}...${end}`;
};
const displayDoc = escapeHtml(truncateMiddle(doc));
return `<span class="citation citation-kb" data-kb-id="${safeKbId}" data-chunk-id="${safeChunkId}" data-doc="${safeDoc}" role="button" tabindex="0"><span class="citation-icon kb"></span><span class="citation-text">${displayDoc}</span><span class="citation-tip"><span class="t-popup__content"><span class="tip-loading">加载中...</span></span></span></span>`;
}
);
};
// Get tokens from markdown content
const getTokens = (content: any) => {
const contentStr = typeof content === 'string' ? content : String(content || '');
if (!contentStr.trim()) return [];
const processed = preprocessMarkdown(contentStr);
return marked.lexer(processed);
};
// Render HTML from a single token
const getTokenHTML = (token: any): string => {
try {
// Preprocess custom citation tags into safe HTML the sanitizer will allow
// Supported formats:
// <kb kb_id="..." doc="..." chunk_id="..." />
// <web url="https://..." title="Example" />
const processed = contentStr
// Web citations -> compact clickable badges with domain text; hover shows title via native tooltip
.replace(
/<web\b([^>]*)\/>/g,
(_m: string, attrString: string) => {
const attrs = parseTagAttributes(attrString);
const url = attrs.url || '';
const title = attrs.title || '';
if (!url) {
return '';
}
// Extract domain for compact display
let domain = url;
try {
const u = new URL(url);
const host = u.hostname || '';
const parts = host.split('.');
if (parts.length >= 2) {
// prefer second-level domain: last two labels
domain = parts.slice(-2).join('.');
} else {
domain = host || url;
}
} catch {
// keep original url text if parsing fails
}
// Escape double quotes in title for attribute safety
const safeTitle = String(title || '').replace(/"/g, '&quot;');
const safeUrl = String(url || '').replace(/"/g, '&quot;');
// Render with embedded tooltip blocks to allow styled parts (bold title)
const tipTitle = safeTitle || '';
const tipUrl = safeUrl || '';
// Keep lightweight tooltip (no t-popup container) to avoid global popup styling
return `<a class="citation citation-web" data-url="${safeUrl}" href="${safeUrl}" target="_blank" rel="noopener noreferrer"><span class="citation-icon web"></span><span class="citation-domain">${domain}</span><span class="citation-tip"><span class="tip-title">${tipTitle}</span><span class="tip-url">${tipUrl}</span></span></a>`;
}
)
// KB citations -> inline badges with simplified display
.replace(
/<kb\b([^>]*)\/>/g,
(_m, attrString: string) => {
const attrs = parseTagAttributes(attrString);
const doc = attrs.doc || '';
const chunkId = attrs.chunk_id || attrs.chunkId || '';
const kbId = attrs.kb_id || attrs.kbId || '';
if (!doc || !chunkId) {
return '';
}
// Escape attributes for safety
const safeDoc = escapeHtml(doc);
const safeKbId = escapeHtml(kbId);
const safeChunkId = escapeHtml(chunkId);
const truncateMiddle = (text: string, maxLength = 13): string => {
if (!text) return '';
if (text.length <= maxLength) return text;
const half = Math.floor((maxLength - 3) / 2);
const start = text.slice(0, half + ((maxLength - 3) % 2));
const end = text.slice(-half);
return `${start}...${end}`;
};
const displayDoc = escapeHtml(truncateMiddle(doc));
console.log('displayDoc', displayDoc);
// Initial tooltip content (single t-popup container; will be updated on hover)
return `<span class="citation citation-kb" data-kb-id="${safeKbId}" data-chunk-id="${safeChunkId}" data-doc="${safeDoc}" role="button" tabindex="0"><span class="citation-icon kb"></span><span class="citation-text">${displayDoc}</span><span class="citation-tip"><span class="t-popup__content"><span class="tip-loading">加载中...</span></span></span></span>`;
}
);
const html = marked.parser([token]);
return DOMPurify.sanitize(html, {
ALLOWED_TAGS: ['p', 'br', 'strong', 'em', 'u', 'code', 'pre', 'ul', 'ol', 'li', 'blockquote', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'a', 'span', 'table', 'thead', 'tbody', 'tr', 'th', 'td', 'img', 'figure', 'figcaption'],
ALLOWED_ATTR: ['href', 'title', 'target', 'rel', 'data-tooltip', 'data-url', 'data-kb-id', 'data-chunk-id', 'data-doc', 'class', 'role', 'tabindex', 'src', 'alt', 'width', 'height', 'style']
});
} catch (e) {
console.error('Token rendering error:', e);
return '';
}
};
// Legacy Markdown rendering function (kept for summaries)
const renderMarkdown = (content: any): string => {
const contentStr = typeof content === 'string' ? content : String(content || '');
if (!contentStr.trim()) return '';
try {
const processed = preprocessMarkdown(contentStr);
const html = marked.parse(processed) as string;
if (!html) return '';
@@ -1183,7 +1185,6 @@ const renderMarkdown = (content: any): string => {
});
} catch (e) {
console.error('Markdown rendering error:', e, 'Content:', contentStr.substring(0, 100));
// Return escaped HTML instead of raw content for safety
return contentStr.replace(/</g, '&lt;').replace(/>/g, '&gt;');
}
};
+44 -111
View File
@@ -17,7 +17,9 @@
</div>
<!-- 直接渲染完整内容,避免切分导致的问题,样式与 thinking 一致 -->
<div class="content-wrapper">
<div class="ai-markdown-template markdown-content" v-html="processMarkdown(content || session.content)"></div>
<div class="ai-markdown-template markdown-content">
<div v-for="(token, index) in markdownTokens" :key="index" v-html="renderToken(token)"></div>
</div>
</div>
<!-- 复制和添加到知识库按钮 - 非 Agent 模式下显示 -->
<div v-if="session.is_completed && (content || session.content)" class="answer-toolbar">
@@ -78,131 +80,65 @@ const props = defineProps({
required: false
}
});
const processedMarkdown = ref([]);
const preview = (url) => {
nextTick(() => {
reviewUrl.value = url;
reviewImg.value = true
})
}
const removeImg = () => {
nextTick(() => {
if (!parentMd.value) return;
const images = parentMd.value.querySelectorAll('img.ai-markdown-img');
if (images) {
images.forEach(async item => {
const isValid = await checkImage(item.src);
if (!isValid) {
item.remove();
}
})
}
})
}
const closePreImg = () => {
reviewImg.value = false
reviewUrl.value = '';
}
const debounce = (fn, delay) => {
let timer
return (...args) => {
clearTimeout(timer)
timer = setTimeout(() => fn(...args), delay)
// 创建自定义渲染器实例
const customRenderer = new marked.Renderer();
// 覆盖图片渲染方法
customRenderer.image = function(href, title, text) {
// 验证图片 URL 是否安全
if (!isValidImageURL(href)) {
return `<p>${t('error.invalidImageLink')}</p>`;
}
}
const checkImage = (url) => {
return new Promise((resolve) => {
const img = new Image();
img.onload = () => {
resolve(true);
}
img.onerror = () => resolve(false);
img.src = url;
});
// 使用安全的图片创建函数
return createSafeImage(href, text || '', title || '');
};
// 安全地处理 Markdown 内容
const processMarkdown = (markdownText) => {
if (!markdownText || typeof markdownText !== 'string') {
return '';
// 计算属性:将 Markdown 文本转换为 tokens
const markdownTokens = computed(() => {
const text = props.content || props.session?.content || '';
if (!text || typeof text !== 'string') {
return [];
}
// 首先对 Markdown 内容进行安全处理
const safeMarkdown = safeMarkdownToHTML(markdownText);
const safeMarkdown = safeMarkdownToHTML(text);
// 创建自定义渲染器实例,继承默认渲染器
const customRenderer = new marked.Renderer();
// 覆盖图片渲染方法
customRenderer.image = function(href, title, text) {
// 验证图片 URL 是否安全
if (!isValidImageURL(href)) {
return `<p>${t('error.invalidImageLink')}</p>`;
}
// 使用安全的图片创建函数
return createSafeImage(href, text || '', title || '');
};
// 使用 marked.lexer 分词
return marked.lexer(safeMarkdown);
});
// 创建临时的 marked 配置,包含 renderer 和 breaks 选项
// breaks: true 会将单个换行渲染为 <br>,而不是忽略
const markedOptions = {
renderer: customRenderer,
breaks: true // 启用单个换行支持
};
// 安全地渲染 Markdown,直接传递选项
let html = marked.parse(safeMarkdown, markedOptions);
// 使用 DOMPurify 进行最终的安全清理
const sanitizedHTML = sanitizeHTML(html);
return sanitizedHTML;
// 渲染单个 token 为 HTML
const renderToken = (token) => {
try {
// 创建临时的 marked 配置
const markedOptions = {
renderer: customRenderer,
breaks: true
};
// 解析单个 token
// marked.parser 接受 token 数组
let html = marked.parser([token], markedOptions);
// 使用 DOMPurify 进行最终的安全清理
return sanitizeHTML(html);
} catch (e) {
console.error('Token rendering error:', e);
return '';
}
};
const handleImg = async (newVal) => {
let index = newVal.lastIndexOf('![');
if (index != -1) {
isImgLoading.value = true;
let str = newVal.slice(index)
if (str.includes('](') && str.includes(')')) {
processedMarkdown.value = splitMarkdownByImages(newVal)
isImgLoading.value = false;
} else {
processedMarkdown.value = splitMarkdownByImages(newVal.slice(0, index))
}
} else {
processedMarkdown.value = splitMarkdownByImages(newVal)
}
removeImg()
}
function splitMarkdownByImages(markdown) {
const imageRegex = /!\[.*?\]\(\s*(?:<([^>]*)>|([^)\s]*))\s*(?:["'].*?["'])?\s*\)/g;
const result = [];
let lastIndex = 0;
let match;
while ((match = imageRegex.exec(markdown)) !== null) {
const textBefore = markdown.slice(lastIndex, match.index);
if (textBefore) result.push(textBefore);
const url = match[1] || match[2];
result.push(url);
lastIndex = imageRegex.lastIndex;
}
const remainingText = markdown.slice(lastIndex);
if (remainingText) result.push(remainingText);
return result;
}
function isLink(str) {
const trimmedStr = str.trim();
// 正则表达式匹配常见链接格式
const urlPattern = /^(https?:\/\/|ftp:\/\/|www\.)(?:(?:[\w-]+(?:\.[\w-]+)*)|(?:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|(?:\[[a-fA-F0-9:]+\]))(?::\d{1,5})?(?:[\/\w.,@?^=%&:~+#-]*[\w@?^=%&\/~+#-])?/i;
return urlPattern.test(trimmedStr);
}
watch(() => props.content, (newVal) => {
debounce(handleImg(newVal), 800)
}, {
immediate: true
})
const myMarkdown = (res) => {
return marked.parse(res, { renderer })
@@ -296,9 +232,6 @@ const handleMarkdownImageClick = (e) => {
};
onMounted(async () => {
processedMarkdown.value = splitMarkdownByImages(props.content);
removeImg();
// 为 markdown-content 中的图片添加点击事件
nextTick(() => {
if (parentMd.value) {