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:
Ilker ManapandClaude Opus 5.5 committed 2026-09-28 22:44:18 +02:00
1 parent 9b663eb23c
commit 6b1f8b0827
7 files changed
+236 -7

No files matched your search

+5
View File
@@ -51,6 +51,11 @@ func (r *Router) registerChat(mux *http.ServeMux, pages *web.AccountPages) {
func(w http.ResponseWriter, req *http.Request, _ store.User) {
web.CodeArchive(r.cfg.MaxBodyBytes)(w, req)
}))
// A finished reply, rendered: markdown the model wrote, made safe.
mux.HandleFunc("POST /chat/render", pages.Guard(
func(w http.ResponseWriter, req *http.Request, _ store.User) {
web.ReplyRenderer(r.cfg.MaxBodyBytes)(w, req)
}))
}
// chatSend dispatches one turn on behalf of a signed-in person.
+11 -4
View File
@@ -60,8 +60,10 @@ func TestMathIsRenderedWithoutTrustingTheModel(t *testing.T) {
if !strings.Contains(page, "out.textContent = reply") {
t.Error("the reply is no longer written with textContent")
}
if strings.Contains(page, "innerHTML") {
t.Error("the chat page assigns innerHTML somewhere")
// Only the server's rendering of a finished reply is placed as HTML; see
// TestTheTruncationNoticeIsNotPutInsideTheReply.
if strings.Count(page, "innerHTML") != 1 {
t.Error("the chat page assigns innerHTML beyond the rendered reply")
}
}
@@ -174,7 +176,12 @@ func TestTheTruncationNoticeIsNotPutInsideTheReply(t *testing.T) {
if !strings.Contains(page, "out.after(note)") {
t.Error("the notice is not appended as a sibling of the reply")
}
if strings.Contains(page, "out.innerHTML") {
t.Error("the reply element is being written as HTML")
// The one exception is the finished reply as /chat/render returns it —
// markdown the server has already made safe — and nothing else may be
// written into the reply as HTML.
if n := strings.Count(page, "innerHTML ="); n != 1 ||
!strings.Contains(page, "out.innerHTML = await resp.text();") ||
!strings.Contains(page, `fetch("/chat/render"`) {
t.Errorf("the reply is written as HTML other than from /chat/render (%d assignments)", n)
}
}
+106
View File
@@ -0,0 +1,106 @@
package web
import (
"encoding/json"
"fmt"
"html/template"
"io"
"net/http"
"regexp"
"strings"
)
// mathSpan finds what KaTeX will typeset, in the order its delimiters are
// tried on the page: display before inline, and a single $ last.
//
// A single-$ span must not start or end with a space. That is what keeps
// "$5 and $10" a sentence about prices rather than a formula, as KaTeX's own
// rule does.
var mathSpan = regexp.MustCompile(`(?s)\$\$.+?\$\$|\\\[.+?\\\]|\\\(.+?\\\)|\$[^\s$](?:[^$\n]*?[^\s$])?\$`)
// codeSpan finds fenced code blocks and inline code, which maths is not
// looked for in. A fence left open — a reply cut off mid-block — is not
// matched, and is read as text.
var codeSpan = regexp.MustCompile("(?s)(?:^|\n)[ \t]*(```|~~~).*?\n[ \t]*(?:```|~~~)[^\n]*|`[^`\n]+`")
// renderReply turns a model's reply into HTML for the chat page.
//
// Markdown with the model card's rules — raw HTML dropped, dangerous links
// emptied, images shown as links — with the maths set aside first. Read as
// markdown, "a_1 * b_2" loses its underscores to emphasis before KaTeX ever
// sees it; set aside, it reaches the page as the text the model wrote, for
// KaTeX to typeset there.
func renderReply(reply string) template.HTML {
var spans []string
protect := func(text string) string {
return mathSpan.ReplaceAllStringFunc(text, func(m string) string {
spans = append(spans, m)
// Letters and digits only, so no markdown rule touches it.
return fmt.Sprintf("llmgwmath%dx", len(spans)-1)
})
}
// Only outside code: "echo $HOME $PATH" in a code block is shell, and a
// formula quoted in backticks is being shown, not typeset.
var b strings.Builder
last := 0
for _, c := range codeSpan.FindAllStringIndex(reply, -1) {
b.WriteString(protect(reply[last:c[0]]))
b.WriteString(reply[c[0]:c[1]])
last = c[1]
}
b.WriteString(protect(reply[last:]))
protected := b.String()
out := string(safeMarkdown(protected, ""))
for i, m := range spans {
out = strings.Replace(out, fmt.Sprintf("llmgwmath%dx", i), mathElement(m), 1)
}
return template.HTML(out) //nolint:gosec // goldmark's safe output, maths escaped
}
// mathElement marks one span of maths for the page to typeset, its
// delimiters removed and the TeX escaped as text.
//
// Marked here rather than found again in the page: KaTeX's own search, run
// over the reply, took "$5 and $10" for a formula. Deciding once, on the
// server, with the stricter rule above, means the page typesets exactly what
// was set aside and nothing else. Inside a code block the mark is shown as
// the text it replaced, since that is all a code block holds.
func mathElement(m string) string {
tex, display := m, false
switch {
case strings.HasPrefix(m, "$$"):
tex, display = m[2:len(m)-2], true
case strings.HasPrefix(m, `\[`):
tex, display = m[2:len(m)-2], true
case strings.HasPrefix(m, `\(`):
tex = m[2 : len(m)-2]
default:
tex = m[1 : len(m)-1]
}
mode := "0"
if display {
mode = "1"
}
return `<span class="math" data-display="` + mode + `" data-src="` +
template.HTMLEscapeString(m) + `">` + template.HTMLEscapeString(tex) + `</span>`
}
// ReplyRenderer answers the chat page's request to render a finished reply.
//
// On the server rather than in the page: the markdown rules are then the
// model card's, already written and tested, and the page carries no parser
// of its own.
func ReplyRenderer(maxBody int64) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var in struct {
Content string `json:"content"`
}
body, err := io.ReadAll(io.LimitReader(r.Body, maxBody))
if err != nil || json.Unmarshal(body, &in) != nil {
http.Error(w, "send the reply as {\"content\": ...}", http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
_, _ = io.WriteString(w, string(renderReply(in.Content)))
}
}
+49
View File
@@ -0,0 +1,49 @@
package web
import (
"strings"
"testing"
)
func TestAReplyIsMarkdownWithItsMathsLeftForKaTeX(t *testing.T) {
got := string(renderReply("Here is **bold** and `code`.\n\n" +
"The sum $a_1 * b_2$ and \\(x_i^2\\), then\n\n$$\\sum_{i=1}^n i$$\n\n" +
"```go\nfmt.Println(\"$5\")\n```\n\nIt costs $5 and $10."))
for _, want := range []string{
"<strong>bold</strong>", "<code>code</code>",
// Untouched by emphasis, and marked for KaTeX with its delimiters
// taken off.
`data-display="0" data-src="$a_1 * b_2$">a_1 * b_2</span>`,
`>x_i^2</span>`, `data-display="1" data-src="$$\sum_{i=1}^n i$$">\sum_{i=1}^n i</span>`,
`<pre><code class="language-go">`,
"It costs $5 and $10.",
} {
if !strings.Contains(got, want) {
t.Errorf("missing %q in %s", want, got)
}
}
if strings.Contains(got, "llmgwmath") {
t.Errorf("a placeholder was left behind: %s", got)
}
}
func TestAReplyCannotRunAnything(t *testing.T) {
got := string(renderReply("<script>alert(1)</script>\n\n[x](javascript:alert(1))\n\n" +
"$<img src=x onerror=alert(1)>$"))
for _, bad := range []string{"<script", "javascript:", "<img"} {
if strings.Contains(got, bad) {
t.Errorf("rendered %q: %s", bad, got)
}
}
}
func TestMathsInCodeIsLeftAsCode(t *testing.T) {
got := string(renderReply("Use `$x$` literally.\n\n```sh\necho $HOME $PATH\n```\n\nBut $y_1$ is maths."))
if strings.Count(got, `class="math"`) != 1 || !strings.Contains(got, `data-src="$y_1$"`) {
t.Errorf("only the maths outside code should be marked: %s", got)
}
if !strings.Contains(got, "<code>$x$</code>") || !strings.Contains(got, "echo $HOME $PATH") {
t.Errorf("code should keep its dollars: %s", got)
}
}
+9 -3
View File
@@ -26,18 +26,24 @@ import (
// - relative links, to the repository's own LICENSE and files, are made to
// point at the repository rather than at this page.
func renderCard(card, repoBase string) template.HTML {
if strings.TrimSpace(card) == "" {
return safeMarkdown(card, repoBase)
}
// safeMarkdown renders a stranger's markdown under the rules above. base, when
// set, is what relative links resolve against.
func safeMarkdown(src, base string) template.HTML {
if strings.TrimSpace(src) == "" {
return ""
}
md := goldmark.New(
goldmark.WithExtensions(extension.GFM),
goldmark.WithParserOptions(parser.WithASTTransformers(
util.Prioritized(cardLinks{base: repoBase}, 100))),
util.Prioritized(cardLinks{base: base}, 100))),
goldmark.WithRendererOptions(
renderer.WithNodeRenderers(util.Prioritized(imageAsLink{}, 100))),
)
var out bytes.Buffer
if err := md.Convert([]byte(card), &out); err != nil {
if err := md.Convert([]byte(src), &out); err != nil {
return ""
}
// Safe by construction: goldmark in its default, safe mode, with the two
+16
View File
@@ -506,3 +506,19 @@ table.repo-files form.inline input[name=version] { width: 6.5rem; }
table.repo-files form.inline button { padding: .3rem .7rem; font-size: .8rem; }
table.repo-files td:last-child { width: 1%; white-space: nowrap; }
table.repo-files td:last-child .opt { display: block; margin-bottom: .2rem; }
/* A finished reply, rendered from markdown. Wrapping goes back to normal:
pre-wrap, right for text as it streams, doubles every gap between blocks. */
.bubble.md { white-space: normal; }
.bubble.md > :first-child { margin-top: 0; }
.bubble.md > :last-child { margin-bottom: 0; }
.bubble.md p, .bubble.md ul, .bubble.md ol, .bubble.md pre, .bubble.md table, .bubble.md blockquote { margin: .5rem 0; }
.bubble.md ul, .bubble.md ol { padding-left: 1.4rem; }
.bubble.md h1, .bubble.md h2, .bubble.md h3, .bubble.md h4 { margin: .8rem 0 .35rem; font-size: 1rem; }
.bubble.md h1 { font-size: 1.15rem; }
.bubble.md code { font-size: .85em; background: var(--card); padding: .05rem .3rem; border-radius: 4px; }
.bubble.md pre { white-space: pre; overflow-x: auto; background: var(--card); border: 1px solid var(--line);
border-radius: 6px; padding: .6rem .75rem; }
.bubble.md pre code { background: none; padding: 0; font-size: .82rem; }
.bubble.md table { width: auto; max-width: 100%; display: block; overflow-x: auto; font-size: .85rem; }
.bubble.md th, .bubble.md td { padding: .25rem .6rem; border-bottom: 1px solid var(--line); }
.bubble.md blockquote { border-left: 3px solid var(--line); padding-left: .75rem; color: var(--muted); }
+40
View File
@@ -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"); }