feat: Markdown рендеринг в чате (marked.js)
- Таблицы, заголовки, жирный текст, списки, код — всё рендерится - marked.js через CDN (GFM + breaks) - Стили для таблиц, code blocks, blockquotes - Стриминг: markdown перерендеривается при каждом чанке - Кнопка "Копировать" копирует raw text (без HTML) - История из БД тоже рендерится Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
+45
-5
@@ -61,6 +61,25 @@ body { font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Robo
|
||||
.msg-system { align-self: center; background: var(--system-bg); border: none; font-size: .8rem; color: var(--system-text); box-shadow: none; padding: .4rem .8rem; }
|
||||
.typing { align-self: flex-start; color: var(--text-light); font-style: italic; font-size: .85rem; padding: .4rem 0; }
|
||||
|
||||
/* Markdown rendered content */
|
||||
.msg-ai { white-space: normal; }
|
||||
.msg-ai h1, .msg-ai h2, .msg-ai h3, .msg-ai h4 { margin: .6rem 0 .3rem; font-size: .95rem; font-weight: 700; color: var(--text); }
|
||||
.msg-ai h2 { font-size: 1rem; }
|
||||
.msg-ai h3 { font-size: .95rem; }
|
||||
.msg-ai h4 { font-size: .9rem; color: var(--text-muted); }
|
||||
.msg-ai p { margin: .3rem 0; }
|
||||
.msg-ai ul, .msg-ai ol { margin: .3rem 0 .3rem 1.2rem; }
|
||||
.msg-ai li { margin: .15rem 0; }
|
||||
.msg-ai strong { font-weight: 600; }
|
||||
.msg-ai hr { border: none; border-top: 1px solid var(--border-light); margin: .6rem 0; }
|
||||
.msg-ai table { border-collapse: collapse; margin: .4rem 0; font-size: .85rem; width: 100%; }
|
||||
.msg-ai th, .msg-ai td { border: 1px solid var(--border); padding: .3rem .5rem; text-align: left; }
|
||||
.msg-ai th { background: var(--bg); font-weight: 600; }
|
||||
.msg-ai code { background: #f5f0eb; padding: .1rem .3rem; border-radius: 3px; font-size: .85em; font-family: 'SF Mono', Menlo, monospace; }
|
||||
.msg-ai pre { background: #2c2c2c; color: #f5f0eb; padding: .6rem; border-radius: 6px; overflow-x: auto; margin: .4rem 0; }
|
||||
.msg-ai pre code { background: none; padding: 0; color: inherit; }
|
||||
.msg-ai blockquote { border-left: 3px solid var(--accent); padding-left: .8rem; color: var(--text-muted); margin: .3rem 0; }
|
||||
|
||||
/* Input */
|
||||
#input-area { background: var(--bg-card); border-top: 1px solid var(--border); padding: .8rem 1rem; box-shadow: 0 -2px 8px rgba(0,0,0,.03); }
|
||||
#input-area > div { max-width: 900px; margin: 0 auto; }
|
||||
@@ -120,7 +139,20 @@ body { font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Robo
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
|
||||
<script>
|
||||
// Configure marked for safe rendering
|
||||
if (typeof marked !== 'undefined') {
|
||||
marked.setOptions({ breaks: true, gfm: true });
|
||||
}
|
||||
|
||||
function renderMarkdown(text) {
|
||||
if (typeof marked !== 'undefined') {
|
||||
try { return marked.parse(text); } catch(e) {}
|
||||
}
|
||||
return text.replace(/</g, '<').replace(/\n/g, '<br>');
|
||||
}
|
||||
|
||||
const API = '';
|
||||
let token = localStorage.getItem('token');
|
||||
let pendingFiles = [];
|
||||
@@ -179,14 +211,19 @@ const msgs = document.getElementById('messages');
|
||||
function addMsg(type, text) {
|
||||
const div = document.createElement('div');
|
||||
div.className = 'msg msg-' + type;
|
||||
div.textContent = text;
|
||||
// Store raw text for copy and streaming
|
||||
div._rawText = text;
|
||||
if (type === 'ai') {
|
||||
div.innerHTML = renderMarkdown(text);
|
||||
} else {
|
||||
div.textContent = text;
|
||||
}
|
||||
if (type !== 'system') {
|
||||
const btn = document.createElement('button');
|
||||
btn.className = 'copy-btn';
|
||||
btn.textContent = 'Копировать';
|
||||
btn.onclick = () => {
|
||||
const text = div.firstChild ? div.firstChild.textContent.trim() : div.textContent.replace('Копировать', '').trim();
|
||||
navigator.clipboard.writeText(text);
|
||||
navigator.clipboard.writeText(div._rawText || div.textContent);
|
||||
btn.textContent = 'Скопировано';
|
||||
btn.classList.add('copied');
|
||||
setTimeout(() => { btn.textContent = 'Копировать'; btn.classList.remove('copied'); }, 1500);
|
||||
@@ -327,10 +364,13 @@ async function sendMessage() {
|
||||
if (eventType === 'text') {
|
||||
if (!aiDiv) {
|
||||
aiDiv = addMsg('ai', '');
|
||||
aiDiv._textNode = aiDiv.firstChild; // text node before copy button
|
||||
}
|
||||
aiText += data.text;
|
||||
if (aiDiv._textNode) aiDiv._textNode.textContent = aiText;
|
||||
aiDiv._rawText = aiText;
|
||||
// Re-render markdown (keep copy button)
|
||||
const copyBtn = aiDiv.querySelector('.copy-btn');
|
||||
aiDiv.innerHTML = renderMarkdown(aiText);
|
||||
if (copyBtn) aiDiv.appendChild(copyBtn);
|
||||
msgs.scrollTop = msgs.scrollHeight;
|
||||
} else if (eventType === 'tool') {
|
||||
// Show minimal status, not raw code
|
||||
|
||||
Reference in New Issue
Block a user