feat(chat): add shared markdown rendering pipeline

Extract chat markdown styles, renderer, DOMPurify config, syntax
highlighting, and mermaid support into reusable utilities with tests.
This commit is contained in:
wizardchen
2026-06-16 00:49:12 +08:00
committed by lyingbug
parent efe8b6f41d
commit ee1c3c732c
14 changed files with 1806 additions and 219 deletions
@@ -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 <span class="streaming-image-loading"><span class="streaming-image-loading__skeleton"></span></span>',
)
})
test('renderChatMarkdown preserves citations, math, and sanitized output through one shared pipeline', () => {
const renderer = createChatMarkdownRenderer({
imageRenderer: ({ href, title, text }) =>
`<img src="${href}" alt="${text}" title="${title || ''}" class="markdown-image">`,
isValidImageUrl: (href) => href.startsWith('https://'),
})
const html = renderChatMarkdown(
[
'See <kb doc="sample-product-guide.pdf" chunk_id="chunk-1" kb_id="kb-1"/>',
'',
'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, /<div class="chat-markdown-table"><table>/)
assert.match(html, /<img src="https:\/\/example\.com\/a\.png"/)
assert.doesNotMatch(html, /javascript:alert/)
})
test('resolveCitationChunkId maps context index to retrieval chunk UUID', () => {
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 }) => `<img src="${href}" alt="${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 <kb doc="sample-topic.pdf" chunk_id="${chunkId}" />`,
{
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 }) => `<img src="${href}" alt="${text}">`,
isValidImageUrl: () => true,
})
const html = renderChatMarkdown(
'See <kb doc="Sample FAQ" chunk_id="2" />',
{
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<kb doc="faq.pdf" chunk_id="1" />'
assert.equal(joinCitationTagsToPreviousLine(input), 'Setup is complete. <kb doc="faq.pdf" chunk_id="1" />')
})
test('joinCitationTagsToPreviousLine inlines consecutive citation tags across single newlines', () => {
const tag1 = `<kb doc="${SAMPLE_DOC}" chunk_id="${SAMPLE_CHUNK_A}" />`
const tag2 = `<kb doc="${SAMPLE_DOC}" chunk_id="${SAMPLE_CHUNK_B}" />`
const tag3 = `<kb doc="${SAMPLE_DOC}" chunk_id="${SAMPLE_CHUNK_C}" />`
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 }) => `<img src="${href}" alt="${text}">`,
isValidImageUrl: () => true,
})
const html = renderChatMarkdown(
[
`<kb doc="${SAMPLE_DOC}" chunk_id="${SAMPLE_CHUNK_A}" />`,
`<kb doc="${SAMPLE_DOC}" chunk_id="${SAMPLE_CHUNK_B}" />`,
`<kb doc="${SAMPLE_DOC}" chunk_id="${SAMPLE_CHUNK_C}" />`,
].join('\n'),
{
renderer,
escapeMarkdown: (text) => text,
sanitizeHtml: (html) => html,
},
)
assert.equal((html.match(/citation-kb/g) || []).length, 3)
assert.doesNotMatch(html, /<\/p>\s*<p>\s*<span class="citation citation-kb"/)
})
test('joinCitationTagsToPreviousLine keeps citations on a new line after a list', () => {
const tag = '<kb doc="doc.pdf" chunk_id="1" />'
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 = '<p>Steps:</p><p></p><p><span class="citation citation-kb" data-chunk-id="x">doc</span></p>'
const out = collapseStandaloneCitationParagraphs(html)
assert.match(out, /Steps:.*citation-kb/s)
assert.doesNotMatch(out, /<p><\/p>/)
})
+143
View File
@@ -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 =
'<span class="streaming-image-loading"><span class="streaming-image-loading__skeleton"></span></span>'
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('<table')) return html
return html.replace(
/<table\b[\s\S]*?<\/table>/gi,
(tableHtml) => `<div class="chat-markdown-table">${tableHtml}</div>`,
)
}
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 <kb>/<web>/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
}
@@ -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);
});
+83
View File
@@ -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,
};
+231
View File
@@ -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<HTMLElement>();
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<HTMLElement>('.chat-mermaid-block').forEach((block) => {
const expandBtn = block.querySelector<HTMLButtonElement>('.chat-mermaid-block__expand');
if (!expandBtn) return;
const hasSvg = !!getMermaidDiagramSvg(block);
expandBtn.disabled = !hasSvg;
expandBtn.classList.toggle('is-disabled', !hasSvg);
});
}
const COPY_ICON = '<svg class="chat-code-block__copy-icon" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><rect x="9" y="9" width="13" height="13" rx="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/></svg>';
const EXPAND_ICON = '<svg class="chat-mermaid-block__expand-icon" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><path d="M8 3H5a2 2 0 0 0-2 2v3"/><path d="M21 8V5a2 2 0 0 0-2-2h-3"/><path d="M3 16v3a2 2 0 0 0 2 2h3"/><path d="M16 21h3a2 2 0 0 0 2-2v-3"/></svg>';
const LANG_LABELS: Record<string, string> = {
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, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;');
}
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 `<div class="chat-code-block">
<div class="chat-code-block__header">
<span class="chat-code-block__lang">${displayLang}</span>
<div class="chat-code-block__actions">
<button type="button" class="chat-code-block__copy" aria-label="${copyLabel}" title="${copyLabel}">
${COPY_ICON}<span class="chat-code-block__copy-text">${copyLabel}</span>
</button>
</div>
</div>
<pre class="chat-code-block__pre"><code class="hljs language-${safeLang}">${highlighted}</code></pre>
</div>`;
}
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 `<div class="chat-mermaid-block">
<div class="chat-mermaid-block__header">
<span class="chat-mermaid-block__badge">${label}</span>
<div class="chat-mermaid-block__actions">
<button type="button" class="chat-mermaid-block__expand" aria-label="${expandLabel}" title="${expandLabel}">${EXPAND_ICON}</button>
</div>
</div>
<pre class="chat-mermaid-block__canvas mermaid"${attrs}>${innerHtml}</pre>
</div>`;
}
export function buildMermaidLoadingHtml(): string {
const label = escapeHtml(i18n.global.t('mermaid.diagram'));
return `<div class="chat-mermaid-block chat-mermaid-block--loading">
<div class="chat-mermaid-block__header">
<span class="chat-mermaid-block__badge">${label}</span>
</div>
<div class="streaming-mermaid-loading" aria-hidden="true"><span class="streaming-mermaid-loading__skeleton"></span></div>
</div>`;
}
async function handleCodeCopy(btn: HTMLButtonElement): Promise<void> {
const code = btn.closest('.chat-code-block')?.querySelector('code')?.textContent ?? '';
if (!code) return;
const textEl = btn.querySelector<HTMLElement>('.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<HTMLElement>('.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<HTMLButtonElement>('.chat-code-block__copy');
if (copyBtn) {
event.preventDefault();
event.stopPropagation();
void handleCodeCopy(copyBtn);
return;
}
const expandBtn = target.closest<HTMLButtonElement>('.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<HTMLElement>(
'.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);
}
+187 -55
View File
@@ -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<void> | null = null
let hljsPromise: Promise<typeof import('highlight.js').default> | 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, '&amp;')
@@ -59,12 +168,29 @@ function escapeHtml(text: string) {
.replace(/"/g, '&quot;')
}
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<typeof mermaid.initialize>[0])
mermaid.initialize({
...MERMAID_CONFIG,
themeVariables: resolveMermaidThemeVariables(),
} as Parameters<typeof mermaid.initialize>[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 `<pre id="${id}" data-mermaid="false"><code class="hljs language-${highlightLang}">${highlighted}</code></pre>`
const inner = `<code class="hljs language-${highlightLang}">${highlighted}</code>`
return buildMermaidBlockHtml(inner, `id="${id}" data-mermaid="false"`)
}
return `<pre><code class="hljs language-${highlightLang}">${highlighted}</code></pre>`
return buildCodeBlockHtml(lang || highlightLang, highlighted, highlightLang)
}
}
export const renderMermaidToSvg = async (code: string, id: string): Promise<string | null> => {
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<HTMLElement>('pre[data-mermaid="false"]')
const mermaidElements = rootElement.querySelectorAll<HTMLElement>(
'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<void> {
if (!rootElement) return
attachMarkdownEnhancementListeners(rootElement)
highlightCodeBlocksInContainer(rootElement)
await renderMermaidInContainer(rootElement)
syncMermaidExpandButtons(rootElement)
}
+2 -2
View File
@@ -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
}
+26 -52
View File
@@ -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