Render a finished chat reply as markdown
A reply streams as text as before; once it ends, the server renders it with the model card's rules - raw HTML dropped, dangerous links emptied, images as links - and the page places that. Headings, lists, tables and code blocks read as they were written. Maths outside code is set aside before the markdown, so its underscores and asterisks are not taken for emphasis, and marked for the page to typeset. KaTeX no longer searches the reply for delimiters, which took "$5 and $10" for a formula. A reply that cannot be rendered stays plain text, with the old search. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
9b663eb23c
commit
6b1f8b0827
7 files changed
+236
-7
No files matched your search
@@ -114,6 +114,22 @@ access to it.</p>
|
||||
// trust stays false, which is KaTeX's default and worth stating: it is what
|
||||
// keeps \href, \url and \includegraphics out of a reply.
|
||||
function renderMath(el) {
|
||||
// A rendered reply says where its maths is: the server set each span
|
||||
// aside before the markdown and marked it, so exactly those are typeset.
|
||||
// KaTeX's own search is the fallback for a reply that stayed plain text,
|
||||
// and it takes "$5 and $10" for a formula, which the marks do not.
|
||||
const marked = el.querySelectorAll("span.math");
|
||||
if (marked.length && typeof katex === "object") {
|
||||
for (const m of marked) {
|
||||
try {
|
||||
katex.render(m.textContent, m, {
|
||||
displayMode: m.dataset.display === "1", throwOnError: false, trust: false,
|
||||
});
|
||||
} catch (e) { m.textContent = m.dataset.src || m.textContent; }
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (el.classList.contains("md")) return;
|
||||
if (typeof renderMathInElement !== "function") return;
|
||||
try {
|
||||
renderMathInElement(el, {
|
||||
@@ -134,6 +150,29 @@ access to it.</p>
|
||||
} catch (e) { /* a reply without maths is better than no reply */ }
|
||||
}
|
||||
|
||||
// The finished reply as markdown: headings, lists, tables, code blocks.
|
||||
//
|
||||
// Rendered by the server, under the rules the mirror's model cards use —
|
||||
// raw HTML dropped, dangerous links emptied, images as links — so what
|
||||
// comes back is safe to place. If that fails the reply stays as the plain
|
||||
// text it streamed as, which is still the whole answer.
|
||||
async function renderReply(out, reply) {
|
||||
try {
|
||||
const resp = await fetch("/chat/render", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json", "X-CSRF-Token": csrf },
|
||||
body: JSON.stringify({ content: reply }),
|
||||
});
|
||||
if (!resp.ok) return;
|
||||
out.innerHTML = await resp.text();
|
||||
out.classList.add("md");
|
||||
for (const a of out.querySelectorAll("a[href]")) {
|
||||
a.target = "_blank";
|
||||
a.rel = "noopener noreferrer";
|
||||
}
|
||||
} catch (_) { /* plain text it is */ }
|
||||
}
|
||||
|
||||
// Fetched rather than linked, because the archive is built from a POST body
|
||||
// and there is no URL that names it. The blob is revoked immediately after
|
||||
// the click: it is the whole reply, and leaving it alive keeps it addressable
|
||||
@@ -325,6 +364,7 @@ access to it.</p>
|
||||
}
|
||||
// Once, at the end. Re-rendering every chunk would try to typeset half
|
||||
// an expression on each token and flicker its way through the reply.
|
||||
await renderReply(out, reply);
|
||||
renderMath(out);
|
||||
}
|
||||
else { out.textContent = "(no content came back)"; out.classList.add("failed"); }
|
||||
|
||||
Reference in new issue
Block a user