From ee1c3c732c2776c0f02c368c64e1b99b79267cef Mon Sep 17 00:00:00 2001 From: wizardchen Date: Tue, 16 Jun 2026 00:49:12 +0800 Subject: [PATCH] feat(chat): add shared markdown rendering pipeline Extract chat markdown styles, renderer, DOMPurify config, syntax highlighting, and mermaid support into reusable utilities with tests. --- .../src/components/css/chat-hljs-dark.less | 75 +++ frontend/src/components/css/chat-hljs.less | 72 +++ .../src/components/css/chat-markdown.less | 587 ++++++++++++++++++ frontend/src/components/css/markdown.less | 4 +- frontend/src/main.ts | 1 + .../src/utils/chatMarkdownRenderer.test.ts | 181 ++++++ frontend/src/utils/chatMarkdownRenderer.ts | 143 +++++ frontend/src/utils/markdownDomPurify.test.mjs | 17 + frontend/src/utils/markdownDomPurify.ts | 83 +++ frontend/src/utils/markdownEnhancements.ts | 231 +++++++ frontend/src/utils/mermaidShared.ts | 242 ++++++-- frontend/src/utils/mermaidViewer.ts | 4 +- frontend/src/utils/security.ts | 78 +-- frontend/src/views/dev/MarkdownTestPage.vue | 307 +++++---- 14 files changed, 1806 insertions(+), 219 deletions(-) create mode 100644 frontend/src/components/css/chat-hljs-dark.less create mode 100644 frontend/src/components/css/chat-hljs.less create mode 100644 frontend/src/components/css/chat-markdown.less create mode 100644 frontend/src/utils/chatMarkdownRenderer.test.ts create mode 100644 frontend/src/utils/chatMarkdownRenderer.ts create mode 100644 frontend/src/utils/markdownDomPurify.test.mjs create mode 100644 frontend/src/utils/markdownDomPurify.ts create mode 100644 frontend/src/utils/markdownEnhancements.ts diff --git a/frontend/src/components/css/chat-hljs-dark.less b/frontend/src/components/css/chat-hljs-dark.less new file mode 100644 index 00000000..2137fa2b --- /dev/null +++ b/frontend/src/components/css/chat-hljs-dark.less @@ -0,0 +1,75 @@ +// Dark-theme highlight token colors only. Chat Markdown layout/chrome styles +// belong to chat-markdown.less. +html[theme-mode='dark'] { + .markdown-content .chat-code-block__pre, + .answer-content .chat-code-block__pre, + .ai-markdown-template .chat-code-block__pre, + .agent-stream-display .chat-code-block__pre { + .hljs { + color: #c9d1d9; + } + + .hljs-doctag, + .hljs-keyword, + .hljs-meta .hljs-keyword, + .hljs-template-tag, + .hljs-template-variable, + .hljs-type, + .hljs-variable.language_ { + color: #ff7b72; + } + + .hljs-title, + .hljs-title.class_, + .hljs-title.class_.inherited__, + .hljs-title.function_ { + color: #d2a8ff; + } + + .hljs-attr, + .hljs-attribute, + .hljs-literal, + .hljs-meta, + .hljs-number, + .hljs-operator, + .hljs-variable, + .hljs-selector-attr, + .hljs-selector-class, + .hljs-selector-id { + color: #79c0ff; + } + + .hljs-regexp, + .hljs-string, + .hljs-meta .hljs-string { + color: #a5d6ff; + } + + .hljs-built_in, + .hljs-symbol { + color: #ffa657; + } + + .hljs-comment, + .hljs-code, + .hljs-formula { + color: #8b949e; + } + + .hljs-name, + .hljs-quote, + .hljs-selector-tag, + .hljs-selector-pseudo { + color: #7ee787; + } + + .hljs-subst { + color: #c9d1d9; + } + + .hljs-section { + color: #1f6feb; + font-weight: 700; + } + } +} diff --git a/frontend/src/components/css/chat-hljs.less b/frontend/src/components/css/chat-hljs.less new file mode 100644 index 00000000..6e767274 --- /dev/null +++ b/frontend/src/components/css/chat-hljs.less @@ -0,0 +1,72 @@ +// Syntax highlighting tokens for chat code blocks. +.chat-markdown-hljs() { + :deep(.chat-code-block__pre) { + .hljs { + color: #24292e; + background: transparent; + } + + .hljs-doctag, + .hljs-keyword, + .hljs-meta .hljs-keyword, + .hljs-template-tag, + .hljs-template-variable, + .hljs-type, + .hljs-variable.language_ { + color: #d73a49; + } + + .hljs-title, + .hljs-title.class_, + .hljs-title.class_.inherited__, + .hljs-title.function_ { + color: #6f42c1; + } + + .hljs-attr, + .hljs-attribute, + .hljs-literal, + .hljs-meta, + .hljs-number, + .hljs-operator, + .hljs-variable, + .hljs-selector-attr, + .hljs-selector-class, + .hljs-selector-id { + color: #005cc5; + } + + .hljs-regexp, + .hljs-string, + .hljs-meta .hljs-string { + color: #032f62; + } + + .hljs-built_in, + .hljs-symbol { + color: #e36209; + } + + .hljs-comment, + .hljs-code, + .hljs-formula { + color: #6a737d; + } + + .hljs-name, + .hljs-quote, + .hljs-selector-tag, + .hljs-selector-pseudo { + color: #22863a; + } + + .hljs-subst { + color: #24292e; + } + + .hljs-section { + color: #005cc5; + font-weight: 700; + } + } +} diff --git a/frontend/src/components/css/chat-markdown.less b/frontend/src/components/css/chat-markdown.less new file mode 100644 index 00000000..20843e0a --- /dev/null +++ b/frontend/src/components/css/chat-markdown.less @@ -0,0 +1,587 @@ +@import './chat-hljs.less'; + +// Single source of truth for chat answer Markdown typography and block styles. +// Used by botmsg, AgentStreamDisplay, embed chat, and the dev Markdown test page. +// Future chat Markdown visual changes should be made here instead of in +// individual Vue components. Non-chat document/wiki preview styles intentionally +// live outside this file. +// +// Shared typography for chat answer markdown (botmsg, AgentStreamDisplay, embed). +// Tuned against ChatGPT markdown test doc: 16px body, 24px line-height on paragraphs, +// stepped headings, dotted links, light tables, pill inline code, kbd chips. +.chat-markdown-typography() { + font-size: 16px; + color: var(--td-text-color-primary); + line-height: 1.625; + letter-spacing: normal; + -webkit-font-smoothing: antialiased; + + :deep(p) { + margin: 0 0 0.25em; + line-height: 1.625; + + + p { + margin-top: 0.75em; + } + + &:last-child { + margin-bottom: 0; + } + } + + // Citation-only paragraphs from marked (tag on its own line) — keep compact. + :deep(p:has(> .citation:only-child)) { + margin: 0.125em 0 0.375em; + line-height: 1.625; + } + + // Inline citations share the paragraph line box with body text. + :deep(p:has(.citation)) { + line-height: 1.625; + } + + :deep(p:has(.citation) + p:has(> img:only-child)), + :deep(p:has(> .citation:only-child) + p:has(> img:only-child)) { + margin-top: 0.5em; + } + + // Models often emit standalone **小节标题:** paragraphs instead of headings. + :deep(p:has(> strong:only-child)) { + margin-top: 1.25em; + margin-bottom: 0.5em; + line-height: 1.5; + + &:first-child { + margin-top: 0; + } + } + + :deep(p + ul), + :deep(p + ol), + :deep(ul + p), + :deep(ol + p), + :deep(blockquote + p), + :deep(p + blockquote) { + margin-top: 0.75em; + } + + :deep(strong) { + font-weight: 600; + } + + :deep(em) { + font-style: italic; + } + + :deep(del) { + text-decoration: line-through; + opacity: 0.72; + } + + :deep(kbd) { + display: inline-block; + padding: 0.15em 0.45em; + font-family: var(--app-font-family-mono); + font-size: 0.8125em; + font-weight: 500; + line-height: 1.4; + color: var(--td-text-color-primary); + background: var(--td-bg-color-secondarycontainer); + border: 1px solid var(--td-component-stroke); + border-radius: 5px; + box-shadow: 0 1px 0 color-mix(in srgb, var(--td-text-color-primary) 8%, transparent); + } + + :deep(code):not(pre code) { + background: color-mix(in srgb, var(--td-text-color-primary) 8%, transparent); + padding: 0.15em 0.3em; + border-radius: 4px; + font-family: var(--app-font-family-mono); + font-size: 0.875em; + font-weight: 500; + } + + :deep(pre:not(.chat-code-block__pre):not(.chat-mermaid-block__canvas)) { + background: var(--td-bg-color-secondarycontainer); + padding: 14px 16px; + border: 1px solid var(--td-component-stroke); + border-radius: 8px; + overflow-x: auto; + margin: 0.75em 0; + line-height: 1.55; + font-size: 13px; + + code { + background: none; + padding: 0; + font-size: inherit; + font-weight: 400; + } + } + + :deep(.chat-code-block) { + margin: 0.75em 0; + border: 1px solid var(--td-component-stroke); + border-radius: 8px; + overflow: hidden; + background: var(--td-bg-color-container); + box-shadow: 0 1px 2px color-mix(in srgb, var(--td-text-color-primary) 6%, transparent); + } + + :deep(.chat-code-block__header) { + display: flex; + align-items: center; + justify-content: space-between; + gap: 12px; + min-height: 36px; + padding: 6px 10px 6px 12px; + background: var(--td-bg-color-secondarycontainer); + border-bottom: 1px solid var(--td-component-stroke); + } + + :deep(.chat-code-block__lang) { + font-size: 12px; + font-weight: 600; + line-height: 1.2; + color: var(--td-text-color-secondary); + letter-spacing: 0.02em; + text-transform: none; + } + + :deep(.chat-code-block__copy) { + display: inline-flex; + align-items: center; + gap: 6px; + margin: 0; + padding: 4px 8px; + border: 1px solid transparent; + border-radius: 6px; + background: transparent; + color: var(--td-text-color-secondary); + font-size: 12px; + font-weight: 500; + line-height: 1; + cursor: pointer; + transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease; + + &:hover { + background: color-mix(in srgb, var(--td-text-color-primary) 6%, var(--td-bg-color-secondarycontainer)); + border-color: var(--td-component-border); + color: var(--td-text-color-primary); + } + + &.is-copied { + color: var(--td-text-color-primary); + } + + &.is-error { + color: var(--td-error-color); + } + } + + :deep(.chat-code-block__copy-icon) { + flex-shrink: 0; + } + + :deep(.chat-code-block__pre) { + margin: 0; + padding: 14px 16px; + border-radius: 0; + background: color-mix(in srgb, var(--td-bg-color-secondarycontainer) 88%, var(--td-bg-color-container)); + overflow-x: auto; + font-size: 13px; + line-height: 1.55; + + code { + background: transparent; + padding: 0; + border-radius: 0; + font-size: inherit; + font-weight: 400; + white-space: pre; + display: block; + } + } + + :deep(.chat-mermaid-block) { + margin: 0.75em 0; + border: 1px solid var(--td-component-stroke); + border-radius: 10px; + overflow: hidden; + background: var(--td-bg-color-container); + box-shadow: 0 1px 3px color-mix(in srgb, var(--td-text-color-primary) 5%, transparent); + } + + :deep(.chat-mermaid-block__header) { + display: flex; + align-items: center; + justify-content: space-between; + gap: 8px; + min-height: 36px; + padding: 6px 10px 6px 12px; + background: var(--td-bg-color-secondarycontainer); + border-bottom: 1px solid var(--td-component-stroke); + } + + :deep(.chat-mermaid-block__badge) { + display: inline-flex; + align-items: center; + gap: 6px; + font-size: 12px; + font-weight: 600; + letter-spacing: 0.04em; + text-transform: uppercase; + color: var(--td-text-color-secondary); + } + + :deep(.chat-mermaid-block__expand) { + display: inline-flex; + align-items: center; + justify-content: center; + width: 28px; + height: 28px; + margin: 0; + padding: 0; + border: 1px solid transparent; + border-radius: 6px; + background: transparent; + color: var(--td-text-color-secondary); + cursor: pointer; + transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease; + + &:hover:not(:disabled) { + background: color-mix(in srgb, var(--td-text-color-primary) 6%, var(--td-bg-color-secondarycontainer)); + border-color: var(--td-component-border); + color: var(--td-text-color-primary); + } + + &:disabled, + &.is-disabled { + opacity: 0.35; + cursor: not-allowed; + } + } + + :deep(.chat-mermaid-block__canvas) { + margin: 0; + padding: 20px 16px 18px; + border: 0; + border-radius: 0; + background: var(--td-bg-color-container); + overflow-x: auto; + text-align: center; + cursor: zoom-in; + + svg { + max-width: 100%; + height: auto; + } + + .node rect, + .node circle, + .node ellipse, + .node polygon, + .node path { + stroke-width: 1.5px; + } + + .edgePath path, + .flowchart-link { + stroke-width: 1.6px; + } + + .label, + .nodeLabel, + .edgeLabel { + font-family: var(--app-font-family); + } + } + + :deep(.chat-mermaid-block--loading .streaming-mermaid-loading) { + margin: 0; + max-width: none; + min-height: 200px; + border: 0; + border-radius: 0; + } + + :deep(pre.mermaid:not(.chat-mermaid-block__canvas)) { + margin: 0.75em 0; + padding: 20px 16px; + border: 1px solid var(--td-component-stroke); + border-radius: 10px; + background: var(--td-bg-color-secondarycontainer); + overflow-x: auto; + text-align: center; + cursor: zoom-in; + + svg { + max-width: 100%; + height: auto; + } + } + + :deep(ul) { + margin: 0.35em 0 0.75em; + padding-left: 1.625em; + list-style-type: disc; + list-style-position: outside; + + ul { + list-style-type: disc; + margin: 0.25em 0 0; + padding-left: 1.25em; + } + } + + :deep(ol) { + margin: 0.35em 0 0.75em; + padding-left: 1.625em; + list-style-type: decimal; + list-style-position: outside; + + ol { + list-style-type: decimal; + margin: 0.25em 0 0; + padding-left: 1.25em; + } + } + + :deep(li) { + margin: 0; + line-height: 1.625; + padding-left: 0.375em; + + + li { + margin-top: 0.375em; + } + + &::marker { + font-weight: 700; + color: var(--td-text-color-primary); + } + + > p { + margin: 0; + + + p { + margin-top: 0.25em; + } + } + } + + :deep(ol > li)::marker { + font-variant-numeric: tabular-nums; + } + + :deep(li > ul), + :deep(li > ol) { + margin-top: 0.25em; + margin-bottom: 0; + } + + :deep(li:has(> input[type='checkbox'])) { + list-style: none; + margin-left: -1.625em; + padding-left: 0; + + &::marker { + content: none; + } + + input[type='checkbox'] { + margin: 0 0.5em 0 0; + vertical-align: middle; + accent-color: var(--td-brand-color); + } + } + + :deep(blockquote) { + border-left: 2px solid var(--td-component-stroke); + padding: 0.5em 0 0.5em 1.25em; + margin: 0.5em 0 0.75em; + color: var(--td-text-color-secondary); + font-weight: 400; + font-style: normal; + + blockquote { + margin-top: 0.35em; + margin-bottom: 0; + } + } + + :deep(blockquote), + :deep(blockquote p) { + line-height: 1.5; + } + + :deep(hr) { + margin: 1.75em 0; + border: none; + border-top: 1px solid var(--td-component-stroke); + } + + :deep(h1) { + font-size: 1.5em; + font-weight: 600; + margin: 0 0 0.375em; + line-height: 1.333; + } + + :deep(h2) { + font-size: 1.25em; + font-weight: 600; + margin: 1em 0 0.25em; + line-height: 1.4; + + &:first-child { + margin-top: 0; + } + } + + :deep(h3) { + font-size: 1.125em; + font-weight: 600; + margin: 0.875em 0 0.25em; + line-height: 1.556; + } + + :deep(h4), + :deep(h5), + :deep(h6) { + font-weight: 600; + margin: 1em 0 0; + line-height: 1.5; + } + + :deep(h4) { + font-size: 1em; + } + + :deep(h5) { + font-size: 0.9375em; + } + + :deep(h6) { + font-size: 0.875em; + font-weight: 600; + color: var(--td-text-color-secondary); + } + + :deep(h1:first-child), + :deep(h2:first-child), + :deep(h3:first-child), + :deep(h4:first-child), + :deep(h5:first-child), + :deep(h6:first-child) { + margin-top: 0; + } + + :deep(a) { + color: var(--td-brand-color); + text-decoration: underline; + text-decoration-color: color-mix(in srgb, var(--td-brand-color) 45%, transparent); + text-underline-offset: 0.18em; + + &:hover { + color: var(--td-brand-color-hover); + text-decoration-color: var(--td-brand-color); + } + } + + :deep(.chat-markdown-table) { + width: fit-content; + max-width: 100%; + margin: 0.875em 0 1em; + overflow-x: auto; + border: 1px solid var(--td-component-stroke); + border-radius: 8px; + background: var(--td-bg-color-container); + -webkit-overflow-scrolling: touch; + } + + :deep(table) { + word-break: initial; + border-collapse: separate; + border-spacing: 0; + display: table; + font-size: 14px; + line-height: 1.55; + width: max-content; + min-width: 0; + } + + :deep(table thead) { + background-color: color-mix(in srgb, var(--td-bg-color-secondarycontainer) 75%, var(--td-bg-color-container)); + } + + :deep(table tbody tr:nth-child(2n)) { + background-color: color-mix(in srgb, var(--td-bg-color-secondarycontainer) 45%, transparent); + } + + :deep(table tr th) { + font-weight: 600; + border: 0; + border-right: 1px solid var(--td-component-stroke); + border-bottom: 1px solid var(--td-component-stroke); + background-color: color-mix(in srgb, var(--td-bg-color-secondarycontainer) 75%, var(--td-bg-color-container)); + text-align: left; + margin: 0; + padding: 8px 12px; + white-space: nowrap; + } + + :deep(table tr td) { + border: 0; + border-right: 1px solid var(--td-component-stroke); + border-bottom: 1px solid var(--td-component-stroke); + text-align: left; + margin: 0; + padding: 8px 12px; + vertical-align: top; + } + + :deep(table tr th:last-child), + :deep(table tr td:last-child) { + border-right: 0; + } + + :deep(table tbody tr:last-child td) { + border-bottom: 0; + } + + :deep(table th[align='center']), + :deep(table td[align='center']) { + text-align: center; + } + + :deep(table th[align='right']), + :deep(table td[align='right']) { + text-align: right; + } + + :deep(img) { + max-width: 80%; + max-height: 300px; + width: auto; + height: auto; + border-radius: 8px; + display: block; + margin: 1.25em 0; + border: 0.5px solid var(--td-component-stroke); + object-fit: contain; + cursor: pointer; + transition: transform 0.2s ease; + } + + :deep(p:has(> img:only-child)) { + margin: 1.25em 0; + line-height: 0; + } + + :deep(.mermaid) { + margin: 0; + } + + .chat-markdown-hljs(); +} diff --git a/frontend/src/components/css/markdown.less b/frontend/src/components/css/markdown.less index 203ef056..4bebe936 100644 --- a/frontend/src/components/css/markdown.less +++ b/frontend/src/components/css/markdown.less @@ -1,3 +1,5 @@ +// Legacy Markdown styles for non-chat document/editor surfaces that render +// `.md-content`. Chat answer Markdown must use `chat-markdown.less` only. :deep(.md-content) { box-sizing: border-box !important; @@ -440,4 +442,4 @@ .outline-label { font-family: var(--app-font-family); } -} \ No newline at end of file +} diff --git a/frontend/src/main.ts b/frontend/src/main.ts index 075131de..36d734d7 100644 --- a/frontend/src/main.ts +++ b/frontend/src/main.ts @@ -8,6 +8,7 @@ import TDesign from "tdesign-vue-next"; import "tdesign-vue-next/es/style/index.css"; import "@/assets/theme/theme.css"; import "@/assets/dropdown-menu.less"; +import "@/components/css/chat-hljs-dark.less"; // vue-virtual-scroller ships its own tiny stylesheet — required for // RecycleScroller/DynamicScroller to size their viewport correctly. // Without it the scroller computes 0 height and renders no items. diff --git a/frontend/src/utils/chatMarkdownRenderer.test.ts b/frontend/src/utils/chatMarkdownRenderer.test.ts new file mode 100644 index 00000000..1dcf2656 --- /dev/null +++ b/frontend/src/utils/chatMarkdownRenderer.test.ts @@ -0,0 +1,181 @@ +import assert from 'node:assert/strict' +import test from 'node:test' + +import { + createChatMarkdownRenderer, + preprocessMathDelimiters, + renderChatMarkdown, + replaceIncompleteImageWithPlaceholder, +} from './chatMarkdownRenderer.ts' +import { resolveCitationChunkId, joinCitationTagsToPreviousLine, collapseStandaloneCitationParagraphs } from './citationMarkdown.ts' + +const SAMPLE_DOC = 'example-report.docx' +const SAMPLE_CHUNK_A = '00000001-0000-4000-8000-000000000001' +const SAMPLE_CHUNK_B = '00000002-0000-4000-8000-000000000002' +const SAMPLE_CHUNK_C = '00000003-0000-4000-8000-000000000003' +const SAMPLE_CHUNK_PRESERVE = 'aaaaaaaa-bbbb-4ccc-8ddd-eeeeeeeeeeee' + +test('preprocessMathDelimiters converts escaped math delimiters for marked-katex', () => { + assert.equal( + preprocessMathDelimiters('inline \\(a+b\\) and block \\[x^2\\]'), + 'inline $a+b$ and block $$x^2$$', + ) +}) + +test('replaceIncompleteImageWithPlaceholder hides an unfinished streaming image', () => { + assert.equal( + replaceIncompleteImageWithPlaceholder('before ![chart](local://bucket/path'), + 'before ', + ) +}) + +test('renderChatMarkdown preserves citations, math, and sanitized output through one shared pipeline', () => { + const renderer = createChatMarkdownRenderer({ + imageRenderer: ({ href, title, text }) => + `${text}`, + isValidImageUrl: (href) => href.startsWith('https://'), + }) + + const html = renderChatMarkdown( + [ + 'See ', + '', + 'Formula: \\(E=mc^2\\)', + '', + '| A | B |', + '| --- | --- |', + '| 1 | 2 |', + '', + '![ok](https://example.com/a.png "图")', + '', + '![bad](javascript:alert(1))', + ].join('\n'), + { + renderer, + escapeMarkdown: (text) => text, + sanitizeHtml: (html) => html.replace(/javascript:alert\(1\)/g, ''), + }, + ) + + assert.match(html, /class="citation citation-kb"/) + assert.match(html, /data-chunk-id="chunk-1"/) + assert.match(html, /katex/) + assert.match(html, /
/) + assert.match(html, / { + const refs = [ + { id: 'uuid-chunk-1', knowledge_title: 'Doc A', chunk_type: 'faq' }, + { id: 'uuid-chunk-2', knowledge_title: 'FAQ TEST - FAQ', chunk_type: 'faq' }, + ] + + assert.equal( + resolveCitationChunkId('2', { doc: 'FAQ TEST - FAQ' }, refs), + 'uuid-chunk-2', + ) + assert.equal( + resolveCitationChunkId('FAQ-2', { doc: 'FAQ TEST - FAQ' }, refs), + 'uuid-chunk-2', + ) + assert.equal( + resolveCitationChunkId('uuid-chunk-1', { doc: 'Doc A' }, refs), + 'uuid-chunk-1', + ) +}) + +test('renderChatMarkdown preserves chunk UUIDs when escapeMarkdown strips UUIDs from prose', () => { + const renderer = createChatMarkdownRenderer({ + imageRenderer: ({ href, text }) => `${text}`, + isValidImageUrl: () => true, + }) + const chunkId = SAMPLE_CHUNK_PRESERVE + const stripUuids = (text: string) => text.replace( + /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi, + '', + ) + + const html = renderChatMarkdown( + `Sample text `, + { + renderer, + escapeMarkdown: stripUuids, + sanitizeHtml: (html) => html, + }, + ) + + assert.match(html, /class="citation citation-kb"/) + assert.match(html, new RegExp(`data-chunk-id="${chunkId}"`)) +}) + +test('renderChatMarkdown resolves indexed chunk_id when knowledge references are provided', () => { + const renderer = createChatMarkdownRenderer({ + imageRenderer: ({ href, text }) => `${text}`, + isValidImageUrl: () => true, + }) + + const html = renderChatMarkdown( + 'See ', + { + renderer, + escapeMarkdown: (text) => text, + sanitizeHtml: (html) => html, + knowledgeReferences: [ + { id: 'resolved-chunk-id', knowledge_title: 'Sample FAQ', chunk_type: 'faq' }, + { id: 'other-chunk', knowledge_title: 'Other FAQ', chunk_type: 'faq' }, + ], + }, + ) + + assert.match(html, /data-chunk-id="resolved-chunk-id"/) + assert.doesNotMatch(html, /data-chunk-id="2"/) +}) + +test('joinCitationTagsToPreviousLine removes blank lines before citation tags', () => { + const input = 'Setup is complete.\n\n' + assert.equal(joinCitationTagsToPreviousLine(input), 'Setup is complete. ') +}) + +test('joinCitationTagsToPreviousLine inlines consecutive citation tags across single newlines', () => { + const tag1 = `` + const tag2 = `` + const tag3 = `` + const input = `${tag1}\n${tag2}\n${tag3}` + assert.equal(joinCitationTagsToPreviousLine(input), `${tag1} ${tag2} ${tag3}`) +}) + +test('renderChatMarkdown inlines consecutive citation tags across newlines', () => { + const renderer = createChatMarkdownRenderer({ + imageRenderer: ({ href, text }) => `${text}`, + isValidImageUrl: () => true, + }) + const html = renderChatMarkdown( + [ + ``, + ``, + ``, + ].join('\n'), + { + renderer, + escapeMarkdown: (text) => text, + sanitizeHtml: (html) => html, + }, + ) + + assert.equal((html.match(/citation-kb/g) || []).length, 3) + assert.doesNotMatch(html, /<\/p>\s*

\s* { + const tag = '' + const input = `1. first\n2. second\n\n${tag}` + assert.equal(joinCitationTagsToPreviousLine(input), `1. first\n2. second\n\n${tag}`) +}) + +test('collapseStandaloneCitationParagraphs merges citations across empty paragraphs', () => { + const html = '

Steps:

doc

' + const out = collapseStandaloneCitationParagraphs(html) + assert.match(out, /Steps:.*citation-kb/s) + assert.doesNotMatch(out, /

<\/p>/) +}) diff --git a/frontend/src/utils/chatMarkdownRenderer.ts b/frontend/src/utils/chatMarkdownRenderer.ts new file mode 100644 index 00000000..44e020f6 --- /dev/null +++ b/frontend/src/utils/chatMarkdownRenderer.ts @@ -0,0 +1,143 @@ +import { marked, type Renderer } from 'marked' +import markedKatex from 'marked-katex-extension' +import type { Tokens } from 'marked' + +import { + collapseStandaloneCitationParagraphs, + extractCitationHtmlPlaceholders, + joinCitationTagsToPreviousLine, + preserveCitationTags, + restoreCitationHtmlPlaceholders, + restoreCitationTags, + type CitationKnowledgeRef, +} from './citationMarkdown.ts' + +const STREAMING_IMAGE_PLACEHOLDER = + '' + +let markedConfigured = false + +export type ImageRendererArgs = { + href: string + title: string | null + text: string +} + +export type ChatMarkdownRendererOptions = { + codeRenderer?: Renderer['code'] + imageRenderer?: (args: ImageRendererArgs) => string + invalidImageHtml?: (href: string) => string + isValidImageUrl?: (href: string) => boolean +} + +export type RenderChatMarkdownOptions = { + renderer: Renderer + escapeMarkdown: (markdown: string) => string + sanitizeHtml: (html: string) => string + collapseStandaloneCitations?: boolean + knowledgeReferences?: CitationKnowledgeRef[] | null + cachedMermaidSvgHtml?: string | null + injectCachedMermaidSvg?: (html: string, cachedSvgHtml?: string | null) => string + prepareMarkdown?: (markdown: string, cachedSvgHtml?: string | null) => string +} + +export function configureMarkedForChatMarkdown(): void { + if (markedConfigured) return + marked.use({ breaks: true, gfm: true }) + marked.use(markedKatex({ throwOnError: false, nonStandard: true })) + markedConfigured = true +} + +export function preprocessMathDelimiters(rawText: string): string { + if (!rawText || typeof rawText !== 'string') return '' + return rawText + .replace(/\\\[([\s\S]*?)\\\]/g, '$$$$$1$$$$') + .replace(/\\\(([\s\S]*?)\\\)/g, '$$$1$$') +} + +export function replaceIncompleteImageWithPlaceholder(content: string): string { + if (!content) return '' + + const lastImgStart = content.lastIndexOf('![') + if (lastImgStart < 0) return content + + const tail = content.slice(lastImgStart) + const hasImageOpen = tail.startsWith('![') + const hasBracketClose = tail.includes(']') + const hasParenOpen = tail.includes('(') + const hasParenClose = tail.includes(')') + if (!hasImageOpen) return content + + if (!hasBracketClose || (hasParenOpen && !hasParenClose)) { + return content.slice(0, lastImgStart) + STREAMING_IMAGE_PLACEHOLDER + } + + return content +} + +export function createChatMarkdownRenderer(options: ChatMarkdownRendererOptions = {}): Renderer { + const renderer = new marked.Renderer() + + if (options.imageRenderer) { + renderer.image = ({ href, title, text }: Tokens.Image) => { + const imageHref = href || '' + if (options.isValidImageUrl && !options.isValidImageUrl(imageHref)) { + return options.invalidImageHtml?.(imageHref) ?? '' + } + return options.imageRenderer?.({ + href: imageHref, + title: title || null, + text: text || '', + }) ?? '' + } + } + + if (options.codeRenderer) { + renderer.code = options.codeRenderer + } + + return renderer +} + +export function wrapChatMarkdownTables(html: string): string { + if (!html || !html.includes('/gi, + (tableHtml) => `

${tableHtml}
`, + ) +} + +export function renderChatMarkdown(rawMarkdown: unknown, options: RenderChatMarkdownOptions): string { + const rawText = typeof rawMarkdown === 'string' ? rawMarkdown : String(rawMarkdown || '') + if (!rawText.trim()) return '' + + configureMarkedForChatMarkdown() + + const { text: tagSafe, tags } = preserveCitationTags(rawText) + const imageSafe = replaceIncompleteImageWithPlaceholder(tagSafe) + const mathSafe = preprocessMathDelimiters(imageSafe) + const restoredTags = restoreCitationTags(mathSafe, tags) + const inlineTags = joinCitationTagsToPreviousLine(restoredTags) + const preparedMarkdown = options.prepareMarkdown + ? options.prepareMarkdown(inlineTags, options.cachedMermaidSvgHtml) + : inlineTags + // Convert //wiki tags to HTML placeholders before escapeMarkdown so + // agent sanitizers (e.g. UUID stripping) cannot damage chunk_id attributes. + const { content: markdownWithPlaceholders, htmlSnippets } = + extractCitationHtmlPlaceholders(preparedMarkdown, options.knowledgeReferences) + const escapedMarkdown = options.escapeMarkdown(markdownWithPlaceholders) + const html = marked.parse(markdownWithPlaceholders, { + renderer: options.renderer, + breaks: true, + async: false, + }) as string + const restoredHtml = restoreCitationHtmlPlaceholders(html, htmlSnippets) + const citationHtml = options.collapseStandaloneCitations === false + ? restoredHtml + : collapseStandaloneCitationParagraphs(restoredHtml) + const tableWrappedHtml = wrapChatMarkdownTables(citationHtml) + const sanitized = options.sanitizeHtml(tableWrappedHtml) + return options.injectCachedMermaidSvg + ? options.injectCachedMermaidSvg(sanitized, options.cachedMermaidSvgHtml) + : sanitized +} diff --git a/frontend/src/utils/markdownDomPurify.test.mjs b/frontend/src/utils/markdownDomPurify.test.mjs new file mode 100644 index 00000000..5b3c23e5 --- /dev/null +++ b/frontend/src/utils/markdownDomPurify.test.mjs @@ -0,0 +1,17 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; +import { + domPurifyAllowedUriRegexp, + markdownDomPurifyConfig, +} from './markdownDomPurify.ts'; + +test('markdownDomPurifyConfig FORBID_TAGS includes script', () => { + assert.ok(Array.isArray(markdownDomPurifyConfig.FORBID_TAGS)); + assert.ok(markdownDomPurifyConfig.FORBID_TAGS.includes('script')); +}); + +test('ALLOWED_URI_REGEXP allows s3:// and rejects javascript:', () => { + const re = markdownDomPurifyConfig.ALLOWED_URI_REGEXP ?? domPurifyAllowedUriRegexp; + assert.match('s3://bucket/key', re); + assert.doesNotMatch('javascript:alert(1)', re); +}); diff --git a/frontend/src/utils/markdownDomPurify.ts b/frontend/src/utils/markdownDomPurify.ts new file mode 100644 index 00000000..a71e6ecd --- /dev/null +++ b/frontend/src/utils/markdownDomPurify.ts @@ -0,0 +1,83 @@ +export const domPurifyForbidTags = ['script', 'style', 'object', 'embed', 'form', 'input'] as const; +export const domPurifyForbidAttr = ['onerror', 'onload', 'onclick', 'onmouseover', 'onfocus', 'onblur'] as const; + +export const domPurifyAllowedUriRegexp = + /^(?:(?:(?:f|ht)tps?|mailto|tel|callto|cid|xmpp):|(?:local|minio|cos|tos|s3|oss|ks3|obs):|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i; + +/** Shared DOMPurify security options (FORBID_*, URI scheme, DOM flags). */ +export const domPurifySecurityOptions = { + ALLOWED_URI_REGEXP: domPurifyAllowedUriRegexp, + FORBID_TAGS: [...domPurifyForbidTags], + FORBID_ATTR: [...domPurifyForbidAttr], + KEEP_CONTENT: true, + RETURN_DOM: false, + RETURN_DOM_FRAGMENT: false, + RETURN_DOM_IMPORT: false, + SANITIZE_DOM: true, + SANITIZE_NAMED_PROPS: true, + WHOLE_DOCUMENT: false, +} as const; + +/** Shared DOMPurify hooks: strip scripts/event attrs; noopener links; alt on images. */ +export const domPurifySecurityHooks = { + beforeSanitizeElements: (currentNode: Element) => { + if (currentNode.tagName === 'SCRIPT') { + currentNode.remove(); + return null; + } + domPurifyForbidAttr.forEach((attr) => { + if (currentNode.hasAttribute(attr)) { + currentNode.removeAttribute(attr); + } + }); + }, + afterSanitizeElements: (currentNode: Element) => { + if (currentNode.tagName === 'A') { + const href = currentNode.getAttribute('href'); + if (href && href.startsWith('http')) { + currentNode.setAttribute('rel', 'noopener noreferrer'); + currentNode.setAttribute('target', '_blank'); + } + } + if (currentNode.tagName === 'IMG' && !currentNode.getAttribute('alt')) { + currentNode.setAttribute('alt', ''); + } + }, +}; + +/** Shared DOMPurify config for chat / dev markdown (KaTeX + Mermaid SVG). */ +export const markdownDomPurifyConfig = { + 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', 'div', + 'svg', 'g', 'path', 'rect', 'circle', 'ellipse', 'line', 'polygon', + 'polyline', 'text', 'tspan', 'defs', 'marker', 'filter', 'use', + 'clippath', 'lineargradient', 'radialgradient', 'stop', 'pattern', + 'image', 'foreignobject', 'desc', 'title', 'switch', 'symbol', 'mask', + 'math', 'annotation', 'semantics', 'mo', 'mi', 'mn', 'msup', 'mrow', 'mfrac', 'msqrt', 'mroot', 'mstyle', + 'button', + ], + ALLOWED_ATTR: [ + 'href', 'title', 'target', 'rel', 'data-tooltip', 'data-url', 'data-kb-id', + 'data-chunk-id', 'data-doc', 'data-slug', 'class', 'role', 'tabindex', 'src', 'alt', 'data-protected-src', + 'width', 'height', 'style', 'id', 'type', 'aria-label', 'data-mermaid', 'disabled', + 'd', 'fill', 'stroke', 'stroke-width', 'stroke-linecap', 'stroke-linejoin', + 'stroke-dasharray', 'stroke-dashoffset', 'stroke-miterlimit', 'stroke-opacity', + 'fill-opacity', 'opacity', 'transform', 'viewbox', 'preserveaspectratio', + 'x', 'y', 'x1', 'y1', 'x2', 'y2', 'cx', 'cy', 'rx', 'ry', 'r', + 'dx', 'dy', 'text-anchor', 'dominant-baseline', 'font-family', 'font-size', + 'font-weight', 'font-style', 'letter-spacing', 'word-spacing', + 'marker-start', 'marker-mid', 'marker-end', 'markerunits', 'markerwidth', + 'markerheight', 'refx', 'refy', 'orient', 'points', 'offset', + 'gradientunits', 'gradienttransform', 'spreadmethod', 'stop-color', 'stop-opacity', + 'patternunits', 'patterntransform', 'clippathunits', 'maskunits', + 'filterunits', 'primitiveunits', 'xmlns', 'xmlns:xlink', 'xlink:href', + 'version', 'baseprofile', 'enable-background', 'overflow', 'visibility', + 'display', 'pointer-events', 'cursor', 'data-emit', 'direction', + 'mathvariant', 'encoding', 'aria-hidden', + ], + USE_PROFILES: { html: true, svg: true, mathMl: true }, + ...domPurifySecurityOptions, + HOOKS: domPurifySecurityHooks, +}; diff --git a/frontend/src/utils/markdownEnhancements.ts b/frontend/src/utils/markdownEnhancements.ts new file mode 100644 index 00000000..72a0c91f --- /dev/null +++ b/frontend/src/utils/markdownEnhancements.ts @@ -0,0 +1,231 @@ +import i18n from '@/i18n'; +import hljs from 'highlight.js'; +import { openMermaidFullscreen } from '@/utils/mermaidViewer'; + +const ENHANCE_FLAG = 'data-markdown-enhancements'; +const boundMarkdownRoots = new WeakSet(); + +const MERMAID_DIAGRAM_SVG_SELECTOR = '.chat-mermaid-block__canvas svg, pre.mermaid svg'; + +function getMermaidDiagramSvg(block: Element | null | undefined): SVGElement | null { + if (!block) return null; + const svg = block.querySelector(MERMAID_DIAGRAM_SVG_SELECTOR); + return svg instanceof SVGElement ? svg : null; +} + +function openMermaidFromBlock(block: Element | null | undefined): void { + const svg = getMermaidDiagramSvg(block); + if (svg) openMermaidFullscreen(svg.outerHTML); +} + +export function syncMermaidExpandButtons(root: HTMLElement | null | undefined): void { + if (!root) return; + root.querySelectorAll('.chat-mermaid-block').forEach((block) => { + const expandBtn = block.querySelector('.chat-mermaid-block__expand'); + if (!expandBtn) return; + const hasSvg = !!getMermaidDiagramSvg(block); + expandBtn.disabled = !hasSvg; + expandBtn.classList.toggle('is-disabled', !hasSvg); + }); +} + +const COPY_ICON = ''; + +const EXPAND_ICON = ''; + +const LANG_LABELS: Record = { + js: 'JavaScript', + javascript: 'JavaScript', + ts: 'TypeScript', + typescript: 'TypeScript', + py: 'Python', + python: 'Python', + go: 'Go', + rust: 'Rust', + java: 'Java', + kotlin: 'Kotlin', + swift: 'Swift', + rb: 'Ruby', + ruby: 'Ruby', + php: 'PHP', + cs: 'C#', + cpp: 'C++', + c: 'C', + sql: 'SQL', + bash: 'Bash', + sh: 'Shell', + shell: 'Shell', + json: 'JSON', + yaml: 'YAML', + yml: 'YAML', + xml: 'XML', + html: 'HTML', + css: 'CSS', + markdown: 'Markdown', + md: 'Markdown', +}; + +function escapeHtml(text: string): string { + return text + .replace(/&/g, '&') + .replace(//g, '>') + .replace(/"/g, '"'); +} + +export function formatCodeLang(lang: string): string { + const normalized = (lang || 'Code').trim(); + if (!normalized) return 'Code'; + const key = normalized.toLowerCase(); + return LANG_LABELS[key] || normalized.charAt(0).toUpperCase() + normalized.slice(1); +} + +export function buildCodeBlockHtml( + lang: string, + highlighted: string, + highlightLang: string, +): string { + const displayLang = escapeHtml(formatCodeLang(lang)); + const copyLabel = escapeHtml(i18n.global.t('embedPublish.copyCode')); + const safeLang = escapeHtml(highlightLang || lang || 'text'); + return `
+
+ ${displayLang} +
+ +
+
+
${highlighted}
+
`; +} + +export function buildMermaidBlockHtml(innerHtml: string, preAttrs = ''): string { + const label = escapeHtml(i18n.global.t('mermaid.diagram')); + const expandLabel = escapeHtml(i18n.global.t('mermaid.expand')); + const attrs = preAttrs ? ` ${preAttrs}` : ''; + return `
+
+ ${label} +
+ +
+
+
${innerHtml}
+
`; +} + +export function buildMermaidLoadingHtml(): string { + const label = escapeHtml(i18n.global.t('mermaid.diagram')); + return `
+
+ ${label} +
+ +
`; +} + +async function handleCodeCopy(btn: HTMLButtonElement): Promise { + const code = btn.closest('.chat-code-block')?.querySelector('code')?.textContent ?? ''; + if (!code) return; + + const textEl = btn.querySelector('.chat-code-block__copy-text'); + const copiedLabel = i18n.global.t('common.copied'); + const defaultLabel = i18n.global.t('embedPublish.copyCode'); + + try { + if (navigator.clipboard?.writeText) { + await navigator.clipboard.writeText(code); + } else { + const textArea = document.createElement('textarea'); + textArea.value = code; + textArea.style.position = 'fixed'; + textArea.style.opacity = '0'; + document.body.appendChild(textArea); + textArea.select(); + document.execCommand('copy'); + document.body.removeChild(textArea); + } + btn.classList.add('is-copied'); + if (textEl) textEl.textContent = copiedLabel; + window.setTimeout(() => { + btn.classList.remove('is-copied'); + if (textEl) textEl.textContent = defaultLabel; + }, 1600); + } catch { + btn.classList.add('is-error'); + window.setTimeout(() => btn.classList.remove('is-error'), 1600); + } +} + +export function highlightCodeBlocksInContainer(root: HTMLElement | null | undefined): void { + if (!root) return; + + root.querySelectorAll('.chat-code-block__pre code').forEach((codeEl) => { + if (codeEl.querySelector('span[class^="hljs-"], span.hljs')) return; + const language = [...codeEl.classList] + .find((cls) => cls.startsWith('language-')) + ?.slice('language-'.length); + if (language && hljs.getLanguage(language)) { + try { + codeEl.innerHTML = hljs.highlight(codeEl.textContent || '', { language }).value; + codeEl.classList.add('hljs'); + return; + } catch { + // fall through + } + } + hljs.highlightElement(codeEl); + }); +} + +export function refreshMarkdownEnhancements(root: HTMLElement | null | undefined): void { + if (!root) return; + highlightCodeBlocksInContainer(root); + syncMermaidExpandButtons(root); +} + +export function attachMarkdownEnhancementListeners( + root: HTMLElement | null | undefined, +): void { + if (!root || boundMarkdownRoots.has(root)) return; + boundMarkdownRoots.add(root); + root.setAttribute(ENHANCE_FLAG, 'true'); + + const onClick = (event: Event) => { + const target = event.target as HTMLElement; + + const copyBtn = target.closest('.chat-code-block__copy'); + if (copyBtn) { + event.preventDefault(); + event.stopPropagation(); + void handleCodeCopy(copyBtn); + return; + } + + const expandBtn = target.closest('.chat-mermaid-block__expand'); + if (expandBtn) { + event.preventDefault(); + event.stopPropagation(); + if (expandBtn.disabled) return; + openMermaidFromBlock(expandBtn.closest('.chat-mermaid-block')); + return; + } + + const canvas = target.closest( + '.chat-mermaid-block__canvas[data-mermaid="true"], .chat-mermaid-block__canvas[data-mermaid="cached"], pre.mermaid[data-mermaid="true"], pre.mermaid[data-mermaid="cached"]', + ); + if (canvas && !target.closest('button')) { + const svg = canvas.querySelector('svg'); + if (svg) { + event.preventDefault(); + event.stopPropagation(); + openMermaidFullscreen(svg.outerHTML); + } + } + }; + + root.addEventListener('click', onClick, true); + syncMermaidExpandButtons(root); +} diff --git a/frontend/src/utils/mermaidShared.ts b/frontend/src/utils/mermaidShared.ts index f29f57df..3a26908e 100644 --- a/frontend/src/utils/mermaidShared.ts +++ b/frontend/src/utils/mermaidShared.ts @@ -1,37 +1,155 @@ import type { Tokens } from 'marked' +import hljs from 'highlight.js' +import 'highlight.js/styles/github.css' import { openMermaidFullscreen } from '@/utils/mermaidViewer.ts' +import { + buildCodeBlockHtml, + buildMermaidBlockHtml, + attachMarkdownEnhancementListeners, + highlightCodeBlocksInContainer, + syncMermaidExpandButtons, +} from '@/utils/markdownEnhancements' + +hljs.registerAliases('mermaid', { languageName: 'plaintext' }) let mermaidMod: typeof import('mermaid') | null = null -let hljsMod: typeof import('highlight.js') | null = null let mermaidInitialized = false let initPromise: Promise | null = null -let hljsPromise: Promise | null = null + +const MERMAID_LIGHT_THEME = { + darkMode: false, + background: '#ffffff', + primaryColor: '#e2e8f0', + primaryTextColor: '#334155', + primaryBorderColor: '#94a3b8', + secondaryColor: '#f1f5f9', + secondaryTextColor: '#475569', + secondaryBorderColor: '#cbd5e1', + tertiaryColor: '#f8fafc', + tertiaryTextColor: '#64748b', + tertiaryBorderColor: '#e2e8f0', + lineColor: '#94a3b8', + textColor: '#334155', + mainBkg: '#ffffff', + nodeBorder: '#94a3b8', + clusterBkg: '#f8fafc', + clusterBorder: '#cbd5e1', + titleColor: '#1e293b', + edgeLabelBackground: '#ffffff', + actorBorder: '#94a3b8', + actorBkg: '#f1f5f9', + actorTextColor: '#334155', + actorLineColor: '#94a3b8', + signalColor: '#94a3b8', + labelBoxBkgColor: '#f1f5f9', + labelBoxBorderColor: '#cbd5e1', + labelTextColor: '#334155', + loopTextColor: '#475569', + noteBkgColor: '#f8fafc', + noteTextColor: '#475569', + noteBorderColor: '#cbd5e1', + // Gantt + sectionBkgColor: '#f8fafc', + altSectionBkgColor: '#ffffff', + gridColor: '#e2e8f0', + todayLineColor: '#64748b', + taskBorderColor: '#94a3b8', + taskBkgColor: '#e2e8f0', + activeTaskBorderColor: '#64748b', + activeTaskBkgColor: '#94a3b8', + doneTaskBkgColor: '#cbd5e1', + doneTaskBorderColor: '#94a3b8', + critBkgColor: '#fecaca', + critBorderColor: '#f87171', + fontSize: '14px', +} + +const MERMAID_DARK_THEME = { + darkMode: true, + background: '#1a1f28', + primaryColor: '#475569', + primaryTextColor: '#e2e8f0', + primaryBorderColor: '#64748b', + secondaryColor: '#334155', + secondaryTextColor: '#cbd5e1', + secondaryBorderColor: '#64748b', + tertiaryColor: '#1e293b', + tertiaryTextColor: '#94a3b8', + tertiaryBorderColor: '#475569', + lineColor: '#64748b', + textColor: '#e2e8f0', + mainBkg: '#1e293b', + nodeBorder: '#64748b', + clusterBkg: '#1a2332', + clusterBorder: '#475569', + titleColor: '#f1f5f9', + edgeLabelBackground: '#1e293b', + actorBorder: '#64748b', + actorBkg: '#1e293b', + actorTextColor: '#f1f5f9', + actorLineColor: '#64748b', + signalColor: '#64748b', + labelBoxBkgColor: '#334155', + labelBoxBorderColor: '#64748b', + labelTextColor: '#e2e8f0', + loopTextColor: '#cbd5e1', + noteBkgColor: '#334155', + noteTextColor: '#e2e8f0', + noteBorderColor: '#64748b', + // Gantt + sectionBkgColor: '#1a2332', + altSectionBkgColor: '#1e293b', + gridColor: '#334155', + todayLineColor: '#94a3b8', + taskBorderColor: '#64748b', + taskBkgColor: '#475569', + activeTaskBorderColor: '#94a3b8', + activeTaskBkgColor: '#64748b', + doneTaskBkgColor: '#334155', + doneTaskBorderColor: '#64748b', + critBkgColor: '#7f1d1d', + critBorderColor: '#ef4444', + fontSize: '14px', +} + +function resolveMermaidThemeVariables() { + const isDark = document.documentElement.getAttribute('theme-mode') === 'dark' + return isDark ? MERMAID_DARK_THEME : MERMAID_LIGHT_THEME +} const MERMAID_CONFIG = { startOnLoad: false, - theme: 'default', - securityLevel: 'strict', + theme: 'base' as const, + securityLevel: 'strict' as const, fontFamily: 'PingFang SC, Microsoft YaHei, sans-serif', flowchart: { useMaxWidth: true, htmlLabels: true, curve: 'basis', + padding: 16, }, sequence: { useMaxWidth: true, - diagramMarginX: 8, - diagramMarginY: 8, - actorMargin: 50, - width: 150, - height: 65, + diagramMarginX: 12, + diagramMarginY: 12, + actorMargin: 56, + width: 156, + height: 68, + boxMargin: 10, }, gantt: { useMaxWidth: true, - leftPadding: 75, - gridLineStartPadding: 35, - barHeight: 20, - barGap: 4, - topPadding: 50, + leftPadding: 80, + gridLineStartPadding: 40, + barHeight: 22, + barGap: 6, + topPadding: 56, + }, + er: { + useMaxWidth: true, + }, + journey: { + useMaxWidth: true, }, } @@ -42,15 +160,6 @@ async function getMermaid() { return mermaidMod.default } -async function getHljs() { - if (!hljsMod) { - hljsMod = await import('highlight.js') - await import('highlight.js/styles/github.css') - hljsMod.default.registerAliases('mermaid', { languageName: 'plaintext' }) - } - return hljsMod.default -} - function escapeHtml(text: string) { return text .replace(/&/g, '&') @@ -59,12 +168,29 @@ function escapeHtml(text: string) { .replace(/"/g, '"') } +function highlightCode(text: string, lang?: string | null) { + const language = (lang || '').trim() + if (language && hljs.getLanguage(language)) { + try { + const result = hljs.highlight(text, { language }) + return { html: result.value, language: result.language || language } + } catch { + // fall through + } + } + const auto = hljs.highlightAuto(text, language ? [language] : undefined) + return { html: auto.value, language: auto.language || language || 'plaintext' } +} + export const ensureMermaidInitialized = () => { if (!initPromise) { initPromise = (async () => { const mermaid = await getMermaid() if (!mermaidInitialized) { - mermaid.initialize(MERMAID_CONFIG as Parameters[0]) + mermaid.initialize({ + ...MERMAID_CONFIG, + themeVariables: resolveMermaidThemeVariables(), + } as Parameters[0]) mermaidInitialized = true } })() @@ -74,34 +200,29 @@ export const ensureMermaidInitialized = () => { let mermaidCount = 0 export const createMermaidCodeRenderer = (idPrefix: string) => { - if (!hljsPromise) { - hljsPromise = getHljs() - } - return ({ text, lang }: Tokens.Code) => { - let highlighted = escapeHtml(text) - let highlightLang: string = lang || 'Code' - const hljs = hljsMod?.default - if (hljs) { - if (highlightLang && hljs.getLanguage(highlightLang)) { - try { - highlighted = hljs.highlight(text, { language: highlightLang }).value - } catch { - const ret = hljs.highlightAuto(text) - highlighted = ret.value - highlightLang = ret.language || 'Code' - } - } else { - const ret = hljs.highlightAuto(text) - highlighted = ret.value - highlightLang = ret.language || 'Code' - } - } + const { html: highlighted, language: highlightLang } = highlightCode(text, lang) if (lang === 'mermaid') { const id = `${idPrefix}-${++mermaidCount}` - return `
${highlighted}
` + const inner = `${highlighted}` + return buildMermaidBlockHtml(inner, `id="${id}" data-mermaid="false"`) } - return `
${highlighted}
` + return buildCodeBlockHtml(lang || highlightLang, highlighted, highlightLang) + } +} + +export const renderMermaidToSvg = async (code: string, id: string): Promise => { + if (!code.trim()) return null + try { + const mermaid = await getMermaid() + ensureMermaidInitialized() + await initPromise + await mermaid.parse(code) + const { svg } = await mermaid.render(id, code) + return svg + } catch (e) { + console.error('Mermaid rendering error:', e) + return null } } @@ -109,25 +230,36 @@ export const renderMermaidInContainer = async ( rootElement: HTMLElement | null | undefined, ) => { if (!rootElement) return + const mermaid = await getMermaid() ensureMermaidInitialized() await initPromise - const mermaidElements = rootElement.querySelectorAll('pre[data-mermaid="false"]') + const mermaidElements = rootElement.querySelectorAll( + 'pre[data-mermaid="false"], .chat-mermaid-block__canvas[data-mermaid="false"]', + ) for (const el of mermaidElements) { try { const code = el.innerText await mermaid.parse(code) - const { svg } = await mermaid.render(`${el.id}-svg`, code) + const renderId = el.id ? `${el.id}-svg` : `mermaid-render-${++mermaidCount}` + const { svg } = await mermaid.render(renderId, code) el.classList.add('mermaid') el.innerHTML = svg - el.onclick = (event) => { - event.stopPropagation() - openMermaidFullscreen(svg) - } + el.setAttribute('data-mermaid', 'true') } catch (e) { console.error('Mermaid rendering error:', e) + continue } - el.setAttribute('data-mermaid', 'true') } } + +export async function enhanceMarkdownContainer( + rootElement: HTMLElement | null | undefined, +): Promise { + if (!rootElement) return + attachMarkdownEnhancementListeners(rootElement) + highlightCodeBlocksInContainer(rootElement) + await renderMermaidInContainer(rootElement) + syncMermaidExpandButtons(rootElement) +} diff --git a/frontend/src/utils/mermaidViewer.ts b/frontend/src/utils/mermaidViewer.ts index c610231f..088d414b 100644 --- a/frontend/src/utils/mermaidViewer.ts +++ b/frontend/src/utils/mermaidViewer.ts @@ -54,7 +54,7 @@ const downloadSvgAsImage = async (svgElement: SVGElement, filename = 'mermaid-di const showBtnFeedback = (btn: HTMLElement, success: boolean, text?: string): void => { const origColor = btn.style.color const origTitle = btn.title - btn.style.color = success ? '#07c05f' : '#ef4444' + btn.style.color = success ? '#374151' : '#ef4444' btn.title = text || (success ? i18n.global.t('common.success') : i18n.global.t('common.failed')) setTimeout(() => { btn.style.color = origColor @@ -89,7 +89,7 @@ export const openMermaidFullscreen = (svgHtml: string): void => { btn.title = title btn.style.cssText = 'display:flex;align-items:center;justify-content:center;width:36px;height:36px;border:1px solid #e5e7eb;border-radius:6px;background:rgba(255,255,255,0.95);color:#6b7280;cursor:pointer;padding:0;box-shadow:0 2px 8px rgba(0,0,0,0.15);' btn.innerHTML = icon - btn.onmouseenter = () => { btn.style.background = '#f0fdf4'; btn.style.color = '#07c05f' } + btn.onmouseenter = () => { btn.style.background = '#f3f4f6'; btn.style.color = '#374151' } btn.onmouseleave = () => { btn.style.background = 'rgba(255,255,255,0.95)'; btn.style.color = '#6b7280' } return btn } diff --git a/frontend/src/utils/security.ts b/frontend/src/utils/security.ts index 0a78a941..48557439 100644 --- a/frontend/src/utils/security.ts +++ b/frontend/src/utils/security.ts @@ -3,8 +3,12 @@ */ import DOMPurify from 'dompurify'; - -const PROVIDER_IMAGE_PLACEHOLDER = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw=='; +import type { Config } from 'dompurify'; +import { + domPurifySecurityHooks, + domPurifySecurityOptions, + markdownDomPurifyConfig, +} from '@/utils/markdownDomPurify'; // 配置 DOMPurify 的安全策略 const DOMPurifyConfig = { @@ -14,7 +18,7 @@ const DOMPurifyConfig = { 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'ul', 'ol', 'li', 'blockquote', 'pre', 'code', 'a', 'img', 'table', 'thead', 'tbody', 'tr', 'th', 'td', - 'div', 'span', 'figure', 'figcaption', 'details', 'summary', 'think', + 'div', 'span', 'figure', 'figcaption', 'details', 'summary', 'think', 'button', // Mermaid SVG 支持的标签 'svg', 'g', 'path', 'rect', 'circle', 'ellipse', 'line', 'polygon', 'polyline', 'text', 'tspan', 'defs', 'marker', 'filter', 'use', @@ -27,6 +31,7 @@ const DOMPurifyConfig = { ALLOWED_ATTR: [ 'href', 'title', 'alt', 'src', 'class', 'id', 'style', 'data-protected-src', 'target', 'rel', 'width', 'height', 'open', + 'type', 'aria-label', 'disabled', 'role', 'tabindex', // Mermaid SVG 支持的属性 'd', 'fill', 'stroke', 'stroke-width', 'stroke-linecap', 'stroke-linejoin', 'stroke-dasharray', 'stroke-dashoffset', 'stroke-miterlimit', 'stroke-opacity', @@ -45,54 +50,8 @@ const DOMPurifyConfig = { 'mathvariant', 'encoding', 'aria-hidden' ], USE_PROFILES: { html: true, svg: true, mathMl: true }, - // 允许的协议 - ALLOWED_URI_REGEXP: /^(?:(?:(?:f|ht)tps?|mailto|tel|callto|cid|xmpp):|(?:local|minio|cos|tos|s3|oss|ks3|obs):|[^a-z]|[a-z+.\-]+(?:[^a-z+.\-:]|$))/i, - // 禁止的标签和属性 - FORBID_TAGS: ['script', 'style', 'object', 'embed', 'form', 'input', 'button'], - FORBID_ATTR: ['onerror', 'onload', 'onclick', 'onmouseover', 'onfocus', 'onblur'], - // 其他安全配置 - KEEP_CONTENT: true, - RETURN_DOM: false, - RETURN_DOM_FRAGMENT: false, - RETURN_DOM_IMPORT: false, - SANITIZE_DOM: true, - SANITIZE_NAMED_PROPS: true, - WHOLE_DOCUMENT: false, - // 自定义钩子函数 - HOOKS: { - // 在清理前处理 - beforeSanitizeElements: (currentNode: Element) => { - // 移除所有 script 标签 - if (currentNode.tagName === 'SCRIPT') { - currentNode.remove(); - return null; - } - // 移除所有事件处理器 - const eventAttrs = ['onclick', 'onload', 'onerror', 'onmouseover', 'onfocus', 'onblur']; - eventAttrs.forEach(attr => { - if (currentNode.hasAttribute(attr)) { - currentNode.removeAttribute(attr); - } - }); - }, - // 在清理后处理 - afterSanitizeElements: (currentNode: Element) => { - // 确保所有链接都有 rel="noopener noreferrer" - if (currentNode.tagName === 'A') { - const href = currentNode.getAttribute('href'); - if (href && href.startsWith('http')) { - currentNode.setAttribute('rel', 'noopener noreferrer'); - currentNode.setAttribute('target', '_blank'); - } - } - // 确保所有图片都有 alt 属性 - if (currentNode.tagName === 'IMG') { - if (!currentNode.getAttribute('alt')) { - currentNode.setAttribute('alt', ''); - } - } - } - } + ...domPurifySecurityOptions, + HOOKS: domPurifySecurityHooks, }; /** @@ -115,7 +74,22 @@ export function sanitizeHTML(html: string): string { } } -function protectProviderImageSrcInHTML(html: string): string { +/** Sanitize assistant markdown HTML (code/mermaid toolbars, KaTeX, SVG). */ +export function sanitizeMarkdownHTML(html: string): string { + if (!html || typeof html !== 'string') { + return ''; + } + + try { + const preparedHTML = protectProviderImageSrcInHTML(html); + return DOMPurify.sanitize(preparedHTML, markdownDomPurifyConfig as Config); + } catch (error) { + console.error('Markdown HTML sanitization failed:', error); + return escapeHTML(html); + } +} + +export function protectProviderImageSrcInHTML(html: string): string { if (!html) return html; const decodeProviderURL = (raw: string): string => raw diff --git a/frontend/src/views/dev/MarkdownTestPage.vue b/frontend/src/views/dev/MarkdownTestPage.vue index 89438e6b..a95d155d 100644 --- a/frontend/src/views/dev/MarkdownTestPage.vue +++ b/frontend/src/views/dev/MarkdownTestPage.vue @@ -2,16 +2,25 @@

Markdown Rendering Test

- Dev-only page for visual regression testing of markdown / LaTeX / Mermaid rendering. + Dev-only page for visual regression testing of chat answer markdown + (same typography as botmsg / AgentStreamDisplay / embed). Add new test cases or paste arbitrary markdown in the editor below.

+ +
+

Basic Text Styles

+
+
+
+
+

LaTeX Formulas

-
+
{{ tc.raw }}
-
+
@@ -19,7 +28,7 @@

Code Blocks

-
+
@@ -27,7 +36,7 @@

Tables

-
+
@@ -35,7 +44,7 @@

Lists & Blockquotes

-
+
@@ -43,7 +52,7 @@

Mixed Content

-
+
@@ -51,7 +60,7 @@

Mermaid Diagram

-
+
@@ -69,7 +78,7 @@
-
+
@@ -80,58 +89,84 @@

Custom Input

Paste any markdown here to test rendering.

- +
-
+