@@ -173,13 +173,10 @@ export default function MessageContent({ content, role, onChoiceSelect }: Messag
|
||||
useEffect(() => {
|
||||
console.log('[MessageContent] 原始内容:', content)
|
||||
|
||||
// 提取 markdown 代码块中的 HTML,并从原始内容中移除代码块标记
|
||||
// 支持 ```html 和不带标识符的 ``` 包裹(后者在内容含 HTML 标签时识别为 HTML 块)
|
||||
// 提取 markdown 代码块中的 HTML(仅识别 ```html 标识符的代码块)
|
||||
let processedContent = content
|
||||
let remainingContent = content
|
||||
let isHtmlCodeBlock = false
|
||||
const htmlTagRegex = /<[a-zA-Z][^>]*>/
|
||||
// 先尝试匹配 ```html 代码块
|
||||
const explicitHtmlRegex = /```html\s*([\s\S]*?)```/gi
|
||||
const htmlCodeBlocks: string[] = []
|
||||
let htmlMatch
|
||||
@@ -195,24 +192,6 @@ export default function MessageContent({ content, role, onChoiceSelect }: Messag
|
||||
isHtmlCodeBlock = true
|
||||
console.log('[MessageContent] 提取到 ```html 代码块:', processedContent)
|
||||
console.log('[MessageContent] 剩余内容:', remainingContent)
|
||||
} else {
|
||||
// 尝试匹配无语言标识的 ``` 代码块,内容含 HTML 标签时也视为 HTML 块
|
||||
const genericCodeRegex = /```\s*\n?([\s\S]*?)```/g
|
||||
const genericBlocks: string[] = []
|
||||
let genericMatch
|
||||
while ((genericMatch = genericCodeRegex.exec(content)) !== null) {
|
||||
const blockContent = genericMatch[1].trim()
|
||||
if (htmlTagRegex.test(blockContent)) {
|
||||
genericBlocks.push(blockContent)
|
||||
}
|
||||
}
|
||||
if (genericBlocks.length > 0) {
|
||||
processedContent = genericBlocks.join('\n')
|
||||
remainingContent = content.replace(/```\s*\n?[\s\S]*?```/g, '').trim()
|
||||
isHtmlCodeBlock = true
|
||||
console.log('[MessageContent] 提取到通用 HTML 代码块:', processedContent)
|
||||
console.log('[MessageContent] 剩余内容:', remainingContent)
|
||||
}
|
||||
}
|
||||
|
||||
// 解析状态面板
|
||||
|
||||
Reference in New Issue
Block a user