Skip to content

Mermaid live preview + v0.4.0 release - #36

Merged
m5d215 merged 4 commits into
mainfrom
feat/mermaid-preview
May 13, 2026
Merged

m5d215 merged 4 commits into
mainfrom
feat/mermaid-preview

Conversation

@m5d215

@m5d215 m5d215 commented May 13, 2026

Copy link
Copy Markdown
Owner

Closes #35.

Summary

  • Embedded HTTP/SSE server streams the mermaid block under the cursor to a browser tab; the diagram stays put when the cursor moves elsewhere so it can sit alongside unrelated prose.
  • The server binds lazily on the first cursor entry into a mermaid block, so sessions that never touch a mermaid block never bind a port.
  • Every mermaid fence line is annotated inline with the preview URL once the server is up — no hunting through the status bar.
  • --no-mermaid disables the server entirely; --mermaid-port pins it.
  • mermaid.js v11 is vendored under assets/ and embedded via include_bytes! so the page works offline.
  • Bumps the version to 0.4.0.

Why a hand-rolled HTTP server

The first cut used tiny_http, but its 1024-byte BufWriter and the 8192-byte chunked_transfer::Encoder buffer mean response headers never reach the client until enough body bytes have accumulated. EventSource sits on connecting… forever when the cursor isn't on a mermaid block at startup. Switching to std::net::TcpListener with explicit flush() after every event resolved it; integration tests under tests/preview_sse.rs lock the behaviour in.

Test plan

  • cargo test — 17 tests pass (10 unit + 3 integration + 4 render)
  • cargo clippy --all-targets -- -D warnings clean
  • cargo fmt --check clean
  • Manual TUI verification on examples/showcase.md:
    • Initial state: no port bound, fence lines plain
    • Cursor enters first mermaid block: server starts, URL appears on every mermaid fence, browser shows the diagram
    • Cursor moves to second mermaid block: browser switches diagrams
    • Cursor moves outside mermaid: previous diagram remains visible
    • --no-mermaid: no server, no URL annotation

m5d215 added 4 commits May 13, 2026 17:51
markdown-browser now binds an HTTP server on 127.0.0.1 at startup and
prints the URL into the status bar. Opening the URL once gives a
browser tab that renders whatever mermaid block the cursor is currently
inside, updating live as the cursor or the underlying file changes.
When the cursor moves outside any mermaid block the previous diagram
stays on screen so the figure can sit alongside unrelated prose.

Implementation:
- `src/cli/preview.rs`: tiny_http server with three routes (`/`,
  `/mermaid.min.js`, `/events`). SSE clients each get a thread with a
  custom `Read` impl that blocks on an mpsc receiver. Identical sources
  are deduplicated at the `PreviewHandle` level so calling
  `set_source` every frame is cheap.
- `src/render`: extend `RenderOutput` with `mermaid_blocks: Vec<MermaidBlock>`,
  populated when a fenced code block's info string is `mermaid`
  (case-insensitive). Range is inclusive of the fence lines.
- `src/cli/tui.rs`: `App::push_mermaid_preview` runs once per main-loop
  iteration after parse/reload, so file edits and history navigation
  participate naturally.
- `--no-mermaid` disables the server; `--mermaid-port` pins it.
- mermaid.min.js v11 (3.2 MB) is vendored under `assets/` and embedded
  via `include_bytes!` so the page works offline.
tiny_http wraps the response writer in a 1024-byte BufWriter and the
chunked-transfer encoder it uses buffers up to 8192 bytes before
emitting a chunk. Together they hold the response headers and every
SSE event in memory until that accumulator fills — long enough that
EventSource never sees `open` fire and the browser sits on
`connecting…` indefinitely whenever the cursor isn't already on a
mermaid block at startup.

Replace the tiny_http server with a minimal accept-and-respond loop
on `std::net::TcpListener`:

- One thread per connection.
- Static routes (`/`, `/mermaid.min.js`) respond with explicit
  `Content-Length` + `Connection: close`.
- `/events` writes headers + flushes immediately, then loops on
  `rx.recv()` writing each event followed by `stream.flush()`. No
  chunked encoding, no intermediate buffering. `TCP_NODELAY` is set
  so events aren't held by Nagle on the loopback path.

Integration tests added under `tests/preview_sse.rs` lock in the
behaviour: response status reaches the client before any event is
pushed, and a pushed event reaches the socket promptly. Both
reproduced the original failure against tiny_http.
The preview server used to bind a port at TUI startup regardless of
whether the document contained any mermaid. Move the bind to the first
cursor entry into a mermaid block so sessions with no mermaid never
touch the network, and ports stay free for sessions that don't need
them.

Also drop the one-shot "mermaid preview: http://..." status-bar
message in favour of inlining the URL on the fence line itself:

    ``` mermaid http://127.0.0.1:54321

The annotation uses a dim style so it reads as metadata, not as part
of the markdown. It is reapplied automatically after every reparse
(file reload, history navigation, shortcode toggle) once the server
is up, so the URL travels with the buffer.
Bumps the minor version for the embedded mermaid live preview feature
(#35).
@m5d215
m5d215 merged commit d2e20bd into main May 13, 2026
3 checks passed
@m5d215
m5d215 deleted the feat/mermaid-preview branch May 13, 2026 09:23
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.

Mermaid rendering via embedded HTTP server + browser live preview

1 participant