Skip to content

fix(images): embed image paths with parentheses - #4

Merged
lecstor merged 2 commits into
mainfrom
clanker/284a6b67-zed-extension-image-paths-with-parenthes
Nov 6, 2026
Merged

lecstor merged 2 commits into
mainfrom
clanker/284a6b67-zed-extension-image-paths-with-parenthes

Conversation

@lecstor

@lecstor lecstor commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

MAK-356. A markdown image whose path contains parentheses, such as ![x](a(b).png), was cut at the first ). The export then read the wrong file. When that file was missing, it stopped with a truncated name like a(b. When the cut still closed the reference, the remainder of the path was left in the text.

What changes

In server/src/images.ts, a bare destination now ends at the ) that balances the ( in ](, as CommonMark and the service's parser (marked) read it. Backslash-escaped parentheses are skipped.

Reference Path read from disk
![x](a(b).png) a(b).png
![x](a((b)).png) a((b)).png (any depth)
![x](a\(b\).png) a(b).png
![x](c%20(d).png) c (d).png
![x](<c (d).png>) c (d).png (already worked)
![x](u(v.png) u(v.png (fallback, as before)
![x](missing(1).png) export stops naming missing(1).png
  • Fallback: when the balanced read does not close the reference, the scan falls back to the old run up to the first ).
  • Unescaping: the backslash in \(, \), \< and \> is removed from a bare path before reading. This matches the VS Code and browser fixes. Other backslashes, including Windows separators, and paths inside <...> are left as written.
  • Behaviour change: ![a](u(v.png) "t") now reads u(v.png) (extension .png), so it is left alone), as the server reads it. Before, it embedded u(v.png and left "t") in the text.
  • Failure name: ImageFailure.src is now the path as read, so an escaped name is reported without its backslashes.

Linearity

The parentheses of each whitespace-free run are matched once, the first time an attempt starts inside it. Every later attempt in that run reuses the result, and attempts move forward only, so total work is linear. Only a ( that follows a ] is recorded; other parentheses are counted, so memory does not grow with them.

The second commit fixes a quadratic case that predates this branch. Every failed attempt whose src ended at the same whitespace block skipped the whole block again: 20,000 ![a](x followed by 20,000 spaces took 6.3 s. Those skips are now memoised by their start, and that input takes 8 ms.

Timings from a scratch script, at the largest of four doubling sizes:

Shape Size Time
![a]( repeated 4.0M chars 225 ms
![a](x( repeated 5.6M 263 ms
![a](() repeated 5.6M 345 ms
![a](\( repeated 5.6M 261 ms
one ![a]( + open parens 4.0M 146 ms
(a) runs 4.8M 111 ms

Each roughly doubles per doubling of size. ![a]( followed by 10M ( peaks at 102MB RSS, the same as main.

Review

A separate review agent reviewed 4198257. It fuzzed the memoised paren scan against a fresh scan from each position (200k random strings, no differences), compared old and new on 300k inputs, and checked hrefs against marked 15. Fixed in the second commit:

  • [WARN] the pre-existing whitespace quadratic above
  • [NIT] paren memory
  • [NIT] the Windows note
  • [NIT] test gaps

The fuzz re-run on the second commit also shows no differences. That commit has not had a separate review.

Known limits

  • Windows separator before a parenthesis: a separator directly before (, ), < or > reads as an escape. ![x](C:\pics\(1).png) reads C:\pics(1).png; write <C:\pics\(1).png> instead. marked reads it the same way.
  • Valid image nested in a malformed one: in ![a](x(![b](c(d).png), the inner c(d).png is neither embedded nor reported. The first-) fallback swallows it, while marked rejects the outer reference and renders the inner one, which the service cannot fetch. main behaves the same.
  • Angle brackets: a path inside <...> is read as written, backslashes included. marked unescapes \( there, so <pics\(1).png> reads pics\(1).png here and pics(1).png on the server.

Checks

  • npm test in server/: typecheck, 54 unit tests (7 new), 6 integration tests
  • npm run build

Not run inside Zed.

`![x](a(b).png)` and `![x](a\(b\).png)` were cut at the first `)`, so
the export read the wrong file and either stopped with a truncated name
or, for a reference that still closed, sent the path unembedded.

A bare destination now ends at the `)` that balances the one in `](`,
as CommonMark and the service's parser read it, with backslash-escaped
parentheses skipped. When that does not close the reference, the scan
falls back to the run up to the first `)`, so `![x](u(v.png)` still
reads `u(v.png`. The backslash in `\(`, `\)`, `\<` and `\>` is removed
from a bare path before it is read from disk; other backslashes and
paths inside `<...>` are left as written.

The parentheses are matched once per whitespace-free run and reused by
every attempt starting inside it, so the scan stays linear on runs of
unterminated or unbalanced references.
Review of 4198257 found:

- A pre-existing quadratic: every failed attempt whose src ends at the
  same whitespace block skipped the whole block again, so 20,000
  `![a](x` followed by 20,000 spaces took 6 s. Skips that start on
  whitespace are now memoised by their start (8 ms).
- ParenMemo recorded every `(` in a run, about 55 bytes a character:
  `![a](` plus 10M `(` peaked at 645MB. It now records only the `(`
  after a `]`, the only ones queried, and counts the rest (102MB, the
  same as before this branch).
- The docstring now says a Windows separator directly before `(`, `)`,
  `<` or `>` reads as an escape, and that `<...>` keeps it.

Tests pin the whitespace shapes and reuse of a match within one run.
@lecstor
lecstor merged commit 6f1508b into main Nov 6, 2026
2 checks passed
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