From 5e144fdc233f1ab2d0f53acbaca38632044cce63 Mon Sep 17 00:00:00 2001 From: kicer Date: Thu, 3 Sep 2026 14:56:58 +0800 Subject: [PATCH] =?UTF-8?q?v2(md):=20=E4=BC=9A=E8=AF=9D/=E5=8E=86=E5=8F=B2?= =?UTF-8?q?=20AI=20=E4=B8=8E=E7=94=A8=E6=88=B7=E6=B6=88=E6=81=AF=E6=94=AF?= =?UTF-8?q?=E6=8C=81=20markdown=20=E6=B8=B2=E6=9F=93(marked,=E6=A0=B7?= =?UTF-8?q?=E5=BC=8F=E5=AF=B9=E9=BD=90=20v1)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/server/public/css/style.css | 43 +++++++++++++++++++++++++++++++++ app/server/public/index.html | 31 ++++++++++++++++++------ 2 files changed, 67 insertions(+), 7 deletions(-) diff --git a/app/server/public/css/style.css b/app/server/public/css/style.css index 4f167bd..c35423c 100644 --- a/app/server/public/css/style.css +++ b/app/server/public/css/style.css @@ -135,6 +135,49 @@ main { flex: 1; min-height: 0; display: flex; flex-direction: column; } max-width: 100%; white-space: pre-wrap; word-break: break-word; } +/* markdown 排版(v1 同款;仅当 marked 渲染成功时加 .md 类,否则保留 pre-wrap 原文) */ +.bubble-ai.md, .bubble-user.md { white-space: normal; } +.bubble-ai.md > :first-child, .bubble-user.md > :first-child { margin-top: 0; } +.bubble-ai.md > :last-child, .bubble-user.md > :last-child { margin-bottom: 0; } +.bubble-ai.md p, .bubble-user.md p { margin: 6px 0; } +.bubble-ai.md h1, .bubble-ai.md h2, .bubble-ai.md h3, +.bubble-user.md h1, .bubble-user.md h2, .bubble-user.md h3 { + margin: 12px 0 6px; line-height: 1.35; font-weight: 600; +} +.bubble-ai.md h1 { font-size: 1.3em; } +.bubble-ai.md h2 { font-size: 1.18em; } +.bubble-ai.md h3 { font-size: 1.08em; } +.bubble-user.md h1, .bubble-user.md h2, .bubble-user.md h3 { font-size: 1.05em; } +.bubble-ai.md ul, .bubble-ai.md ol, +.bubble-user.md ul, .bubble-user.md ol { padding-left: 1.5em; margin: 6px 0; } +.bubble-ai.md li, .bubble-user.md li { margin: 2px 0; } +.bubble-ai.md pre, .bubble-user.md pre { + background: var(--surface); border: 1px solid var(--border); + padding: 8px 10px; border-radius: 8px; margin: 6px 0; + overflow-x: auto; max-width: 100%; font-size: 13px; line-height: 1.5; +} +.bubble-ai.md code, .bubble-user.md code { + font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; + background: var(--surface); padding: 1px 4px; border-radius: 4px; font-size: 0.92em; +} +.bubble-ai.md pre code, .bubble-user.md pre code { background: none; padding: 0; } +.bubble-ai.md img, .bubble-user.md img { max-width: 100%; border-radius: 8px; } +.bubble-ai.md table, .bubble-user.md table { + border-collapse: collapse; margin: 6px 0; display: block; overflow-x: auto; +} +.bubble-ai.md th, .bubble-ai.md td, +.bubble-user.md th, .bubble-user.md td { + border: 1px solid var(--border); padding: 4px 8px; font-size: 13px; text-align: left; +} +.bubble-ai.md blockquote, .bubble-user.md blockquote { + border-left: 3px solid var(--gray-light); padding-left: 8px; + color: var(--gray); margin: 6px 0; +} +.bubble-ai.md a, .bubble-user.md a { color: var(--active); } +.bubble-ai.md hr, .bubble-user.md hr { + border: 0; border-top: 1px solid var(--border); margin: 10px 0; +} + /* AI 思考过程折叠模块 */ .think { align-self: stretch; max-width: 100%; diff --git a/app/server/public/index.html b/app/server/public/index.html index 7cd7a70..b16276a 100644 --- a/app/server/public/index.html +++ b/app/server/public/index.html @@ -4,6 +4,7 @@ Agent 任务台 + @@ -256,6 +257,14 @@ // ---------- 工具 ---------- function $(id) { return document.getElementById(id); } function esc(s) { const d = document.createElement('div'); d.textContent = s == null ? '' : String(s); return d.innerHTML; } + // ---------- markdown 渲染(同步 index:marked CDN;缺失/异常时回退转义纯文本) ---------- + // 返回 { html, fmt }:fmt=false 表示未做转换,气泡不带 .md 类、保持 pre-wrap 原文排版 + if (typeof marked !== 'undefined') marked.setOptions({ breaks: true, gfm: true }); + function mdHtml(text) { + if (typeof marked === 'undefined') return { html: esc(text), fmt: false }; + try { return { html: marked.parse(text || ''), fmt: true }; } + catch (_) { return { html: esc(text), fmt: false }; } + } function fmtTime(ts) { if (!ts) return ''; const d = new Date(ts); @@ -396,8 +405,9 @@ const p = parseUploadNotice(m.content); const attach = p.files && p.files.length ? '
📎 ' + esc(p.files.join('、')) + '
' : ''; + const r = mdHtml(p.text); return '
' + - '
' + attach + esc(p.text) + '
' + + '
' + attach + r.html + '
' + '
' + fmtTime(m.created_at) + '
' + '
'; } @@ -405,8 +415,9 @@ function aiMsgHtml(m) { const hasText = !!(m.content && m.content.trim()); if (!hasText) return null; // 纯工具调用的应答:不渲染空气泡,工具卡由 appendToolCardsFromAssistant 铺 + const r = mdHtml(m.content); return '
' + - '
' + esc(m.content) + '
' + + '
' + r.html + '
' + actionsHtml(m.created_at, m.content) + '
'; } @@ -531,9 +542,10 @@ function appendUserMsg(content, fileNames) { const attach = fileNames && fileNames.length ? '
📎 ' + esc(fileNames.join('、')) + '
' : ''; + const r = mdHtml(content); const wrap = document.createElement('div'); wrap.className = 'msg me'; - wrap.innerHTML = '
' + attach + esc(content) + '
' + + wrap.innerHTML = '
' + attach + r.html + '
' + '
' + fmtTime(Date.now()) + '
'; $('chat-view').appendChild(wrap); scrollBottom(); @@ -895,15 +907,20 @@ const d = JSON.parse(ev.data); removeTyping(); const hasText = !!(d.content && d.content.trim()); + const r = hasText ? mdHtml(d.content) : null; if (state.streamEl) { - if (hasText) state.streamEl.textContent = d.content; - else state.streamEl.remove(); + if (r) { + state.streamEl.className = 'bubble-ai' + (r.fmt ? ' md' : ''); // 增量期间是纯文本,收尾统一换 md 排版 + state.streamEl.innerHTML = r.html; + } else { + state.streamEl.remove(); + } state.streamEl = null; state.streamText = ''; - } else if (hasText) { + } else if (r) { const wrap = document.createElement('div'); wrap.className = 'msg other'; - wrap.innerHTML = '
' + esc(d.content) + '
'; + wrap.innerHTML = '
' + r.html + '
'; $('chat-view').appendChild(wrap); } if (d.tool_calls) appendToolCardsFromAssistant(d, $('chat-view'));