feat(chat): integrate marked.js with custom dark code blocks and clean typography
This commit is contained in:
@@ -18,3 +18,43 @@ body {
|
||||
min-height: 100vh;
|
||||
min-height: -webkit-fill-available;
|
||||
}
|
||||
|
||||
/* Стилизация Markdown-контента ответов ассистента в стиле Gemini */
|
||||
.chat-markdown-content h1,
|
||||
.chat-markdown-content h2,
|
||||
.chat-markdown-content h3,
|
||||
.chat-markdown-content h4 {
|
||||
font-weight: 700;
|
||||
color: #0f172a;
|
||||
margin-top: 0.85rem;
|
||||
margin-bottom: 0.35rem;
|
||||
font-size: 0.95rem;
|
||||
}
|
||||
.chat-markdown-content p {
|
||||
margin-bottom: 0.45rem;
|
||||
}
|
||||
.chat-markdown-content p:last-child {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
.chat-markdown-content ul {
|
||||
list-style-type: disc;
|
||||
margin-left: 1.25rem;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
.chat-markdown-content ol {
|
||||
list-style-type: decimal;
|
||||
margin-left: 1.25rem;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
.chat-markdown-content li {
|
||||
margin-bottom: 0.2rem;
|
||||
}
|
||||
.chat-markdown-content code:not([id^="code-block-"]) {
|
||||
padding: 0.15rem 0.35rem;
|
||||
border-radius: 0.375rem;
|
||||
background-color: #f1f5f9;
|
||||
color: #4338ca;
|
||||
font-size: 0.825rem;
|
||||
border: 1px solid #e2e8f0;
|
||||
font-family: monospace;
|
||||
}
|
||||
|
||||
@@ -248,6 +248,7 @@
|
||||
<script src="/static/js/sidebar/prompts_context.js?v=2.6.0"></script>
|
||||
<script src="/static/js/chat/task_widget.js?v=2.6.0"></script>
|
||||
<script src="/static/js/chat/topics.js?v=2.6.0"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
|
||||
<script src="/static/js/chat/scroll.js?v=2.6.0"></script>
|
||||
<script src="/static/js/chat/messages.js?v=2.6.0"></script>
|
||||
<script src="/static/js/chat/input.js?v=2.6.0"></script>
|
||||
|
||||
@@ -1,10 +1,116 @@
|
||||
/**
|
||||
* ===============================================================================
|
||||
* FILE: modules/web_api/static/js/chat/messages.js
|
||||
* ROLE: Рендеринг пузырей сообщений пользователя, ассистента и индикатора загрузки.
|
||||
* ROLE: Рендеринг сообщений на базе marked.js с поддержкой marked v4-v14+.
|
||||
* ===============================================================================
|
||||
*/
|
||||
|
||||
function safeEscapeCode(str) {
|
||||
if (!str) return '';
|
||||
return String(str)
|
||||
.replace(/&/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, ''');
|
||||
}
|
||||
|
||||
function renderMarkdownGemini(rawText) {
|
||||
if (!rawText || typeof rawText !== 'string') return '';
|
||||
|
||||
if (typeof marked === 'undefined') {
|
||||
return safeEscapeCode(rawText).replace(/\n/g, '<br>');
|
||||
}
|
||||
|
||||
const renderer = new marked.Renderer();
|
||||
|
||||
// Переопределяем ТОЛЬКО блоки кода (совместимо со всеми версиями marked)
|
||||
renderer.code = function(arg1, arg2) {
|
||||
// Поддержка и старых версий (code, lang), и новых (token)
|
||||
let code = '';
|
||||
let lang = 'code';
|
||||
|
||||
if (typeof arg1 === 'object' && arg1 !== null) {
|
||||
code = arg1.text || '';
|
||||
lang = arg1.lang || 'code';
|
||||
} else {
|
||||
code = arg1 || '';
|
||||
lang = arg2 || 'code';
|
||||
}
|
||||
|
||||
const language = (lang || 'code').trim();
|
||||
const blockId = 'code-block-' + Math.random().toString(36).substr(2, 9);
|
||||
|
||||
return `
|
||||
<div class="my-3 rounded-xl overflow-hidden border border-slate-700/80 bg-slate-900 shadow-md font-mono text-xs text-left">
|
||||
<div class="flex items-center justify-between px-3.5 py-1.5 bg-slate-800 border-b border-slate-700/70 text-slate-300">
|
||||
<span class="text-[11px] font-semibold tracking-wider text-slate-400 lowercase">${safeEscapeCode(language)}</span>
|
||||
<button type="button" onclick="window.copyCodeBlock('${blockId}', this)"
|
||||
class="flex items-center gap-1.5 px-2 py-1 text-[11px] text-slate-300 hover:text-white bg-slate-700/60 hover:bg-slate-700 rounded-md transition cursor-pointer">
|
||||
<i class="fa-regular fa-copy text-xs"></i>
|
||||
<span>Копировать</span>
|
||||
</button>
|
||||
</div>
|
||||
<div class="p-3.5 overflow-x-auto text-emerald-300 leading-relaxed font-mono whitespace-pre"><code id="${blockId}">${safeEscapeCode(code)}</code></div>
|
||||
</div>
|
||||
`;
|
||||
};
|
||||
|
||||
marked.setOptions({
|
||||
renderer: renderer,
|
||||
gfm: true,
|
||||
breaks: true
|
||||
});
|
||||
|
||||
return marked.parse(rawText);
|
||||
}
|
||||
|
||||
window.copyCodeBlock = function(blockId, btnElement) {
|
||||
const codeEl = document.getElementById(blockId);
|
||||
if (!codeEl) return;
|
||||
const textToCopy = codeEl.innerText;
|
||||
|
||||
function showSuccessFeedback() {
|
||||
const originalHtml = btnElement.innerHTML;
|
||||
btnElement.innerHTML = `<i class="fa-solid fa-check text-emerald-400 text-xs"></i><span class="text-emerald-400 font-bold">Скопировано!</span>`;
|
||||
btnElement.classList.add('bg-slate-800');
|
||||
setTimeout(() => {
|
||||
btnElement.innerHTML = originalHtml;
|
||||
btnElement.classList.remove('bg-slate-800');
|
||||
}, 2000);
|
||||
}
|
||||
|
||||
if (navigator.clipboard && window.isSecureContext) {
|
||||
navigator.clipboard.writeText(textToCopy)
|
||||
.then(showSuccessFeedback)
|
||||
.catch(() => fallbackCopy(textToCopy));
|
||||
} else {
|
||||
fallbackCopy(textToCopy);
|
||||
}
|
||||
|
||||
function fallbackCopy(text) {
|
||||
try {
|
||||
const textArea = document.createElement("textarea");
|
||||
textArea.value = text;
|
||||
textArea.style.position = "fixed";
|
||||
textArea.style.left = "-999999px";
|
||||
textArea.style.top = "-999999px";
|
||||
document.body.appendChild(textArea);
|
||||
textArea.focus();
|
||||
textArea.select();
|
||||
const successful = document.execCommand('copy');
|
||||
document.body.removeChild(textArea);
|
||||
if (successful) {
|
||||
showSuccessFeedback();
|
||||
} else {
|
||||
console.warn("Fallback execCommand copy failed");
|
||||
}
|
||||
} catch (err) {
|
||||
console.error("Не удалось скопировать код в буфер:", err);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
window.appendUserMessage = function(text, filename = null) {
|
||||
const container = document.getElementById("chat-messages-container");
|
||||
if (!container) return;
|
||||
@@ -15,7 +121,7 @@ window.appendUserMessage = function(text, filename = null) {
|
||||
fileBadge = `
|
||||
<div class="inline-flex items-center gap-1.5 px-2.5 py-1 mb-2 bg-indigo-700/80 rounded-lg text-xs font-semibold text-white shadow-xs">
|
||||
<i class="fa-solid fa-paperclip text-xs"></i>
|
||||
<span class="truncate max-w-xs">${escapeHtml(filename)}</span>
|
||||
<span class="truncate max-w-xs">${safeEscapeCode(filename)}</span>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
@@ -24,7 +130,7 @@ window.appendUserMessage = function(text, filename = null) {
|
||||
<div id="${msgId}" class="user-chat-bubble relative flex gap-3 max-w-4xl mx-auto w-full justify-end pt-3 scroll-mt-4">
|
||||
<div class="flex-1 max-w-2xl bg-indigo-600 text-white rounded-2xl rounded-tr-none p-4 shadow-sm">
|
||||
${fileBadge}
|
||||
<div class="text-sm leading-relaxed whitespace-pre-wrap">${escapeHtml(text)}</div>
|
||||
<div class="text-sm leading-relaxed whitespace-pre-wrap">${safeEscapeCode(text)}</div>
|
||||
</div>
|
||||
<div class="w-8 h-8 rounded-lg bg-slate-200 text-slate-600 flex items-center justify-center shrink-0 shadow-sm mt-0.5 font-bold text-sm">
|
||||
<i class="fa-solid fa-user"></i>
|
||||
@@ -73,9 +179,9 @@ window.appendAssistantMessage = function(text, buttons = [], actionPayload = nul
|
||||
buttonsHtml = `
|
||||
<div class="flex flex-wrap gap-2 mt-3.5 pt-2.5 border-t border-slate-100">
|
||||
${allButtons.map(b => `
|
||||
<button onclick="window.sendChatAction('${escapeHtml(b.value || b.action || b.title || '')}')"
|
||||
<button onclick="window.sendChatAction('${safeEscapeCode(b.value || b.action || b.title || '')}')"
|
||||
class="px-3 py-1.5 bg-indigo-50 hover:bg-indigo-100 text-indigo-700 rounded-lg text-xs font-semibold border border-indigo-200 transition">
|
||||
${escapeHtml(b.label || b.title || b.action)}
|
||||
${safeEscapeCode(b.label || b.title || b.action)}
|
||||
</button>
|
||||
`).join('')}
|
||||
</div>
|
||||
@@ -83,7 +189,7 @@ window.appendAssistantMessage = function(text, buttons = [], actionPayload = nul
|
||||
}
|
||||
}
|
||||
|
||||
const bodyContent = isHtmlBody ? text : escapeHtml(text);
|
||||
const bodyContent = isHtmlBody ? text : renderMarkdownGemini(text);
|
||||
|
||||
const html = `
|
||||
<div class="flex gap-3 max-w-4xl mx-auto w-full pt-1">
|
||||
@@ -92,7 +198,7 @@ window.appendAssistantMessage = function(text, buttons = [], actionPayload = nul
|
||||
</div>
|
||||
<div class="flex-1 bg-white border border-slate-200 rounded-2xl rounded-tl-none p-4 shadow-sm min-w-0">
|
||||
<div class="text-[11px] font-bold text-indigo-600 uppercase tracking-wider mb-1.5">ИИ-ассистент SCUD Orion AI</div>
|
||||
<div class="text-sm text-slate-800 leading-relaxed whitespace-pre-wrap">${bodyContent}</div>
|
||||
<div class="text-sm text-slate-800 leading-relaxed chat-markdown-content">${bodyContent}</div>
|
||||
${payloadHtml}
|
||||
${buttonsHtml}
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user