From 34e42b3254462984602142642e12dd342a84e3a4 Mon Sep 17 00:00:00 2001 From: shiyue Date: Mon, 30 Mar 2026 04:51:25 +0800 Subject: [PATCH] fix: parse todo list json in code blocks --- public/app.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/public/app.js b/public/app.js index c7aff10..111ebbe 100644 --- a/public/app.js +++ b/public/app.js @@ -2176,7 +2176,31 @@ if (!content) return; 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(); if (trimmed.startsWith('{') && trimmed.includes('"type"') && trimmed.includes('"todo_list"')) { try {