Repository navigation
Description
Render ```svg fences as inline SVG
Summary
ompweb renders ```mermaid fences as diagrams but shows every other fenced language as a plain code block. ```svg therefore stays source text with line numbers and a copy button, even though omp/omp CLI sessions emit exactly those fences expecting them to display inline.
Environment
@kahme247/ompweb0.6.0 (also checked 0.5.2)- Node 24 / Next 16.3.4
- Chrome on Windows 11 (relay and plain tabs behave the same)
Steps to reproduce
-
Start
ompweb. -
Send an omp session a message that produces this reply:
```svg <svg viewBox="0 0 420 90" font-family="system-ui, sans-serif"> <text x="0" y="18" font-size="13" fill="var(--muted)">svg block renders inline</text> <rect x="0" y="30" width="400" height="46" rx="6" fill="none" stroke="var(--border)"/> </svg> ```
-
Open the session in ompweb.
Actual: the block renders as .markdown-code-block — line numbers, an svg language chip, only a Copy action.
Expected: the same inline rendering the TUI gives, i.e. a picture.
A ```mermaid fence in the same conversation at least offers a "Preview Mermaid diagram" action, which confirms the fence is parsed correctly and the language chip is svg, not something else.
Root cause
components are not shipped in the npm package, so this is read from the built chunk .next/static/chunks/app/page-<hash>.js. The markdown code renderer dispatches on the fence language and special-cases only mermaid:
let s = className?.replace("language-", "").toLowerCase() ?? "", o = String(children);
return className?.includes("language-") || o.includes("\n")
? "mermaid" === s
? <MermaidBlock code={...} isStreaming={...} defaultPreview={...} />
: <CodeBlock code={...} lang={s} isStreaming={...} />
: <code ...>{children}</code>;svg has no branch, so it can never reach the renderer no matter how the fence is written.
Second issue: theme variables do not exist in the web UI
omp's SVG conventions use var(--fg), var(--muted), var(--surface) and var(--c1)…var(--c6). The ompweb theme defines --text, --text-muted, --bg-panel, --accent and --status-* — but none of the former. Inline-rendering an omp-style <svg> therefore resolves those fills to the initial value and the drawing goes black on a black background, i.e. invisible.
Proposed change
Add an svg branch next to the mermaid one:
"svg" === s
? <SvgBlock code={o.replace(/\n$/, "")} isStreaming={isStreaming} />
: <CodeBlock ... />with SvgBlock doing the following (reference implementation attached below):
-
Parse with
DOMParser.parseFromString(code, "image/svg+xml"). -
If there is a
parsererroror the root element is notsvg, fall back to<CodeBlock lang="svg">. This also covers streaming, where the fence is still half-written. -
Strip
script,foreignObject,on*attributes, andjavascript:/data:text/htmlvalues inhref/xlink:href. -
Add
max-width:100%;height:autoand defaultfill="currentColor", so drawings without an explicitfillstay visible in dark themes. -
Render inside a wrapper div whose style defines the missing variables as aliases:
{ "--fg": "var(--text)", "--muted": "var(--text-muted)", "--surface": "var(--bg-panel)", "--c1": "var(--accent)", "--c2": "var(--status-success)", "--c3": "var(--status-warning)", "--c4": "var(--status-error)", "--c5": "var(--status-modified)", "--c6": "var(--status-renamed)", }
--c1…--c6is a deliberate approximation; if you prefer real series colours, define them in the theme and drop those five entries.
Two design questions worth your call:
- Whether the alias map belongs in the theme (
--fg: var(--text)at:root) rather than in this component. A theme-level alias would also fix```mermaidthemes and any future HTML generated from omp output. - Whether
<style>inside an inline SVG should be stripped likescript. Inline SVG CSS applies to the whole document. I left it in place for fidelity (AI-generated diagrams often rely onclass=+<style>), but it is a real surface.
Verification I ran against a locally patched 0.6.0
| Check | Result |
|---|---|
| Valid fence | .markdown-svg-block > svg, width 100%, aspect preserved via viewBox |
| Theme variables | --muted resolves to rgb(201,201,209), --border to the theme border |
<script> / onload / foreignObject / javascript: href |
removed; no globals set |
| Malformed / unclosed SVG | falls back to the code block |
mermaid, json, powershell fences |
unchanged |
mermaid preview action |
still works |
Note on caching (unrelated but worth knowing)
/ _next/static/** is served with cache-control: public, max-age=31536000, immutable. Any in-place edit of a built chunk stays invisible after a restart because the file name does not change; only a hard reload or a renamed chunk takes effect. Not a bug, but it makes local patches look broken.
Activity
xfz2021 commentedon Nov 6, 2026
anhnt256 commentedon Nov 11, 2026
I'll fix this — adding an
svgbranch next tomermaidin the markdown code renderer, with a newSvgBlockthat parses the fence with DOMParser, sanitizes it (script/foreignObject/style,on*attributes,javascript:/data:text/htmlhrefs) and renders it inline with the omp theme-variable aliases the web theme lacks. Malformed or still-streaming markup falls back to the code block. Opening a PR with the component, the renderer branch, and tests for the sanitizer and the dispatch.Merge pull request ompweb-dev#455 from anhnt256/feat/render-svg-fence…