From 8ba3be7b01624d298f288e5ba62701df0a4414b0 Mon Sep 17 00:00:00 2001 From: wizardchen Date: Fri, 19 Dec 2025 22:18:08 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E6=B5=81=E5=BC=8F?= =?UTF-8?q?=E6=B8=B2=E6=9F=93=E6=9C=BA=E5=88=B6=EF=BC=8C=E6=94=AF=E6=8C=81?= =?UTF-8?q?Markdown=E5=86=85=E5=AE=B9=E9=80=90token=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat/components/AgentStreamDisplay.vue | 195 +++++++++--------- frontend/src/views/chat/components/botmsg.vue | 155 ++++---------- 2 files changed, 142 insertions(+), 208 deletions(-) diff --git a/frontend/src/views/chat/components/AgentStreamDisplay.vue b/frontend/src/views/chat/components/AgentStreamDisplay.vue index 787d24c3..aec6db74 100644 --- a/frontend/src/views/chat/components/AgentStreamDisplay.vue +++ b/frontend/src/views/chat/components/AgentStreamDisplay.vue @@ -36,9 +36,8 @@ 'thinking-last': isLastThinking(event.event_id) }" > -
+
+
@@ -53,8 +52,8 @@ 'answer-done': event.done }" > -
+
+
@@ -126,7 +125,9 @@ @@ -1069,111 +1070,112 @@ const parseTagAttributes = (attrString: string): Record => { 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( + /]*)\/>/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, '"'); + const safeUrl = String(url || '').replace(/"/g, '"'); + const tipTitle = safeTitle || ''; + const tipUrl = safeUrl || ''; + return `${domain}${tipTitle}${tipUrl}`; + } + ) + .replace( + /]*)\/>/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 `${displayDoc}加载中...`; + } + ); +}; + +// 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: - // - // - const processed = contentStr - // Web citations -> compact clickable badges with domain text; hover shows title via native tooltip - .replace( - /]*)\/>/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, '"'); - const safeUrl = String(url || '').replace(/"/g, '"'); - // 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 `${domain}${tipTitle}${tipUrl}`; - } - ) - // KB citations -> inline badges with simplified display - .replace( - /]*)\/>/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 `${displayDoc}加载中...`; - } - ); + 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, '>'); } }; diff --git a/frontend/src/views/chat/components/botmsg.vue b/frontend/src/views/chat/components/botmsg.vue index ecc2b68d..830f9f05 100644 --- a/frontend/src/views/chat/components/botmsg.vue +++ b/frontend/src/views/chat/components/botmsg.vue @@ -17,7 +17,9 @@
-
+
+
+
@@ -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 `

${t('error.invalidImageLink')}

`; } -} -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 `

${t('error.invalidImageLink')}

`; - } - // 使用安全的图片创建函数 - return createSafeImage(href, text || '', title || ''); - }; + // 使用 marked.lexer 分词 + return marked.lexer(safeMarkdown); +}); - // 创建临时的 marked 配置,包含 renderer 和 breaks 选项 - // breaks: true 会将单个换行渲染为
,而不是忽略 - 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) {