|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## What this is |
| 6 | + |
| 7 | +`mdview` is a single-binary Go CLI that converts a markdown file to styled HTML and opens it in the |
| 8 | +default browser. It's essentially one file, `main.go`, plus a handful of embedded assets. There are |
| 9 | +no other Go source files and no test files currently in the repo. |
| 10 | + |
| 11 | +## Build & run |
| 12 | + |
| 13 | +Requires Go (see `go.mod`/`mise.toml` for version) and, for packaging targets, `just` and `pandoc`. |
| 14 | + |
| 15 | +- Quick local build: `go build` (produces `./mdview`) |
| 16 | +- Run directly without building: `go run . <filename.md>` |
| 17 | +- Full cross-platform build via `just` (uses `justfile`, the project's task runner — replaced the old Makefile): |
| 18 | + - `just` or `just linux` — Linux amd64/arm64/i386 builds |
| 19 | + - `just all` — linux + windows + darwin + freebsd |
| 20 | + - `just deb` — build `.deb` packages (requires a prior linux build) |
| 21 | + - `just snap` — build snap package via `snapcraft` |
| 22 | + - `just clean` — remove build artifacts |
| 23 | + - Pass `VERSION=x.y.z` as an env var to any target to stamp the version (embedded via `-ldflags -X main.appVersion=...`); CI derives this from the previous git tag. |
| 24 | + - `just manpage` regenerates `mdview.1` from `mdview.1.md` via pandoc. |
| 25 | +- There is no test suite and no lint target configured; `go vet ./...` and `gofmt` are reasonable sanity checks before committing. |
| 26 | + |
| 27 | +## Architecture |
| 28 | + |
| 29 | +Everything happens in `main()` in `main.go`, in a straight-line pipeline: |
| 30 | + |
| 31 | +1. **Flag parsing** — `-o`, `-v/-version`, `-h/-help`, `-b/-bare`. First positional arg is the input markdown file. |
| 32 | +2. **Image inlining** (`processMarkdownImages` → `processHTMLImages`/markdown image regex + `imageToDataURI`) — rewrites relative image references (both `![]()` markdown syntax and raw `<img src=...>` HTML) into base64 `data:` URIs *before* markdown parsing, so the output HTML is fully self-contained/offline-viewable. This includes path-traversal guards (caps `..` traversal depth) and a 10MB per-image size cap. |
| 33 | +3. **Markdown parsing** via Goldmark, configured with: |
| 34 | + - `extension.GFM` (tables, task lists, strikethrough, etc.) |
| 35 | + - `extension.Typographer` (smart quotes/dashes) |
| 36 | + - `go.abhg.dev/goldmark/mermaid` with `NoScript: true` (diagram rendering is handled ourselves) |
| 37 | + - `html.WithUnsafe()` (raw HTML passthrough is required for the image-tag handling above) |
| 38 | +4. **Title extraction** (`getTitleFromAST`/`extractText`) — walks the parsed AST for the first H1 to use as the `<title>`. |
| 39 | +5. **Mermaid script embedding** (`embedMermaidScript`) — only injects the embedded `mermaid.min.js` + an init script (with light/dark theme detection via `prefers-color-scheme`) if the rendered HTML actually contains a mermaid block, keeping non-diagram output lean. |
| 40 | +6. **Templating & output** — `template.html` and `github-markdown.css` are embedded via `//go:embed` and combined with the rendered content via `fmt.Fprintf`. `-bare` skips the CSS. Output goes to `-o <path>` if given, otherwise a random temp filename (see below). |
| 41 | +7. **Launch** — opens the resulting HTML file via `github.com/pkg/browser`. |
| 42 | + |
| 43 | +### Temp file / Snap sandboxing (`getTempDir`, `isSnap`) |
| 44 | + |
| 45 | +Output location resolution order: `MDVIEW_DIR` env var → (if running as a Snap, per `SNAP_USER_COMMON` env var) `~/mdview-temp` → OS default temp dir. This exists because Snap-sandboxed browsers (e.g. Snap Firefox) can't read from `/tmp`, only from the user's home directory — `check()` also prints a Snap-specific hint on file errors for this reason. |
| 46 | + |
| 47 | +### Packaging |
| 48 | + |
| 49 | +`justfile`, `control` (Debian control file template, `VERSION` substituted at build time), `mdview.1.md` |
| 50 | +(man page source) and `snap/` all support the release pipeline in `.github/workflows/release.yml`, |
| 51 | +which triggers on tag push, builds all platform targets + deb packages via `just`, and publishes a |
| 52 | +draft GitHub release with `softprops/action-gh-release`. `.github/workflows/build.yml` runs the same |
| 53 | +`just all` build on every push as a CI check (no tests to run). |
0 commit comments