Metadata-Version: 2.5
Name: md2site-cli
Version: 0.2.0
Summary: Compile a folder of Markdown into a static, offline-capable HTML docs site.
Project-URL: Homepage, https://github.com/Jayfraggs/md2site
Project-URL: Repository, https://github.com/Jayfraggs/md2site
Project-URL: Issues, https://github.com/Jayfraggs/md2site/issues
Author: Jayfraggs
License-Expression: MIT
License-File: LICENSE
License-File: THIRD_PARTY_LICENSES.md
Classifier: Development Status :: 3 - Alpha
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Documentation
Classifier: Topic :: Software Development :: Documentation
Classifier: Topic :: Text Processing :: Markup :: Markdown
Requires-Python: >=3.11
Requires-Dist: jinja2>=3.1
Requires-Dist: markdown-it-py>=3
Requires-Dist: mdit-py-plugins>=0.4
Requires-Dist: pygments>=2.17
Requires-Dist: watchdog>=4
Provides-Extra: dev
Requires-Dist: build; extra == 'dev'
Requires-Dist: pytest-cov; extra == 'dev'
Requires-Dist: pytest>=8; extra == 'dev'
Requires-Dist: ruff; extra == 'dev'
Description-Content-Type: text/markdown

# md2site

Turn a folder of Markdown into a static, offline-capable HTML docs site — with build-time link checking for humans and AI coding agents.

[![CI](https://github.com/Jayfraggs/md2site/actions/workflows/ci.yml/badge.svg)](https://github.com/Jayfraggs/md2site/actions/workflows/ci.yml)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
![Python 3.11 | 3.12 | 3.13](https://img.shields.io/badge/python-3.11%20%7C%203.12%20%7C%203.13-blue)

## How it works

<p align="center">
  <img src="docs/assets/md2site-overview.jpg" width="900"
       alt="Hand-drawn engineering-style sketch. On the left, a folder labelled docs/ and a stack of Markdown pages labelled raw docs and .md. In the middle, a pipe feeding a gear-and-crank machine labelled transform, with a small terminal prompt showing $_. On the right, a tilted browser window labelled live HTML showing a purple sidebar, an article column, a dark syntax-highlighted code block, a Mermaid flowchart, and a search bar with a magnifying glass. The word md2site is written at the bottom.">
</p>
<p align="center"><em>md2site at a glance — sketch by Jayfraggs.</em></p>

Read the sketch left to right:

1. **`docs/` → "raw docs": your Markdown stays the source of truth.** The folder and the stack of `.md` pages on the left are ordinary files with headings, lists and fenced code — the things you commit and the things AI agents write. Any folder depth, any filenames; nothing is converted into a proprietary format.
2. **"transform": a small build machine.** The pipe feeds Markdown into the gears. The terminal prompt (`$_`) is the CLI — `md2site build` — and the gears are the pipeline stages: **scan → parse → resolve links → render**. The hand crank is `md2site watch`, which re-runs the machine whenever a file changes.
3. **"live HTML": a themed reading experience, right in the browser.** The window on the right shows what comes out:
   - **Purple sidebar** — the navigation tree, generated from your folder structure with zero routing config (the purple is the default accent colour and is configurable).
   - **Article column** — one standalone HTML page per Markdown file, titled from its first `# H1`, with a sticky "On this page" outline.
   - **Dark code panel** — syntax highlighting done at build time, so no highlighting JavaScript ships to readers.
   - **Mermaid panel** — ` ```mermaid ` blocks become diagrams, loaded only on pages that use them.
   - **Search bar and magnifier** — instant offline full-text search (Ctrl/Cmd+K).
   - Everything opens straight from disk (`file://`), from `localhost` during `watch`, or from any static host.
4. **The blueprint dimension lines** (the arrows labelled `m` and `l`) are purely decorative — a nod to an engineering drawing. They don't correspond to any setting.

## Why md2site

- **Authoring vs. reading friction:** AI agents and developers write Markdown fast, while dense multi-file Markdown is tiring to read raw.
- **No heavy generator toolchain:** No Node runtime, no framework boilerplate, no rigid directory layout — zero-config builds work out of the box.
- **Dead local references solved:** Relative `.md` cross-links, Obsidian `[[wikilinks]]`, `#anchors`, doc assets, and links to repo source files outside `docs/` all resolve cleanly for `file://`, editor URIs, or remote repository URLs.
- **Agent-friendly reference checker:** `md2site check --json` returns structured diagnostics (`file`, `line`, `code`, `suggestion`) so an agent can fix broken references in the same loop that wrote them.

## Features

- **Navigation:** Recursive sorted scan, front matter `order`/`hidden`/`draft` support, numeric-prefix stripping, acronym-aware title humanization, collapsible `<details>/<summary>` sidebar (works without JS), breadcrumbs, and document-order Previous/Next links.
- **Links & references:** Relative/root-relative `.md` links, directory `index.md`/`README.md` resolution, `[[Wikilinks]]` and `![[embeds]]` with nearest-match tie-breaking, heading `#anchor` validation, case-mismatch detection (`MD2S006`), repo source-file modes (`relative`, `editor`, `remote`, `auto`), and opt-in inline-code path autolinking.
- **Reading experience:** Flicker-free light/dark toggle (`localStorage` + `prefers-color-scheme`), configurable CSS tokens, build-time Pygments syntax highlighting with one-click Copy buttons, GitHub alerts (`> [!NOTE]`–`[!CAUTION]`), task lists, footnotes, scrollable tables, responsive mobile drawer, and print stylesheet.
- **Search & diagrams:** Offline `assets/search-index.js` loaded lazily on `Ctrl/Cmd+K` with Unicode prefix matching and `<mark>` highlights; vendored `mermaid@12.3.0` loaded only on pages containing ` ```mermaid ` fences and re-rendered on theme switch.
- **Agent integration:** Bundled `SKILL.md` (`md2site init --with-skill`), `md2site hook` post-edit stdin validator, `md2site check --json`, and `md2site watch` with Server-Sent Events live reload.

## Install

Note: the PyPI distribution name is `md2site-cli` (the `md2site` name on PyPI belongs to an unrelated project), while the CLI command and Python package remain `md2site`. (`md2site-cli` will be published to PyPI once trusted publishing is enabled; until then, install directly from Git.)

```bash
pipx install git+https://github.com/Jayfraggs/md2site.git
```

Or run without installing:

```bash
uvx --from git+https://github.com/Jayfraggs/md2site md2site build
```

## Quick start

```text
$ md2site init docs
Initialized md2site in /path/to/repo/docs
  created /path/to/repo/docs/md2site.config.json
  created /path/to/repo/docs/custom.css
  ensured /path/to/repo/docs/_site
  added docs/_site/ to /path/to/repo/.gitignore

$ md2site build docs
Built 2 pages (8 written, 0 unchanged, 0 removed) in 40 ms [0 errors, 0 warnings]

$ md2site check docs --json
{
  "version": 1,
  "summary": {
    "pages": 2,
    "errors": 0,
    "warnings": 0,
    "duration_ms": 5
  },
  "diagnostics": []
}
```

Open `docs/_site/index.html` directly in your browser (`file://`), or start live reload while editing:

```bash
md2site watch docs --open
```

## Commands

| Command | Arguments & flags | Description |
|---|---|---|
| `init` | `[PATH] [--force] [--with-skill]` | Create `md2site.config.json`, `custom.css`, `_site/`, `.gitignore` entry, and optional `SKILL.md` |
| `build` | `[PATH] [--config FILE] [--mode relative\|editor\|remote\|auto] [--out DIR] [--strict] [--json] [--quiet] [--drafts]` | Build the static HTML documentation site |
| `check` | `[PATH] [--config FILE] [--strict] [--json]` | Resolve and validate links without writing files |
| `watch` | `[PATH] [--config FILE] [--host 127.0.0.1] [--port 8000] [--open]` | Serve `_site/` locally with SSE live reload and incremental rebuilds |
| `hook` | *(reads JSON on stdin)* | Post-edit agent hook; prints diagnostics to stderr and exits `2` on issues |

Run `md2site --version` or `md2site <command> --help` for full details.

## Configuration

`md2site` works with zero configuration. Place an optional `md2site.config.json` inside your docs directory to customize behavior (see [docs/configuration.md](docs/configuration.md)):

| Key | Default | Purpose |
|---|---|---|
| `site_title` | `"Project Documentation"` | Site title shown in header, browser title, and landing page |
| `docs_dir` | `"."` | Source Markdown root relative to the config file |
| `output_dir` | `"_site"` | Generated HTML output directory |
| `repo_root` | `"auto"` | Ancestor `.git` root (or config dir) used to resolve source links |
| `exclude` | `[]` | POSIX glob patterns excluded from scanning |
| `source_link_mode` | `"relative"` | One of `relative`, `editor`, `remote`, or `auto` |
| `wikilinks` | `true` | Enable `[[Wikilinks]]` and `![[embeds]]` |
| `max_highlight_bytes` | `262144` | Max fence bytes for Pygments highlighting (`0` = unlimited) |
| `nav` | `null` | Optional explicit sidebar ordering override (`null` = auto tree) |
| `search.enabled` | `true` | Emit offline `assets/search-index.js` and enable `Ctrl/Cmd+K` modal |
| `search.max_index_bytes` | `0` | Optional byte budget for `assets/search-index.js` (`0` = unlimited) |

<details>
<summary>Full default md2site.config.json</summary>

```json
{
  "site_title": "Project Documentation",
  "docs_dir": ".",
  "output_dir": "_site",
  "repo_root": "auto",
  "exclude": [],
  "source_link_mode": "relative",
  "remote_repo_url": "",
  "remote_branch": "main",
  "editor_scheme": "vscode",
  "autolink_code_paths": false,
  "wikilinks": true,
  "allow_html": true,
  "mermaid": true,
  "max_highlight_bytes": 262144,
  "nav": null,
  "search": { "enabled": true, "body_chars": 6000, "max_index_bytes": 0 },
  "theme": {
    "default_mode": "system",
    "accent_color": "#7c3aed",
    "accent_color_dark": "#a78bfa",
    "code_theme_light": "default",
    "code_theme_dark": "github-dark",
    "light": { "bg_primary": "#ffffff", "bg_sidebar": "#f8fafc", "text_primary": "#0f172a" },
    "dark":  { "bg_primary": "#0b0f19", "bg_sidebar": "#111827", "text_primary": "#f1f5f9" },
    "font_sans": "system-ui, -apple-system, sans-serif",
    "font_mono": "'JetBrains Mono', 'Fira Code', monospace"
  }
}
```

</details>

## How links are resolved

| Source syntax | Target | Output |
|---|---|---|
| `[x](./a.md#h)` | `.md` in docs | Relative `.html#h`; anchor validated |
| `[x](dir/)` | Directory with `index.md` / `README.md` | `dir/index.html` |
| `[[Note]]`, `[[Note#H]]`, `[[Note\|Label]]`, `[[folder/Note]]` | Page | Relative HTML link (path → stem → title → alias) |
| `![[img.png]]`, `![](./img.png)` | Asset in docs | Copied to mirrored path, relative URL |
| `[x](../src/server.py#L42)` | File outside docs, inside `repo_root` | Rewritten per `source_link_mode` |
| `![](../assets/logo.png)` | Image outside docs | Copied to `_site/_external/<repo-relative path>` |
| `http(s)://`, `mailto:`, `tel:` | External | Untouched; `http(s)://` gets `class="external"` and `rel="noopener"` |
| `#anchor` | Same page | Validated against heading IDs |
| `/abs/path` | Root-relative | Treated as relative to `docs_dir` |

### `source_link_mode`

- `relative` — exact relative path back to the file on disk (e.g. `../../src/server.py#L42`).
- `editor` — IDE URI (e.g. `vscode://file/<abs-path>:42`).
- `remote` — repository blob URL (`{remote_repo_url}/blob/{remote_branch}/src/server.py#L42`).
- `auto` — emits `href` as `relative` plus `data-remote`, swapping to remote on non-local HTTP hosts.

### Diagnostics codes

| Code | Meaning |
|---|---|
| `MD2S001` | Broken Markdown link (target `.md` not found) or missing explicit `nav` target |
| `MD2S002` | Broken file/asset link |
| `MD2S003` | Ambiguous wikilink (resolved to nearest) |
| `MD2S004` | Unresolved wikilink |
| `MD2S005` | Broken `#anchor` heading fragment |
| `MD2S006` | Case mismatch (works on case-insensitive FS, breaks on Linux) |
| `MD2S007` | Target escapes `repo_root` |
| `MD2S008` | Output path or asset collision |
| `MD2S009` | Bad front matter or invalid UTF-8 bytes |
| `MD2S010` | `remote`/`auto` mode without `remote_repo_url` |
| `MD2S011` | Cross-filesystem `relative` repo-file fallback when `--out` cannot reach `repo_root` |

## Works offline, from `file://`

Browsers enforce strict restrictions when opening HTML files straight from disk:

- **No `fetch()` of local JSON:** search emits `assets/search-index.js` (`window.__MD2SITE_INDEX__=[...];`) and injects it via a classic `<script>` tag instead of fetching JSON.
- **No ES modules or `<base>` tags:** scripts use classic `<script defer>` and every link/asset uses explicit relative paths (`./`, `../`).
- **No bare directory links:** folder links always resolve to explicit `…/index.html` files.
- **Zero runtime network requests:** system font stacks by default, build-time Pygments CSS, and vendored `mermaid.min.js`.

## For AI agents

- Install the skill with `md2site init docs --with-skill` (copies [SKILL.md](src/md2site/skill/SKILL.md) to `.claude/skills/md2site/SKILL.md`).
- Or add a post-edit hook (`md2site hook`) and `AGENTS.md` snippet — see [docs/agents.md](docs/agents.md).
- Typical agent loop: edit Markdown → `md2site check --json` → fix diagnostics via `suggestion` → `md2site build`.

## Performance

- Cold build of **100 pages** (~2 KB each with Python code blocks, cross-links, and GFM tables): **~468 ms** (0.47 s) on a Linux x86_64 VM (Python 3.12).
- Incremental `watch` re-renders only the edited page and `search-index.js` when headings, metadata, and outgoing link targets are unchanged; unchanged outputs are skipped by content comparison.

## Non-goals

Plugin API, versioned docs, i18n, hosting/deployment tooling, WYSIWYG editing, server-side search, Node build toolchain, MDX, theme marketplace, and built-in PDF export (use browser print).

## Development

```bash
git clone https://github.com/Jayfraggs/md2site.git
cd md2site
python -m venv .venv
.venv/bin/pip install -e ".[dev]"
.venv/bin/ruff check .
.venv/bin/ruff format --check .
.venv/bin/pytest -q --cov=md2site --cov-report=term-missing
.venv/bin/md2site build docs --strict
```

See [tests/README.md](tests/README.md) for fixture and golden snapshot instructions, and [docs/architecture.md](docs/architecture.md) for the pipeline overview.

## Contributing

Issues and pull requests are welcome. Please run `ruff check .`, `ruff format --check .`, and `pytest -q` before submitting changes, and follow [Conventional Commits](https://www.conventionalcommits.org/).

## License

[MIT](LICENSE) © 2026 Jayfraggs. See [THIRD_PARTY_LICENSES.md](THIRD_PARTY_LICENSES.md) for vendored Mermaid license details.

## Credits

Built on [markdown-it-py](https://github.com/executablebooks/markdown-it-py), [mdit-py-plugins](https://github.com/executablebooks/mdit-py-plugins), [Pygments](https://pygments.org/), [Jinja2](https://jinja.palletsprojects.com/), [watchdog](https://github.com/gorakhargosh/watchdog), and [Mermaid](https://mermaid.js.org/).
