Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "markdown-browser"
version = "0.3.0"
version = "0.4.0"
edition = "2024"
license = "MIT"
description = "A terminal markdown browser with first-class GFM table rendering"
Expand Down
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ Status: early development.
- **Yank with expand/shrink** — `y` enters yank mode and grows the selection (line → paragraph → list item / blockquote → heading section → whole document); `Y` shrinks; `Enter` copies to the OS clipboard
- **Link following** — local `.md` files (relative and absolute), heading anchors (`#slug`), markdown URLs fetched in-app, and other URLs handed off to the OS
- **HTTPS URL input** — accepts an `http(s)://` URL on the command line; markdown links to remote `.md` / `.markdown` files navigate in-app, with relative links resolved against the remote document's URL
- **Mermaid live preview** — an embedded HTTP server streams the mermaid block under the cursor to a browser tab over SSE; the figure stays put when the cursor moves away, so you can read prose elsewhere while the diagram remains visible
- **History navigation** — back / forward through visited locations
- **In-app help** — `?` shows the full keybinding list
- **Plain-text render subcommand** — `markdown-browser render <file>` writes ANSI-styled output to stdout, suitable for piping or snapshot testing

### Out of scope (kept extensible)

- Image rendering (Sixel / Kitty / iTerm2 inline)
- Mermaid diagram rendering

The renderer architecture exposes a pluggable trait so an alternative implementation can be slotted in later without restructuring callers.

Expand Down Expand Up @@ -82,6 +82,32 @@ Press `?` inside the TUI for the same list shown here.
| `Enter` (in yank mode) | Copy selection to OS clipboard |
| `Esc` (in yank mode) | Cancel yank |

### Mermaid live preview

The first time the cursor lands inside a fenced code block tagged
`mermaid`, `markdown-browser` lazily binds an HTTP server on `127.0.0.1`
(random port by default) and appends the preview URL to every mermaid
fence line in the buffer:

```
``` mermaid http://127.0.0.1:54321
```

Open that URL in a browser once and the tab live-renders whichever
mermaid block the cursor is currently on. When the cursor moves
outside a mermaid block the previous diagram stays on screen, so you
can read prose alongside the figure.

Flags:

- `--no-mermaid` — disable the preview server entirely (it won't bind a
port even if you visit a mermaid block)
- `--mermaid-port <PORT>` — pin the port (useful when reverse-proxying
or sharing the tab between sessions)

The mermaid runtime is bundled into the binary, so the page works
without network access once the binary is downloaded.

### Customizing

Drop a TOML file at `$XDG_CONFIG_HOME/markdown-browser/config.toml` (default
Expand Down
3,405 changes: 3,405 additions & 0 deletions assets/mermaid.min.js

Large diffs are not rendered by default.

90 changes: 90 additions & 0 deletions assets/preview.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>markdown-browser preview</title>
<style>
:root {
color-scheme: light dark;
--bg: #ffffff;
--fg: #1a1a1a;
--muted: #6b7280;
--border: #e5e7eb;
--accent: #2563eb;
}
@media (prefers-color-scheme: dark) {
:root {
--bg: #0d1117;
--fg: #e6edf3;
--muted: #8b949e;
--border: #30363d;
--accent: #58a6ff;
}
}
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--fg); font: 14px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif; }
header { padding: 0.5rem 1rem; border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; }
header h1 { margin: 0; font-size: 0.85rem; font-weight: 600; color: var(--muted); letter-spacing: 0.05em; text-transform: uppercase; }
header .status { font-size: 0.75rem; color: var(--muted); }
header .status .dot { display: inline-block; width: 0.5rem; height: 0.5rem; border-radius: 50%; background: var(--muted); margin-right: 0.25rem; vertical-align: middle; }
header .status.live .dot { background: #22c55e; }
main { padding: 1.5rem; }
.placeholder { color: var(--muted); text-align: center; padding: 4rem 1rem; font-size: 0.95rem; }
.placeholder code { background: var(--border); padding: 0.125rem 0.375rem; border-radius: 0.25rem; font-size: 0.85em; }
.error { color: #dc2626; background: rgba(220, 38, 38, 0.08); border: 1px solid rgba(220, 38, 38, 0.3); padding: 1rem; border-radius: 0.375rem; white-space: pre-wrap; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.85rem; }
.mermaid { display: flex; justify-content: center; }
.mermaid svg { max-width: 100%; height: auto; }
</style>
</head>
<body>
<header>
<h1>Mermaid Preview</h1>
<span class="status" id="status"><span class="dot"></span>connecting…</span>
</header>
<main id="main">
<div class="placeholder">Place the cursor on a <code>mermaid</code> code block in <code>markdown-browser</code> to render it here.</div>
</main>
<script src="/mermaid.min.js"></script>
<script>
mermaid.initialize({ startOnLoad: false, theme: window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'default', securityLevel: 'strict' });

const main = document.getElementById('main');
const status = document.getElementById('status');
let renderCounter = 0;

function setStatus(text, live) {
status.classList.toggle('live', !!live);
status.innerHTML = '<span class="dot"></span>' + text;
}

async function render(source) {
if (!source) {
main.innerHTML = '<div class="placeholder">Place the cursor on a <code>mermaid</code> code block in <code>markdown-browser</code> to render it here.</div>';
return;
}
const id = 'mermaid-' + (++renderCounter);
try {
const { svg } = await mermaid.render(id, source);
main.innerHTML = '<div class="mermaid">' + svg + '</div>';
} catch (err) {
main.innerHTML = '<div class="error">' + (err && err.message ? err.message : String(err)) + '</div>';
}
}

function connect() {
const es = new EventSource('/events');
es.addEventListener('open', () => setStatus('live', true));
es.addEventListener('mermaid', (e) => {
let payload;
try { payload = JSON.parse(e.data); } catch { payload = { source: '' }; }
render(payload.source || '');
});
es.addEventListener('error', () => {
setStatus('reconnecting…', false);
// EventSource auto-reconnects; no manual retry needed.
});
}

connect();
</script>
</body>
</html>
27 changes: 27 additions & 0 deletions examples/showcase.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,33 @@ ASCII 罫線も生のまま:
+---+---+
```

### Mermaid (browser-side preview)

ターミナル内ではコードのままだが、`--mermaid` を有効にしたまま実行して
ブラウザを起動時に表示された URL に向けると、カーソルがこのブロックに
あるあいだ図がライブで描画される。カーソルを別の場所に動かしても直前の
図はそのまま残る。

```mermaid
graph TD
A[markdown-browser] -->|SSE| B(Browser tab)
B --> C{Cursor on<br/>mermaid block?}
C -->|yes| D[Render diagram]
C -->|no| E[Keep previous]
```

複数ブロックに対応していて、カーソルが入ったブロックに切り替わる:

```mermaid
sequenceDiagram
participant TUI
participant Server
participant Browser
TUI->>Server: set_source(mermaid)
Server-->>Browser: SSE: mermaid event
Browser->>Browser: mermaid.render(svg)
```

## 水平線

下に thematic break が入る。
Expand Down
19 changes: 18 additions & 1 deletion src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use clap::{Parser, Subcommand};
pub mod ansi;
pub mod keymap;
pub mod net;
pub mod preview;
pub mod render;
pub mod source;
pub mod tui;
Expand All @@ -23,6 +24,15 @@ pub struct Cli {
/// Inside the TUI, press `e` to toggle at runtime.
#[arg(long, global = true)]
pub no_emoji: bool,

/// Disable the embedded mermaid preview server.
#[arg(long)]
pub no_mermaid: bool,

/// Bind the mermaid preview server to this port. Defaults to an
/// OS-assigned ephemeral port.
#[arg(long, value_name = "PORT")]
pub mermaid_port: Option<u16>,
}

#[derive(Subcommand, Debug)]
Expand Down Expand Up @@ -55,6 +65,13 @@ pub fn run(cli: Cli) -> std::io::Result<()> {
render::ColorChoice::resolve(color, no_color),
emoji,
),
None => tui::run(cli.file.as_deref(), emoji),
None => tui::run(
cli.file.as_deref(),
emoji,
tui::PreviewConfig {
enabled: !cli.no_mermaid,
port: cli.mermaid_port,
},
),
}
}
Loading