# md-preview A live Markdown pane for Herdr. Open a `.md` file and it shows up beside your terminal, rendered, reloading on every save, with an in-place editor. Mermaid diagrams are drawn as real diagrams in a dark style that decides on its own what matters in them. ![md-preview](preview.png) ## What you see - The file as a reading column (about 70 characters wide) on an 8 px grid: headings, nested lists, tables, quotes, code, task lists. - ` ```mermaid ` blocks as diagrams, not code. Three levels of emphasis instead of everything shouting: - **focus**, 1–2 nodes: colored border and glow; - **main flow**, every solid route from the entry to the outcome: bright; - **side branches**, optional (`-.->`) and error paths: muted. - Pictures alone on a line drawn inline, several on one line as a gallery (up to 4 per row, short text between them becomes a caption). Web images are downloaded once per pane. - A header with the file name and folder, plus **Edit**. ## How it behaves - Reloads by itself when the file changes on disk, including when an agent rewrites it. - **⌘E** edits the raw Markdown in the same place, **⌘S** saves, **Esc** goes back. If the file changed on disk meanwhile, you choose: overwrite or reload. - Where the diagram focus comes from, first match wins: 1. the author: a `%% focus: , ` line inside the diagram; 2. the text around it: nodes named in the title and the paragraphs above and below; 3. the shape: the outcome of the main flow plus the node the most routes pass through (betweenness centrality). - Wide `graph LR` diagrams are tried as `TD` too; the narrower one wins. A diagram never shrinks below 85%; wider ones scroll sideways. - Clicking a diagram or picture opens it in [media-preview](../media-preview/RECIPE.md) with zoom. Links to `.md` open in the same pane, `http(s)` in the browser. - Commands: `herdr-app do md-preview.open:/path/file.md`, `md-preview.edit`, `md-preview.save`, `md-preview.preview`. ## Built from - `mods/md-preview/main.js`: the pane, block rendering, image and diagram staging. - `mods/md-preview/markdown.js`: a small dependency-free block parser. - `mods/md-preview/mermaid/`: `render.mjs` lays out the diagram with [beautiful-mermaid](https://github.com/lukilabs/beautiful-mermaid) (MIT, ELK, no browser, about 0.1 s) and restyles its SVG for resvg (no CSS variables, no `color-mix()`). `focus.mjs` picks the focus and main flow. `metrics.json` holds real glyph widths of Lexend Deca 600 and DM Mono so labels fit their boxes. - `extras/` (optional, macOS): a tiny AppleScript applet that makes ⌘-click on a `.md` path in the Herdr terminal, `open file.md` or a Finder double-click open the file in this pane, plus a `herdr-preview ` shell command. ## Install ```sh cp -R mods/md-preview ~/.config/herdr-app/mods/ cd ~/.config/herdr-app/mods/md-preview/mermaid && npm i herdr-app check md-preview # run from ~/.config/herdr-app/mods ``` Optional, makes the applet the default app for `.md` (`--no-default` only builds it): ```sh bash extras/install.sh ``` - **Selecting and copying**: letters inside a paragraph select as usual (TextView). Whole blocks select with a triple-click on the block, a click or a drag in the left margin (a 2 px bar beside the block: `dim` on hover, `accent` when selected), shift-click to extend, ⌘A for all; selected blocks get the theme's `selection` ground. ⌘C copies their markdown exactly as written in the file (the source lines they span, list markers included, common indent removed); ⌘⇧C copies `file:line` or `file:from-to`; Esc clears. On hover a code block shows **Copy** (its code) and a heading **Copy section** (the heading and everything to the next heading of the same or a higher level) and **Copy ref**. The header briefly says what was copied. The selection survives the file being rewritten. - **Front matter** (`---` … `---` on the first lines): a small key/value table (13 px, keys `dim`, values `muted`) over a 1 px rule, instead of a rule and a setext heading. - **Code blocks** show their sideways scrollbar whenever a line is too long. ## Watch out - **Diagrams need Node** on the PATH Herdr sees and `npm i` in `mermaid/`. Without it the block stays code with a one-line hint. - **Fonts**: diagrams use [Lexend Deca](https://fonts.google.com/specimen/Lexend+Deca) and [DM Mono](https://fonts.google.com/specimen/DM+Mono) (OFL). Install them, or resvg falls back to a system font and labels may not fit. - `beautiful-mermaid` is pinned to 1.1.3: the style is applied to its SVG by text patterns. - `extras/` is macOS-only (`osacompile`, `duti`, `lsregister`) and falls back to your default text editor when Herdr isn't running. - Herdr's `image()` reads only files under the app home, so pictures and diagrams are copied to `mods/md-preview/.cache/` and removed when the pane closes. - The parser is forgiving, not full CommonMark: no footnotes, HTML blocks or indented code. - TextView lets a mod only switch selection on: it can't read the selection, select all or add a triple-click, and headings or code drawn as plain text aren't selectable at all. That's why blocks are selected by the mod itself, from line ranges the parser keeps (`from`, `to` on every block; mermaid fences stay as blank lines so later line numbers don't move). - A field named `key` on the view would hide the `key()` method that handles keys: the cache folder's key is `cacheKey`. - `md-preview.` reaches every shown copy, so scripted selections act on all of them. ## Done when - `herdr-app do pane.split '{"direction":"right","mod":"md-preview","props":{"file":"/path/notes.md"}}'` shows the file rendered, and saving it elsewhere updates the pane. - A ` ```mermaid ` block shows as a diagram with one or two glowing nodes and muted side paths. - `md-preview.select:3-5` then `md-preview.copy` puts those blocks' markdown, as written, on the clipboard; `copy-section:N` on a heading copies up to the next heading of its level; front matter shows as a key/value table.