Spaces:
Runtime error
Runtime error
marked.js for mardown rendering
Browse files8 unmodified lines9101112131415291 unmodified lines307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375234 unmodified lines610611612549613614615616140 unmodified lines7577587596967607617627637 unmodified lines77177277371077477577677730 unmodified lines8088098107478118127497507517527537547557567577588138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628 unmodified lines <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.css">
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/katex.min.js"></script>
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.11/dist/contrib/auto-render.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<style>
:root {291 unmodified lines background-color: var(--msg-ai);
}
.ai .message-content h1,
.ai .message-content h2,
.ai .message-content h3 {
margin: 16px 0 8px 0;
line-height: 1.3;
}
.ai .message-content h1 { font-size: 1.4em; }
.ai .message-content h2 { font-size: 1.2em; }
.ai .message-content h3 { font-size: 1.05em; }
.ai .message-content p {
margin: 8px 0;
}
.ai .message-content ul,
.ai .message-content ol {
margin: 8px 0;
padding-left: 24px;
}
.ai .message-content li {
margin: 4px 0;
}
.ai .message-content code {
background-color: #1a1a1a;
padding: 2px 6px;
border-radius: 4px;
font-size: 0.9em;
font-family: 'Courier New', monospace;
}
.ai .message-content pre {
background-color: #111111;
padding: 12px 16px;
border-radius: 8px;
overflow-x: auto;
margin: 10px 0;
}
.ai .message-content pre code {
background: none;
padding: 0;
}
.ai .message-content hr {
border: none;
border-top: 1px solid var(--border-color);
margin: 12px 0;
}
.ai .message-content strong {
font-weight: 600;
}
.ai .message-content blockquote {
border-left: 3px solid var(--brand-orange);
padding-left: 12px;
margin: 8px 0;
color: var(--text-secondary);
}
.ai-avatar {
width: 30px;
height: 30px;234 unmodified lines data.messages.forEach(msg => {
const sender = msg.role === 'user' ? 'user' : 'ai';
const el = appendMessage(sender, msg.content);
if (sender === 'ai') renderMath(el);
if (sender === 'ai') renderContent(el);
});
});
}140 unmodified lines reader.read().then(({done, value}) => {
if (done) {
contentElement.classList.remove('cursor');
renderMath(contentElement);
renderContent(contentElement);
isSending = false;
return;
}7 unmodified lines const payload = line.substring(6);
if (payload === '[DONE]') {
contentElement.classList.remove('cursor');
renderMath(contentElement);
renderContent(contentElement);
isSending = false;
return;
}30 unmodified lines }
/* =========================================
Math Rendering (KaTeX)
Markdown + Math Rendering
========================================= */
function renderMath(element) {
if (typeof renderMathInElement === 'undefined') return;
renderMathInElement(element, {
delimiters: [
{left: '$$', right: '$$', display: true},
{left: '$', right: '$', display: false},
{left: '\\(', right: '\\)', display: false},
{left: '\\[', right: '\\]', display: true}
],
throwOnError: false
function renderContent(element) {
const raw = element.textContent;
// Extract math blocks into placeholders so marked does not mangle them
const mathBlocks = [];
let protected_text = raw;
// Order matters: extract display math ($$) before inline ($)
protected_text = protected_text.replace(/\$\$([\s\S]*?)\$\$/g, (match) => {
mathBlocks.push(match);
return '%%MATH' + (mathBlocks.length - 1) + '%%';
});
protected_text = protected_text.replace(/\\\[([\s\S]*?)\\\]/g, (match) => {
mathBlocks.push(match);
return '%%MATH' + (mathBlocks.length - 1) + '%%';
});
protected_text = protected_text.replace(/\\\(([\s\S]*?)\\\)/g, (match) => {
mathBlocks.push(match);
return '%%MATH' + (mathBlocks.length - 1) + '%%';
});
protected_text = protected_text.replace(/\$([^$\n]+?)\$/g, (match) => {
mathBlocks.push(match);
return '%%MATH' + (mathBlocks.length - 1) + '%%';
});
// Parse markdown
let html = marked.parse(protected_text);
// Restore math blocks
mathBlocks.forEach((block, i) => {
html = html.replace('%%MATH' + i + '%%', block);
});
element.innerHTML = html;
// Run KaTeX on the rendered HTML
if (typeof renderMathInElement !== 'undefined') {
renderMathInElement(element, {
delimiters: [
{left: '$$', right: '$$', display: true},
{left: '$', right: '$', display: false},
{left: '\\(', right: '\\)', display: false},
{left: '\\[', right: '\\]', display: true}
],
throwOnError: false
});
}
}
/* =========================================stem.html+114-14