Skip to content

Render inline SVG diagrams to hosted images when cross-posting - #7

Merged
DanMat merged 1 commit into
mainfrom
feat/syndication-inline-svg
Sep 7, 2026
Merged

DanMat merged 1 commit into
mainfrom
feat/syndication-inline-svg

Conversation

@DanMat

@DanMat DanMat commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

Flagged by the danmat.dev portfolio agent: syndicating a post whose body has a raw inline <svg> diagram fails with HTTP 403 from Dev.to (Forem sanitises body HTML and rejects inline SVG; Hashnode has the same limitation). Confirmed by probing the Dev.to API directly: the body with <svg> blocks stripped posts 201; with them, 403.

What this does

Instead of dropping the diagrams, the body sent to a target now has each inline <svg> rendered to a PNG, hosted on the site, and replaced with a normal markdown image, so the diagram actually shows on Dev.to and Hashnode. Done in the shared Syndicator path, so both targets benefit. The stored post body on the site keeps its inline SVG unchanged.

How

  • SvgRasterizer (behind a Rasterizer interface) shells out to rsvg-convert via an argv array (no shell). Renders at ~1200px onto a solid light background with a dark ink for currentColor (declared colours like the accent hex untouched), so a currentColor diagram stays legible on any platform's light/dark theme.
  • Content-addressed hosting (UploadsDiagramStore): keyed by the SVG's hash, stored under uploads/nimbuscms.blog/diagrams/<ab>/<hash>.png. The file is the cache, so a diagram renders once and re-syndication reuses it. No DB table, nothing to migrate.
  • DiagramInliner leaves fenced code blocks and prose untouched; alt text from the SVG's aria-label, else "Diagram".
  • Fallback, never hard-fail: if a diagram can't be rendered or hosted, it degrades to a short pointer to the canonical original, so a hiccup is a working post, not a 403.
  • 403 translation: a target 403 now says "rejected the body, likely raw HTML or SVG" instead of a bare status, so an agent isn't misled into blaming the API key.

Security

Author SVG is treated as untrusted: <script>, event handlers, foreignObject, DOCTYPE/entities (XXE), and any external href (a file:/remote ref is a local-file-read / SSRF vector, since the rendered pixels get hosted publicly) are refused; size- and time-capped; SVG isolated in a private temp dir; librsvg runs no scripts and fetches no network. A refused SVG falls back to the pointer.

Requires (image)

Rendering needs rsvg-convert (librsvg2-bin) plus a base font (fonts-dejavu-core) in the image. Without it, diagrams fall back to the pointer, so the plugin still works everywhere. Documented in the README.

Verified

Rasterized the real diagram from how-i-stopped-an-llm-from-fabricating-citations (via the app's PDO path, valid UTF-8): renders cleanly, text crisp, currentColor → dark ink, accent amber preserved, solid paper background. The SVG passes the safety guard (no scripts/foreignObject/external refs).

Tests

DiagramInlinerTest (svg → hosted image; fenced code + prose preserved; render-once; render/host failure → canonical pointer), SvgRasterizerTest (a stub binary proves safe SVG renders while script/handler/foreignObject/doctype/external-href/oversize/non-svg are all refused, and a missing binary degrades to null), a Syndicator test that the transform reaches the outgoing body, and the Dev.to/Hashnode 403 translations. phpstan L6 + cs-fixer clean.
🤖 Generated with Claude Code

Dev.to (Forem) and Hashnode sanitise raw HTML and reject inline <svg>: a post
whose body contains SVG diagrams fails to syndicate (Dev.to answers 403) or
loses the figure. Instead of dropping diagrams, the body sent to a target now
has each inline <svg> rendered to a PNG, hosted on the site, and replaced with
a normal markdown image, so the diagram actually shows on the cross-post. The
stored post body is never changed; only the outgoing copy is transformed, in
the shared Syndicator path so both targets benefit.

- SvgRasterizer (behind a Rasterizer interface) shells out to rsvg-convert via
  an argv array (no shell), rendering onto a solid light background with a dark
  ink for currentColor so currentColor-based diagrams stay legible on any
  platform theme; declared colours are untouched. Author SVG is untrusted:
  scripts, event handlers, foreignObject, DOCTYPE/entities, and external refs
  are refused, size- and time-capped, and the SVG is isolated in a private temp
  dir. librsvg runs no scripts and fetches no network.
- Content-addressed hosting (UploadsDiagramStore): a diagram is keyed by the
  hash of its SVG and stored under uploads/<pluginId>/diagrams/, so it renders
  once and re-syndication reuses it. The file is the cache, so there is no table
  and nothing to migrate.
- DiagramInliner leaves fenced code blocks and prose untouched. Any render or
  host failure degrades to a short pointer to the canonical original, so a
  hiccup is a working post, not a 403.
- A target 403 is now translated to a clear "rejected the body, likely raw HTML
  or SVG" message, so an agent is not misled into blaming the API key.

Rendering needs rsvg-convert (librsvg2-bin) plus a base font in the image;
without it, diagrams fall back to the pointer. README documents this.

Tests: DiagramInliner (svg -> hosted image, fenced code + prose preserved,
render-once, render/host failure -> canonical pointer), SvgRasterizer safety
guard (a stub proves safe SVG renders while script/handler/foreignObject/
doctype/external-href/oversize/non-svg are refused, and a missing binary
degrades to null), the Syndicator applies the transform to the outgoing body,
and the Dev.to/Hashnode 403 translations.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@DanMat
DanMat merged commit 6505258 into main Sep 7, 2026
2 checks passed
@DanMat
DanMat deleted the feat/syndication-inline-svg branch September 7, 2026 19:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant