Commit 0fbdc3be by 李伟康

feat(markdown): 添加对流式输出中嵌套details标签的处理

- 实现了在流式输出场景下对details标签内json代码块的截断处理
- 新增了对未完成details标签的检测和内容截取逻辑
- 优化了details标签内JSON内容的过滤机制
- 添加了console.log用于调试latex内容
- 改进了嵌套details标签的正则匹配和清理算法
1 parent 967feecc
...@@ -94,7 +94,7 @@ export function Markdown(props: MarkdownProps) { ...@@ -94,7 +94,7 @@ export function Markdown(props: MarkdownProps) {
}, [processedContent]) }, [processedContent])
const latexContent = contentObj.mainContent const latexContent = contentObj.mainContent
console.log(latexContent)
// 强行过滤所有代码块内容 // 强行过滤所有代码块内容
const filteredContent = useMemo(() => { const filteredContent = useMemo(() => {
if (!latexContent) return '' if (!latexContent) return ''
...@@ -108,6 +108,50 @@ export function Markdown(props: MarkdownProps) { ...@@ -108,6 +108,50 @@ export function Markdown(props: MarkdownProps) {
filtered = filtered.replace(/<details[^>]*data-think[^>]*>[\s\S]*/gi, '') filtered = filtered.replace(/<details[^>]*data-think[^>]*>[\s\S]*/gi, '')
} }
// 处理 <details> 内的 ```json:一旦出现就截断后面所有内容(适配流式输出)
// 查找每个 <details> 标签内的第一个 ```json 位置
const detailsRegex = /<details[^>]*data-think[^>]*>/gi
let result = filtered
while (true) {
const match = detailsRegex.exec(result)
if (!match) break
const detailsStartIndex = match.index
const detailsTagEnd = detailsStartIndex + match[0].length
// 在这个 details 标签后查找 </details>
const detailsEndIndex = result.indexOf('</details>', detailsTagEnd)
if (detailsEndIndex === -1) {
// 如果没有找到结束标签,说明是流式输出中还未完成的部分
// 检查从 details 开始到当前末尾是否有 ```json
const contentAfterDetails = result.substring(detailsTagEnd)
const jsonIndex = contentAfterDetails.indexOf('```json')
if (jsonIndex !== -1) {
// 找到 ```json,截断后面的所有内容
result = result.substring(0, detailsTagEnd + jsonIndex)
}
break
}
// 提取 details 内部的内容
const detailsContent = result.substring(detailsTagEnd, detailsEndIndex)
const jsonIndex = detailsContent.indexOf('```json')
if (jsonIndex !== -1) {
// 找到 ```json,只保留前面的内容
const beforeJson = detailsContent.substring(0, jsonIndex)
result = result.substring(0, detailsTagEnd) + beforeJson + result.substring(detailsEndIndex)
// 重置搜索,因为内容长度改变了
detailsRegex.lastIndex = 0
} else {
// 没找到,继续下一个
detailsRegex.lastIndex = detailsEndIndex
}
}
filtered = result
// 移除所有 ```json 和 ``` 标记,无论它们是否成对出现 // 移除所有 ```json 和 ``` 标记,无论它们是否成对出现
filtered = filtered.replace(/```json\s*/gi, '') filtered = filtered.replace(/```json\s*/gi, '')
filtered = filtered.replace(/```\s*/gi, '') filtered = filtered.replace(/```\s*/gi, '')
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!