Skip to content

feat(editor): render markdown images inline - #430

Merged
fcoury merged 11 commits into
mainfrom
explore/inline-markdown-images
Nov 3, 2026
Merged

fcoury merged 11 commits into
mainfrom
explore/inline-markdown-images

Conversation

@fcoury

@fcoury fcoury commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Standalone Markdown image lines are invisible as pictures today: concealment only hides the ![alt](path) markup and leaves the alt text. This prototype draws the image in the buffer while the cursor is somewhere else, and keeps that block the same height when the source is showing.

Scrolling a tall image used to scale the whole raster into whatever rows were still on screen, so the picture squashed, top bar included. Landing on the ![alt](path) line removed the image and pulled every line below it up by the height of the block.

Pressing j on that declaration did nothing from column 0: the picture rows were treated as soft wraps of the same line, so the motion landed inside the image and snapped back. j and k now walk only the declaration's own text, including its real wraps, and then step to the neighboring buffer line. Counts, half-page motion, gg/G, and the mouse wheel do not stop on picture rows.

What changed

In a Markdown buffer, a line whose only content is ![alt](target) becomes an image block in Normal mode. The block reserves vertical rows from the picture's aspect ratio and the terminal's cell aspect, capped by markdown_image_max_height (default 16). Moving the cursor onto that line shows the original Markdown in the block's first row. The remaining image rows stay underneath it, so j and k step onto the declaration and then past the block, and the lines below do not move.

When the top or the bottom of an image leaves the viewport, Red crops to the visible rows. Kitty transmits the scaled raster once and places only the visible slice: source y and h are the hidden and visible rows times that raster's pixels-per-row, and r is the visible row count. Sixel and iTerm2 encode that same cropped slice. The pixels-per-row stay constant as the scroll offset changes.

markdown_images = "auto" asks the terminal before it trusts environment variables. The probe is the Kitty graphics query (\x1b_Gi=31,...a=q...) plus primary DA, with a short timeout. A successful answer selects that protocol even inside a multiplexer.

Herdr (HERDR_ENV=1 or TERM_PROGRAM=herdr) parses the Kitty graphics protocol in the pane and draws it on a Kitty-capable outer terminal such as Ghostty. It does not use tmux passthrough. Zellij 0.45+ does the same. Tmux relays the protocol only when passthrough is enabled, so those queries and later placements, deletes, moves, and crops are wrapped in \x1bPtmux;. GNU screen does not relay graphics; a failed query there stays on half-blocks. A leaked GHOSTTY_RESOURCES_DIR or KITTY_WINDOW_ID does not select Kitty inside any of these.

Kitty and Sixel bitmaps are the cell rectangle in real pixels (columns * cell width, rows * cell height), resampled with Lanczos and never enlarged past the source. Cell size comes from CSI 16 t (CSI 6 ; height ; width t), then TIOCGWINSZ pixel size divided by the character grid. When a multiplexer reports zero, the size is 10×20. Kitty c/r fit that raster into the cells. iTerm2 receives pixels with width/height in cells and preserveAspectRatio=1. Deletes name both the image id and the placement id (a=d,d=i,i=ID,p=ID), which keeps the transmitted pixels.

The text fallback resamples with Lanczos and draws quadrant block elements (the same Unicode block as ▀, including ▘▝▖▗). Sextants are not used; that Legacy Computing block is missing from many fonts. Half-blocks remain the glyph when the 2×2 sample is a horizontal split. Those glyphs are not drawn under a Kitty placement. The cells there are blank spaces, so they do not punch holes in the picture.

Herdr composites Kitty images above cell backgrounds. An opaque popup cannot cover a z=-1 placement. When a popup intersects an image, Red deletes that placement and adds up to four placements for the remaining cells, using Kitty source rectangles (x,y,w,h) and matching c/r at the cell offset of each piece. The image is transmitted once with a=t and reused. Closing the popup deletes the fragments and restores one full placement. Sixel and iTerm2 drop for the whole image while a popup intersects them, so none of their pixels sit under the popup.

Remote images are read in chunks and stop at 8MB. A forced redraw deletes tracked Kitty placements before it forgets their ids.

On macOS, a workspace path is named the same way when canonicalize fails as when it succeeds, so /var and /private/var are one directory. A saved agent conversation is no longer dropped when the first lookup fails.

Terminal support

Protocol How it is chosen Encoded Exercised here
Kitty graphics Query returns i=31;OK, including through Herdr, Zellij, and tmux passthrough. Direct terminals may also match Kitty, Ghostty, WezTerm, or Konsole when no multiplexer is present. RGBA transmitted once. Placements use c/r and source rectangles. A popup splits the image. A scroll crops it at a constant pixels-per-row. Payload dimensions, placement split, top-crop placement, and pixels-per-row tests.
iTerm2 TERM_PROGRAM=iTerm.app / LC_TERMINAL=iTerm2 / VS Code, and only outside a multiplexer unless a query selected something else. Cropped PNG of the visible rows, cell width/height, preserveAspectRatio=1. Payload test and visible-row crop. Dropped while a popup intersects.
Sixel DA1 attribute 4, or foot/mlterm/Windows Terminal outside a multiplexer. Inside a multiplexer, only when DA1 says so. Sixel of the visible rows at the cell-pixel raster. Payload size test and visible-row crop. Dropped while a popup intersects.
Half-blocks / quadrants auto when the query fails and the terminal is truecolor, or markdown_images = "halfblocks". Lanczos samples, quadrant glyphs, ▀ for a horizontal split. Resampling test and the stills below.
Placeholder off, or neither graphics nor truecolor. [image: alt (path)] VHS recording below.
Herdr HERDR_ENV=1 or TERM_PROGRAM=herdr. Native Kitty parser, no passthrough wrap. Real protocol when the query succeeds. Popup splits and scroll crops are forwarded as placements. Detection test. Checked against herdr 0.9.5's client byte stream.
tmux TMUX set. Wrapped query. Passthrough for placement and delete when the query succeeds. Passthrough test.
Zellij ZELLIJ or ZELLIJ_SESSION_NAME. Native Kitty parser. Real protocol when the query succeeds. Detection test.
GNU screen STY or TERM=screen. Half-blocks when the query fails. Detection test.

Demo

Recorded with VHS in a truecolor terminal using JetBrainsMono Nerd Font. VHS does not composite Kitty graphics, so the Kitty stills show the cell layer only.

The declaration replaces the first image row. after the picture stays on the same screen row:

Half-block image with the cursor on the line above it

The same image with the raw Markdown in its first row and the picture still below

Cells under a Kitty image. The before frame is the quadrant fallback painted in the rectangle. The after frame leaves that rectangle blank:

Quadrant cells painted inside the Kitty image rectangle

The same rectangle left blank so the Kitty image is unobstructed

Text fallback, nearest-neighbor half-blocks compared with the resampled quadrant rendering:

Nearest-neighbor half-blocks of a striped image

Resampled quadrant rendering of the same image in Red

Half-blocks, then the source on the image line, then Insert mode, then the image again:

Half-block sunset rendered inline, then revealed as Markdown source

Keymap popup drawn over the image, then the image restored after Escape:

Which-key popup covering the lower part of the inline image

Placeholder with markdown_images = "off":

Placeholder line instead of pixels

How to Test

git fetch origin explore/inline-markdown-images
git checkout explore/inline-markdown-images
cargo build
unset NO_COLOR
export COLORTERM=truecolor
cargo run -- examples/markdown-images/sample.md

Scroll until the top or the bottom of a tall image leaves the window. The visible portion should keep its shape. Move the cursor onto the ![...] line. The source appears in the first row of the block, the picture stays in the rows below, and the following line does not jump. From column 0 or the end of a single-row declaration, j goes to the next buffer line and keeps the desired column. On a wrapped declaration, j steps through the text rows first. k from the following line comes back. Picture rows are skipped.

Inside Herdr on Ghostty, leave markdown_images at auto. The picture should be sharp across the whole rectangle, with no quadrant patches. Press g. The popup must contain no image pixels, and the rest of the image must stay sharp. Esc restores one full placement with no ghost. PageDown removes it when the line leaves the screen.

cargo test --lib markdown_images
cargo clippy --all-targets --all-features -- -D warnings

The library tests cover a constant pixels-per-row across scroll offsets, a top crop that is placed instead of rescaled, Sixel and iTerm2 bitmaps cropped to the visible rows, a declaration line that keeps the block height, j from column 0 and from the end of that line, k back onto it, blank cells under a Kitty image, and placements that do not intersect a popup rectangle.

Open in Web Open in Cursor 

Show standalone Markdown images in normal mode, and reveal the raw
source on the cursor line. Detect Kitty, iTerm2, and Sixel, then fall
back to half-blocks or a text placeholder.
Terminal graphics sit above the cell grid, so a which-key popup drawn
in cells was hidden by the image. Kitty placements now use z=-1, and
any image a popup, menu, or hover covers is removed until that overlay
closes, then placed again.
Linux can return ETXTBSY for a moment after the staged update's write
handle closes. Spawn retries that case. The Windows jq smoke test also
allows process creation to finish before treating the filter as hung.
@fcoury

fcoury commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-03T00:29:25.591704Z 27d2900 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@fcoury
fcoury marked this pull request as ready for review November 3, 2026 00:28

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 27d29000f6

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/editor/markdown_images.rs Resolved
Comment thread src/editor/markdown_images.rs Resolved
Comment thread src/editor/markdown_images.rs Resolved
A forced redraw cleared Kitty placement ids before the delete was sent,
so PageDown and a popup left the previous image on screen. Emit those
deletes first, move a placement when only its row changes, and crop an
image that is only partly in the viewport.

Herdr, tmux, and zellij use half-blocks in auto mode because they do not
reliably relay deletes. Popup cells use an explicit RGB background.
Remote images stop as soon as they pass the size cap, and the cell
fingerprint is taken after every visible image row is painted.
Herdr, tmux, and Zellij keep Kitty graphics when their capability query
succeeds. A leaked Ghostty variable no longer forces half-blocks, and
GNU screen still falls back when the query fails.

Kitty and Sixel rasters are resampled to the cell rectangle in pixels.
The size comes from CSI 16 t or TIOCGWINSZ, with a 10x20 default when a
multiplexer reports zero. iTerm2 sends the original pixels. The text
fallback resamples with Lanczos and draws quadrant blocks.
Graphics mode was still painting quadrant cells in the image rectangle.
Those cells have their own background, so a z=-1 placement showed through
only the gaps and looked patchy. A popup deleted the placement and left
the blocks, which made the whole picture go low-resolution.

Kitty placements now stay up under an overlay. The rectangle is blank
spaces, and the popup covers the overlap with its opaque background.
Sixel and iTerm2 still drop while a popup intersects them.
Herdr draws Kitty images above cell backgrounds, so an opaque popup
cannot cover a z=-1 placement. When a popup intersects an image, delete
that placement and add placements for the remaining cells, using source
rectangles. The pixels are transmitted once and reused when the popup
closes. Sixel and iTerm2 still drop for the whole image while a popup
intersects them.
A partial viewport scaled the whole raster into the remaining rows, and
landing on the image line collapsed that block so everything below jumped.
Crop Kitty, Sixel, and iTerm2 to the visible rows at a constant
pixels-per-row, and show the raw declaration in the block's first row.
j on a revealed image line treated the picture as soft wraps, so column 0
landed inside the block and snapped back. Walk only the declaration text,
then the next buffer line. On macOS, /var and /private/var name one workspace
when canonicalize fails, so a saved agent conversation is not dropped.
canonicalize rewrites /var and /tmp through /private. When that call failed,
the raw path and the canonical path were stored as two workspaces, so the
saved conversation was not the record the next launch loaded. One identity is
used for the lock, the filename, and the record, and the private prefix is
applied even if canonicalize fails. No retry.
A macOS temp path is long enough to wrap ![swatch](...) onto a second row,
so the line under the picture moved. The test now uses a short relative
path, and each wrapped text row takes an image row so the block height
stays the same when the source is wider than the window.
@cursor
cursor Bot marked this pull request as draft November 3, 2026 03:43
@fcoury
fcoury marked this pull request as ready for review November 3, 2026 03:44
@fcoury
fcoury merged commit a0391a7 into main Nov 3, 2026
18 checks passed
@fcoury
fcoury deleted the explore/inline-markdown-images branch November 3, 2026 03:47
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