fix(desktop): keep image alt/src/title strings so markdown preview can't crash on numeric alt text

Merged
SA
3mo ago
+107-0

Problem

Opening a markdown document whose image alt text (or src/title) is a purely numeric or boolean-looking string — e.g. ![123](screenshot.png) — crashes the rendered markdown view on every render. On the affected machine it threw 8 times in five minutes as the component remounted; the preview/editor is unusable for any document with that shape, and it hits any user whose content matches. The same throw was captured twice: once escaping via TipTap's deferred onCreate timer (DESKTOP-100) and once from the value-sync effect (DESKTOP-ZZ).

Root cause

@tiptap/core's default attribute parser (used for any attribute without an explicit parseHTML) is fromString(element.getAttribute(name)), which coerces "123" → number 123 and "true"/"false" → booleans. @tiptap/extension-image@3.21.0 declares src/alt/title with no parseHTML, so tiptap-markdown's markdown→HTML→DOM parse of ![123](x.png) loads an image node with alt: 123 (a number).

Serializing back to markdown — getEditorMarkdown, called from createSourceTracking in onCreate and from the value-sync effect's equality check — reaches prosemirror-markdown 1.13.5's image serializer:

state.write("![" + state.esc(node.attrs.alt || "") + "](" + node.attrs.src.replace(...) +
    (node.attrs.title ? ' "' + node.attrs.title.replace(...) + '"' : "") + ")")

esc calls .replace on the non-string and throws TypeError: str.replace is not a function (bundled: str2.replace). Numeric src (![a](123)) and numeric title (![a](x "2024")) throw on the same serializer line the same way — all reproduced in the new tests, failing before the fix.

Deliberately not fixed at the serializer or by swallowing errors: getEditorMarkdown's output is the merge baseline for format-preserving saves, so the document must simply never hold a non-string. Archiving instead of fixing would leave every affected document permanently broken, and Sentry-side suppression is off the table per repo policy.

Fix

Give SafeImage explicit verbatim parseHTML for src/alt/title (element.getAttribute(...)), bypassing the fromString coercion, so those attributes stay exactly what the document said. width/height keep the coerced default — numbers are fine there and the markdown serializer never touches them. As a bonus, ![123](x.png) now round-trips with its alt intact instead of crashing.

Reaches the failure: the events came from release 1.22.0 with @tiptap/core@3.21.0 / tiptap-markdown@0.9.0 / prosemirror-markdown@1.13.5 — the exact versions on main today, and createMarkdownExtensions.ts is unchanged since #6406. The fix ships in the next desktop release and executes on the same path that threw (initial content parse feeding both capture sites).

No typed cause added — nothing reads one; no error construction is involved at all, the fix removes the throw at its source.

Out of scope, same latent defect (naming, not fixing here): MarkdownEditor.tsx's LinearImage (apps/desktop/src/renderer/components/MarkdownEditor/MarkdownEditor.tsx:71) extends the same Image extension and serializes via tiptap-markdown, so a numeric alt there would throw identically. No Sentry events point at it; it can get the same three-line treatment in a follow-up.

Verification

  • bunx biome check on both changed files — Checked 2 files in 11ms. No fixes applied.
  • cd apps/desktop && bun run typecheck — tsc --noEmit clean.
  • bun test apps/desktop/src/renderer/components/MarkdownRenderer — 49 pass, 0 fail (before the fix, the 4 new coercion tests failed: numeric alt reproduced the exact esc throw from the issue, numeric src/title threw on node.attrs.src.replace / node.attrs.title.replace; the ordinary-image round-trip control passed both before and after).

Refs DESKTOP-100
Refs DESKTOP-ZZ

https://claude.ai/code/session_01DDjpii9beJCCCn5Z2cfG73


Summary by cubic

Prevents markdown preview crashes when an image’s alt/src/title are numeric or boolean-looking strings by preserving them as strings. Old behavior coerced these values via @tiptap/core, causing prosemirror-markdown to throw during serialization; new behavior reads attributes verbatim so the document round-trips safely.

  • Extends image as SafeImage and overrides parseHTML for src/alt/title to return element.getAttribute(...) verbatim; leaves width/height default coercion intact.
  • Adds tests that cover numeric/boolean-like alt/src/title and a normal image round-trip.
  • Fix applies on the same path reported in DESKTOP-100 and DESKTOP-ZZ; no migration or config changes required.
  • Known follow-up: LinearImage in MarkdownEditor.tsx shares the defect and will get the same treatment separately.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed Markdown images with numeric or boolean-looking src, alt, and title values being incorrectly converted.
    • Ensured image attributes remain intact when Markdown is parsed and converted back.