🐛 D107 · 修复:消息内容markdown渲染+粗体/代码/换行格式支持

This commit is contained in:
root 2026-05-20 22:56:46 +08:00
parent 10a6bb0ee2
commit 6ce011086c

View File

@ -252,6 +252,8 @@ table.tool-table td:last-child{text-align:right}
.msg-bb code{background:rgba(255,255,255,.06);padding:2px 6px;border-radius:4px;font-size:14px}
.msg-bb pre{background:rgba(0,0,0,.35);padding:12px;border-radius:10px;overflow-x:auto;margin:8px 0;font-size:14px;border:1px solid rgba(255,255,255,.04)}
/* 消息内容 markdown 渲染 */
.msg-bb strong,.msg-bb b{font-weight:600;color:var(--light)}.msg-bb em,.msg-bb i{font-style:italic;color:var(--text-soft)}.msg-bb ul,.msg-bb ol{margin:8px 0 8px 20px;padding-left:10px}.msg-bb li{margin:4px 0}.msg-bb h1,.msg-bb h2,.msg-bb h3,.msg-bb h4{margin:12px 0 8px;font-weight:600;color:var(--light)}.msg-bb p{margin-bottom:10px;line-height:1.8}.msg-bb blockquote{border-left:3px solid var(--glass-edge);padding-left:12px;margin:8px 0;color:var(--text-soft)}
.chat-modal-ft{flex-shrink:0;padding:14px 20px 18px;border-top:1px solid var(--glass-edge)}
.chat-inp-row{display:flex;gap:10px}
.chat-inp-row input{flex:1;padding:13px 16px;background:rgba(255,255,255,.035);border:1px solid var(--glass-edge);border-radius:14px;color:var(--text);font-size:15px;outline:none;font-family:inherit}
@ -731,7 +733,7 @@ async function modalSend() {
const data = JSON.parse(t.slice(6));
if (data.token) {
fullText += data.token;
bubble.textContent = fullText;
bubble.innerHTML = markdownToHtml(fullText);
body.scrollTop = body.scrollHeight;
}
if (data.type === 'tool-call') {
@ -772,7 +774,7 @@ function appendMsg(role, text) {
div.className = 'msg ' + role;
const n = role === 'user' ? '你' : (currentPersona === 'zhuyuan' ? '铸渊' : '霜砚');
const icon = role === 'user' ? '☀' : (currentPersona === 'zhuyuan' ? '⚔' : '❄');
div.innerHTML = '<div class="msg-av">'+icon+'</div><div class="msg-con"><div class="msg-nm">'+n+'</div><div class="msg-bb">'+escapeHtml(text)+'</div></div>';
div.innerHTML = '<div class="msg-av">'+icon+'</div><div class="msg-con"><div class="msg-nm">'+n+'</div><div class="msg-bb">' + markdownToHtml(text) + '</div></div>';
body.appendChild(div);
body.scrollTop = body.scrollHeight;
}
@ -789,6 +791,16 @@ function escapeHtml(t) {
return t.replace(/&/g,'&amp;').replace(/</g,'&lt;').replace(/>/g,'&gt;');
}
function markdownToHtml(t) {
let h = escapeHtml(t);
h = h.replace(/\*\*(.+?)\*\*/g, '<strong>$1</strong>');
h = h.replace(/```([\s\S]*?)```/g, '<pre>$1</pre>');
h = h.replace(/`([^`]+)`/g, '<code>$1</code>');
h = h.replace(/
/g, '<br>');
return h;
}
// ===== 登录 =====
async function handleLogin() {
const u = document.getElementById('login-user').value.trim();