Repository navigation
feat: follow relative Markdown links and live-reload relative images - #12
Merged
Merged
Conversation
The page always lives at `/`, so the browser resolved a document's relative references against the wrong directory: `[b](other.md)` navigated the preview to a bare 404 page, and `../` was lost before the server saw it. Resolve them on the server instead, against the current document's directory. - `POST /open` takes a link as written in the document and switches the preview to it, through the same `switch_to` a control-socket `open` uses. The client catches plain clicks on relative `.md`/`.markdown` links and posts them; the switch comes back as a `reload` and the new document starts at the top. A link that cannot be followed (missing file, not Markdown, server gone) shows a toast saying why. It requires an `X-Mdpreviewer` header, which a browser will not send cross-origin without a CORS preflight the server never grants, so another web page cannot switch the preview. - `GET /file?path=<link>` serves relative images. The client rewrites each relative `img` source on the parsed fragment, before insertion, so the unresolved URL is never requested. Only image extensions are served, checked on the canonical path so a symlink or `..` cannot reach other files, and `Content-Security-Policy: sandbox` keeps an SVG opened on its own from running script on this origin. `is_markdown` moves to `render.rs` so the server can share it with the CLI. The `basics.md` fixture gains a relative link to `mermaid.md` to try it by hand. fix(client): follow Markdown links on modified and middle clicks Left to the browser, a ctrl-, shift- or middle-click on a relative Markdown link opened it in a new tab resolved against `/`, which is a 404. The server shows one document to every tab, so a second tab could not show a different file anyway: follow the link in place instead. Middle clicks arrive as `auxclick`, so listen for that too. feat: reload relative images when they are replaced on disk Only the Markdown file was watched, so replacing an image the document shows changed nothing until the text was saved, and not even then: a page reuses an image it already holds for a URL, `no-cache` or not. - The server records each image `/file` serves for the current document and watches it, one watch per directory through the new `watch::watch_files`. A change sends a new `images` SSE event. The set belongs to `Current`, so switching documents drops it. - Image URLs carry `&v=<n>`. The client bumps `n` on `images` and re-points every relative image, so only the images refetch and the text does not flicker. An open zoom is re-cloned once they have decoded. Between replacements the URL is stable, so saving the text does not refetch every image.
- `--restart` fails if the old server has not released the socket after 2 s, instead of handing the file to the server it meant to replace. - `--quit` reports an unsafe socket directory instead of claiming no server is running. `--restart` still falls through to a standalone preview there. - A control `quit` removes the socket even when the process is not going to exit, so an in-process test can check it. New test: `quit_removes_the_control_socket`. - `format_request` matches every `Request` variant, so a new one cannot go out on the wire as `quit`. - `expand_tilde` takes `HOME` through `expand_home`, so its test no longer mutates the environment of a multi-threaded test runner. - Drop a stale doc comment and duplicate `#[cfg(unix)]` above `Stop`. - A single click on a code block or table waits out the double-click window before zooming, so double-clicking to select a word does not open the overlay. - CI checks the MSRV (1.88), and the release workflow runs CI first, so a commit that fails it is never published.
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.
Summary
The preview page always lives at
/, so a document's relative references were resolved against the wrongdirectory: clicking
[setup](docs/setup.md)opened a bare 404 page,../was lost before the server saw it,and relative images did not load. The server now resolves them against the current document's directory.
Relative links and images
.md/.markdownfile switches the preview to that file, starting atthe top. The client posts the link to
POST /open, which switches the document the same way a control-socketopendoes. Ctrl-, shift- and middle-clicks do the same: the server shows one document to every tab, so a newtab could not show a different file anyway.
at the top of the window saying why.
GET /file?path=<link>. The client rewrites theirsrcbefore thefragment is inserted, so the unresolved URL is never requested.
served for the current document and sends an
imagesSSE event. The client bumps a version in the image URLs,so only the images are fetched again and the text does not flicker. The browser reuses an image it already
holds for a URL even under
no-cache, so a new URL is needed.Security
POST /openrequires anX-Mdpreviewerheader. A browser will not send it cross-origin without a CORSpreflight, which the server never grants, so another web page cannot switch the preview. Other methods get 405.
/fileserves image extensions onon the canonical path, so a symlink or..cannot reach other files. Responseicy: sandbox
andnosniff`, so an SVG opened onits own runs no script.
Fixes from review
--restartfails if the old serve within 2 s, instead of handing thefile to theserver it meant to replace.
--quitreports an unsafe socket directory instead of saying no server is running.quitalways removes thd by a test.format_requestmatches everyRequestvariant, so a future one cannot go out on the wire asquit.expand_tildetest no longer changesHOMEwhile other tests run in parallel.select a word no longer opens the
CI
msrvjob checks the build oninCargo.toml`.ci.ymlas a reusable workflow before release-plz publishes, so a committhat fails
CI is never released. As a result every push to
mainruns CI twice../.github/workflows comment. The pinned actionlint doesnot accept GitHub's newer$/` form yet.Testing
cargo test: 64 tests pass. New iing links (including refusals andCORS),serving images, image-change eventn the document switches, and quitremoving the
socket.
mise run lintis clean, andcargo +1.88 check --locked --all-targetsbuilds.examples/basics.mdnow has a working and a broken relative link to try this.Known limitations
the Markdown file.