# Comments

Comment on any text in a note, the way you would in a shared document. Comments are kept beside your notes and never inside them, so every note stays plain Markdown that Obsidian can open.

## Using them

- Select text and choose Comment in the toolbar. Or press Cmd+Alt+M (Ctrl+Alt+M on Windows and Linux), or run "Add comment" from the command palette.
- The Comments panel opens with a box to write in. Cmd or Ctrl + Enter saves it. Escape cancels.
- Text with a comment is highlighted. Click a highlight to see its thread. Click a thread to go to its text.
- By keyboard, the panel is the way to every comment. Tab moves through the threads. Each one is a button named after the text it is about, and Enter on it shows that text in the note.
- A comment or reply you started writing waits for you if you open another note. It is still there when you come back, until you close Hatch.
- Each thread can be replied to, resolved, reopened and deleted.
- Resolved threads are hidden until you turn on "Show resolved".
- Comments are signed with your account name when you are signed in, and "You" when you are not.

## Where they are kept

- One file per note, in the vault's hidden folder: `.hatch/comments/<note path>.json`. Comments on `Projects/Cabin.md` are in `.hatch/comments/Projects/Cabin.md.json`.
- Hidden folders are not notes. They are left out of search, the graph and published sites.
- The file syncs and backs up with the rest of the vault.
- Renaming a note in Hatch moves its comments.
- Deleting a note in Hatch sets its comments aside in `.hatch/comments/.deleted/<note path>.json`. "Bring back this note" in History puts them back. After 60 days they are removed, the same window as version history.
- A note with no threads has no file.

```json
{
  "version": 1,
  "threads": [
    {
      "id": "mb2k1x9f3a",
      "anchor": { "quote": "roof", "prefix": "The cabin needs a ", "suffix": " before winter." },
      "resolved": false,
      "messages": [{ "id": "mb2k1xa07c", "author": "Alex", "at": "2026-11-29T10:00:00.000Z", "text": "Metal or shingle?" }]
    }
  ]
}
```

- Fields Hatch doesn't know are kept when it saves.
- A file Hatch can't read is never saved over. The panel says so and leaves it alone.

## Comments from another device

The file can change while the note is open, for example when sync brings in a comment written on your phone. Hatch never saves its own older copy over that.

- Before every save, Hatch reads the file again and puts only your change on top.
- Threads and replies are matched by `id`. Both sides are kept.
- If you resolved or reopened a thread, your choice is saved. Otherwise the file's stays.
- A thread you deleted stays deleted, even if it got a reply elsewhere. A thread deleted elsewhere stays deleted too.
- When sync finishes, when files change on disk, and when you come back to the Hatch window, the open note's comments are read again and the panel updates.

Hatch knows what you changed by keeping the file as it last read or wrote it (`base`) next to the copy with your changes. Nothing extra is written to the file.

## How a comment finds its text

A thread doesn't store a position. It stores the text it is about (`quote`) and about 32 characters before and after it (`prefix`, `suffix`). This is the W3C text quote selector.

Each time the note loads or changes, every thread is looked up again:

1. Find every exact copy of the quote in the note's text.
2. Pick the copy whose surroundings match the saved prefix and suffix best.
3. If nothing around it matches any more, use the first copy.
4. If the quote is gone, the thread is "Detached". It stays in the panel, where it can still be read, resolved or deleted. It attaches again if the text comes back.

The text that is searched is what you read, not the Markdown. Blocks are one per line, formatting marks are left out, and a link to a note counts as its name. Making a word bold never loses a comment.

## In the code

- `packages/editor/src/comments.ts` is the editor side and knows nothing about files.
  - `onComment` and `onCommentClick` in `EditorOptions`. With `onComment` set, the selection toolbar shows the Comment button.
  - `setCommentRanges(view, ranges)` highlights ranges. The editor keeps them in place while you type.
  - `resolveAnchors`, `resolveAnchor`, `makeAnchor`, `anchorFor` and `docText` turn selections into anchors and back.
- `packages/app/src/commentStore.ts` reads, writes, moves and sets aside the files, changes threads, and merges with the file on disk (`mergeComments`).
- `packages/app/src/comments.tsx` has the controller (`store.comments`), the panel and the built-in plugin that registers the panel and commands.
- `store.renameNote` and `store.deleteNote` tell the controller, so the file follows the note. The History panel calls `store.comments.restored` when it brings a note back.
- The store calls `store.comments.diskChanged()` after a sync and when the vault reports changed files.
- Tests: `comments.test.ts` in the editor, `commentStore.test.ts` in the app, and `scripts/e2e-comments.py` in a browser.

## Not done yet

- A note renamed or deleted outside the app (in Finder, say) leaves its comments file where it was.
- If two devices change the same comments file while both are offline, sync treats it like any other file edited in two places. The merge above covers changes that have reached this device.
- People editing a note together see each other's comments once the file reaches them by sync. Threads are not sent through the live session.
- With the pointer you can click a highlight to open its thread. By keyboard you go through the panel. There is no key yet for "open the thread under the cursor".
