mirror of
https://github.com/AstrBotDevs/AstrBot
synced 2026-07-16 01:40:15 +08:00
fix(webchat): render standalone HTML replies as code (#6074)
Co-authored-by: Stable Genius <259448942+stablegenius49@users.noreply.github.com> Co-authored-by: Soulter <37870767+Soulter@users.noreply.github.com>
This commit is contained in:
@@ -63,9 +63,10 @@
|
||||
<!-- Text (Markdown) -->
|
||||
<MarkdownRender
|
||||
v-else-if="renderPart.part.type === 'plain' && renderPart.part.text && renderPart.part.text.trim()"
|
||||
:key="`${renderPart.key}-${isDark ? 'dark' : 'light'}`"
|
||||
custom-id="message-list" :custom-html-tags="['ref']" :content="renderPart.part.text" :typewriter="false"
|
||||
class="markdown-content" :is-dark="isDark" />
|
||||
custom-id="message-list" :custom-html-tags="['ref']"
|
||||
:content="normalizeMarkdownContent(renderPart.part.text)" :typewriter="false"
|
||||
class="markdown-content" :is-dark="isDark" :monacoOptions="{ theme: isDark ? 'vs-dark' : 'vs-light' }"
|
||||
:key="`${renderPart.key}-${isDark ? 'dark' : 'light'}`"/>
|
||||
|
||||
<!-- Image -->
|
||||
<div v-else-if="renderPart.part.type === 'image' && renderPart.part.embedded_url" class="embedded-images">
|
||||
@@ -152,6 +153,21 @@ const emitDownloadFile = (file) => {
|
||||
emit('download-file', file);
|
||||
};
|
||||
|
||||
const isMarkdownCodeFence = (text) => /^(```|~~~)/.test(text.trim());
|
||||
|
||||
const looksLikeStandaloneHtml = (text) => {
|
||||
const normalized = text.trim();
|
||||
if (!normalized) return false;
|
||||
if (!/(<!doctype\s+html|<html\b|<head\b|<body\b)/i.test(normalized)) return false;
|
||||
return /(<\/html>|<\/body>|<\/head>|<form\b|<input\b|<button\b)/i.test(normalized);
|
||||
};
|
||||
|
||||
const normalizeMarkdownContent = (text) => {
|
||||
if (typeof text !== 'string') return text;
|
||||
if (isMarkdownCodeFence(text) || !looksLikeStandaloneHtml(text)) return text;
|
||||
return `\`\`\`\`html\n${text}\n\`\`\`\``;
|
||||
};
|
||||
|
||||
const formatDuration = (seconds) => {
|
||||
if (seconds < 1) {
|
||||
return `${Math.round(seconds * 1000)}ms`;
|
||||
|
||||
Reference in New Issue
Block a user