tools/CLAUDE.md
neil 21202c2ca3 Keep server details out of the repo; deploy target comes from HOST or .deploy.env
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 01:19:28 -08:00

47 lines
3.3 KiB
Markdown

# CLAUDE.md
See `README.md` for what this is and how it's deployed. This file is how to extend it.
## Adding a tool
1. **Front end:** create `app/static/js/tools/<id>.js` exporting `mount(root, ctx)`:
- append the UI to `root`; start with `el('div.tool', {}, toolbar, …)` so it fills the view
(`.toolbar`, `.btn`, `.segmented`, `.pane`, `.statusbar` are the shared building blocks in
`css/style.css`);
- `ctx.rest` is the path after the tool's own (`/paste/abc` → `abc`), `ctx.query` the search
params, `ctx.navigate(path)` client-side navigation, `ctx.setTitle(text)` the top-bar suffix;
- return a cleanup function if you add global listeners or timers.
2. **Register it** in `app/static/js/registry.js` (id, name, path, category, icon, description,
`load`). The sidebar, home grid, `/`-search and router all come from that list. Add a new
category to `CATEGORY_ORDER` if it should not sort last.
Optional: `opensPaste: true` if the tool loads `?paste=<id>` (it then shows in a paste's
"Open in" menu; call `markRead(id)` from `paste-state.js` when it does, and don't overwrite the
local draft), and `badge: () => number` for a count on its nav item (dispatch `tools:badges`
on `window` when it changes). Share buttons use `createPaste(text, { share: true })`.
3. **Server side, only if needed:** add `app/tools/<id>.py` exposing `router` (a FastAPI
`APIRouter`) with routes under `/api/<id>/…`, and optional `async startup()`/`shutdown()`.
`main.py` mounts every module in `tools/` automatically. Persistent files go under
`$DATA_DIR` (`/data`, bind-mounted from `/opt/tools/data`). Keep it light: it shares a small
server with other workloads (container capped at 192 MB / 0.5 CPU).
4. Prefer running in the browser. Vendor any library into `static/vendor/` (pinned version in
the filename comment or README); the CSP allows scripts from `'self'` only, no CDNs.
## Conventions
- Persist per-browser state with `store` from `ui.js` (namespaced, never throws), and flush
debounced saves on leave (`onLeave`, `debounce(...).flush`).
- Never render user content as HTML without DOMPurify (the Markdown preview) or `textContent`.
- `ui.js` has the shared pieces: `el`, `segmented`, `modal`, `dropdown`, `toast`, `copyText`,
`copyRich` (HTML + plain text), `downloadText`, `openTextFile`, `tabIndent`, `store`, `debounce`.
Note `el()` sets string values as attributes; assign `innerHTML` with `Object.assign`.
- Formatters in `code-format.js` must stay whitespace-only for the built-in languages. After
changing one, run the tests and also the formatter over a large set of real files, checking that
`squash(out) === squash(src)` and that formatting is idempotent (that's how the current rules
were validated: 1000+ Kotlin/Java/C/C++ files, 0 problems).
- Heavy libraries (Prettier, sql-formatter) are loaded with `import()` on first use only; keep
the initial page light (the server's upstream is slow).
- Run `node --test tests/` and check the page in a browser before deploying; `deploy.sh` refuses
to deploy if the tests fail.
- **This repo is public. Never commit anything about the machines it runs on**: no IPs, SSH
targets, hostnames, providers, network layout or tunnel setup. The deploy target lives in the
gitignored `.deploy.env`; operational notes belong in the private homelab docs.