Render assistant Markdown and LaTeX as sanitized HTML with native MathML

Server-side rendering keeps the browser free of any Markdown/MathJax toolchain (single-binary). New src/render.rs: pulldown-cmark (tables/strikethrough/math) + pulldown-latex -> MathML Core. Safety: raw HTML events are escaped to text (pulldown-cmark 0.13 has no ENABLE_HTML opt-out) and link/image URLs pass a scheme allowlist (javascript:/data:/vbscript: stripped, control chars removed). content_html is added to get_messages (assistant only) and to the agent Final SSE event; the GUI injects it via innerHTML for assistant bubbles (history + live) while user text stays textContent. CSS styles Markdown and native MathML; block math must not override display (UA block math layout) or it collapses to vertical stacks. 10 render unit tests incl. XSS; 136 tests green, clippy/fmt clean; verified live + visually.
This commit is contained in:
adm committed 2026-11-25 20:58:20 +01:00
1 parent d1b56d2c23
commit 7dd66ea138
7 files changed
+386 -13

No files matched your search

+20 -6
View File
@@ -1,7 +1,8 @@
// strata-ctl web GUI — vanilla ES module, no build step, no dependencies.
// Talks to the session-aware API (/api/sessions...); the server owns context.
// Model output is always inserted via textContent (never innerHTML) to avoid
// injection. Mobile-first: the session list is an off-canvas drawer on phones.
// User text is inserted via textContent; assistant Markdown/LaTeX arrives as
// server-sanitized HTML (content_html) injected via innerHTML. Mobile-first:
// the session list is an off-canvas drawer on phones.
const $ = (sel) => document.querySelector(sel);
@@ -59,7 +60,7 @@ function el(tag, cls, text) {
return n;
}
function messageNode(role, content, reasoning, usage, ts) {
function messageNode(role, content, reasoning, usage, ts, html) {
const wrap = el("div", `msg ${role}`);
const roleNode = el("div", "role", role === "user" ? "Tu" : "Assistente");
if (ts) roleNode.appendChild(el("span", "when", " · " + stampLabel(ts)));
@@ -71,7 +72,15 @@ function messageNode(role, content, reasoning, usage, ts) {
det.appendChild(el("div", "reasoning", reasoning));
bubble.appendChild(det);
}
bubble.appendChild(el("div", "content", content || ""));
const contentNode = el("div", "content");
if (html != null) {
// Server-sanitized Markdown/LaTeX (assistant only): safe to inject.
contentNode.classList.add("rendered");
contentNode.innerHTML = html;
} else {
contentNode.textContent = content || "";
}
bubble.appendChild(contentNode);
if (usage) bubble.appendChild(el("div", "usage", usage));
wrap.appendChild(bubble);
return wrap;
@@ -179,7 +188,7 @@ function renderTranscript(messages) {
const usage = m.completion_tokens
? usageText({ prompt_tokens: m.prompt_tokens, completion_tokens: m.completion_tokens })
: null;
t.appendChild(messageNode("assistant", m.content, m.reasoning_content, usage, m.created_at));
t.appendChild(messageNode("assistant", m.content, m.reasoning_content, usage, m.created_at, m.content_html));
}
}
}
@@ -420,9 +429,14 @@ async function agentTurn(content, t, attachments) {
if (!L.text && m.content) { L.text = m.content; L.contentNode.textContent = m.content; }
if (!L.reasoning && m.reasoning_content) {
L.reasoning = m.reasoning_content;
L.reasoningNode.textContent = m.reasoning;
L.reasoningNode.textContent = L.reasoning;
L.det.hidden = false;
}
// Swap the streamed plain text for server-rendered Markdown/LaTeX.
if (ev.content_html != null) {
L.contentNode.classList.add("rendered");
L.contentNode.innerHTML = ev.content_html;
}
L.det.open = false;
if (ev.response.usage) L.bubble.appendChild(el("div", "usage", usageText(ev.response.usage)));
live = null;