diff --git a/internal/router/chat.go b/internal/router/chat.go
index 097af77..810ddb9 100644
--- a/internal/router/chat.go
+++ b/internal/router/chat.go
@@ -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.
diff --git a/internal/web/chat_test.go b/internal/web/chat_test.go
index 9b51521..63094af 100644
--- a/internal/web/chat_test.go
+++ b/internal/web/chat_test.go
@@ -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)
}
}
diff --git a/internal/web/chatrender.go b/internal/web/chatrender.go
new file mode 100644
index 0000000..6832415
--- /dev/null
+++ b/internal/web/chatrender.go
@@ -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 `` + template.HTMLEscapeString(tex) + ``
+}
+
+// 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)))
+ }
+}
diff --git a/internal/web/chatrender_test.go b/internal/web/chatrender_test.go
new file mode 100644
index 0000000..a270539
--- /dev/null
+++ b/internal/web/chatrender_test.go
@@ -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{
+ "bold", "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`,
+ `>x_i^2`, `data-display="1" data-src="$$\sum_{i=1}^n i$$">\sum_{i=1}^n i`,
+ `
`,
+ "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("\n\n[x](javascript:alert(1))\n\n" +
+ "$
$"))
+ for _, bad := range []string{"