Repository navigation
Mermaid live preview + v0.4.0 release - #36
Merged
Merged
Conversation
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #35.
Summary
--no-mermaiddisables the server entirely;--mermaid-portpins it.assets/and embedded viainclude_bytes!so the page works offline.Why a hand-rolled HTTP server
The first cut used
tiny_http, but its 1024-byteBufWriterand the 8192-bytechunked_transfer::Encoderbuffer mean response headers never reach the client until enough body bytes have accumulated. EventSource sits onconnecting…forever when the cursor isn't on a mermaid block at startup. Switching tostd::net::TcpListenerwith explicitflush()after every event resolved it; integration tests undertests/preview_sse.rslock the behaviour in.Test plan
cargo test— 17 tests pass (10 unit + 3 integration + 4 render)cargo clippy --all-targets -- -D warningscleancargo fmt --checkcleanexamples/showcase.md:--no-mermaid: no server, no URL annotation