fix: parse todo list json in code blocks

This commit is contained in:
shiyue
2026-03-30 04:51:25 +08:00
parent 86d044f8a9
commit 34e42b3254

View File

@@ -2176,7 +2176,31 @@
if (!content) return; if (!content) return;
if (typeof content === 'string') { if (typeof content === 'string') {
// 尝试检测是否是 JSON 格式的 todo_list // 检测并提取 JSON 格式的 todo_list(可能在代码块中)
const jsonMatch = content.match(/```json\s*(\{[\s\S]*?"type"\s*:\s*"todo_list"[\s\S]*?\})\s*```/);
if (jsonMatch) {
try {
const parsed = JSON.parse(jsonMatch[1]);
if (parsed.type === 'todo_list') {
const before = content.substring(0, jsonMatch.index);
const after = content.substring(jsonMatch.index + jsonMatch[0].length);
if (before.trim()) {
const textDiv = document.createElement('div');
textDiv.innerHTML = renderMarkdown(before);
bubble.appendChild(textDiv);
}
bubble.appendChild(createTodoListElement(parsed));
if (after.trim()) {
const textDiv = document.createElement('div');
textDiv.innerHTML = renderMarkdown(after);
bubble.appendChild(textDiv);
}
return;
}
} catch {}
}
// 尝试直接解析 JSON
const trimmed = content.trim(); const trimmed = content.trim();
if (trimmed.startsWith('{') && trimmed.includes('"type"') && trimmed.includes('"todo_list"')) { if (trimmed.startsWith('{') && trimmed.includes('"type"') && trimmed.includes('"todo_list"')) {
try { try {