Every answer arrives as Markdown and the page prints it unchanged.
Measured on the live service 2026-10-15 (guest chat, cosmic-pretzel-98, qwen3.8-27b Q6_K). The model answered with exactly this text:
# Demo
This is a sentence with **bold**, *italic*, and `inline code`.
- Item one
- Item two
```go
package main
...
```
| Column A | Column B |
| :--- | :--- |
The page shows the hashes, the asterisks, the backticks and the pipes. A table is unreadable. A code block has no box and no horizontal scroll, so long lines wrap into the prose.
Cause: internal/web/assets/app.js writes the answer with element.textContent and app.css keeps the message in white-space: pre-wrap. There is no renderer.
Plan:
- Add assets/markdown.js: a pure parser that turns the answer into a node spec, plus a builder that makes DOM nodes with createElement and textContent only.
- Render the assistant body through it, both while streaming and at the end. Keep the person's own message as typed, never as Markdown.
- Style headings, lists, inline code, fenced code, blockquotes, rules, links and tables for the dark panel. A table and a code block must scroll sideways on a phone instead of widening the page.
Constraints:
- The page sends Content-Security-Policy script-src 'self', so no CDN library and no inline script. The renderer ships as an asset.
- No HTML string may ever be built from model text. An answer that contains or an onerror attribute must stay visible text, and a javascript: link must not become a link. This is the whole reason for the spec plus builder split: the builder sets only href, target, rel, class and start, and only on tags it knows.
- An answer must never fail to appear because the renderer tripped over it.
Acceptance: the demo prompt above renders as a heading, bold and italic text, an inline code chip, two lists, a fenced Go block, a bordered table, a quote and one link, in a signed-out browser at 1280 px and at 390 px, with no horizontal page scroll.
Done and live. hydrabrainserver v0.4.0 on pi-node-001, rebuilt in 12s, image digest sha256:077c66ec97bb4b067c1bafaba3d529dc3ecf8a7923036606f94803c83605cfc0.
What changed
assets/markdown.jsrenders the answer in two halves, and the split is the safety rule:markdownToSpec(text)is pure. It turns the answer into a tree of{t, a, c}nodes and never touches the DOM.buildNode(spec, doc)turns that tree into elements withcreateElementand text nodes, and holds the only tags and attributes a rendered answer may carry:href,target,rel,class,start.No HTML string is ever built from model text. The page sends
script-src 'self', so the renderer is a served asset, not a CDN library. If the renderer throws,renderMarkdownfalls back to plain text, because an answer must still reach the person.Rendered: headings (an answer's
#becomes an h2, a chat answer is not a page), bold, italic, strikethrough, inline code, fenced code with its language, bullet and numbered lists with nesting and a keptstart, blockquotes, rules, links and tables with alignment. A table and a code block scroll inside their own box. The person's own message stays as typed and is never read as Markdown. A streaming answer re-renders once per frame, and an open fence renders as code until it closes.Verified on the live service, signed out, 2026-10-15
#,*, backtick or|.<script>alert(1)</script>,<img src=x onerror=alert(1)>and[click](javascript:alert(1)). All three read as words.document.querySelectorAll(".body script, .body img, .body iframe").lengthis 0, and thejavascript:link did not become a link. Every rendered link carriesrel="noopener noreferrer"andtarget="_blank".scrollWidth == clientWidth. The code box scrolls inside itself (1124 against 333) and a six column table scrolls in its wrapper (685 against 335).Tests
internal/web/testdata/markdown_test.jsholds the renderer checks, including both live answers and the hostile text.go test ./...runs them through node and skips when node is absent, so runnode internal/web/testdata/markdown_test.jsby hand before releasing a renderer change.TestAnswersNeverBecomeMarkupfails the build ifinnerHTML,outerHTML,insertAdjacentHTML,document.write,eval(ornew Functionappears in the assets, or if index.html stops loading markdown.js before app.js.README, runbook and testbook updated. Runbook note for operations: a report of "the answer looks like raw Markdown again" means the fallback fired or the asset failed to load, so check the console and
GET /assets/markdown.jsbefore touching the model or the provider.