# Integration: anchors and edits

> textAnchor and resolveAnchor keep a comment on its words while the Markdown is rewritten (exact, moved, ambiguous, orphaned); applyEdits and mapOffset write corrections into the Markdown and carry the other anchors along

- HTML version: https://postext.dev/en/docs/integration-anchors
- Last updated: 2026-12-04
- Reading time: 7 min
- Other languages: [es](https://postext.dev/es/docs/integration-anchors.md), [ca](https://postext.dev/ca/docs/integration-anchors.md), [de](https://postext.dev/de/docs/integration-anchors.md), [el](https://postext.dev/el/docs/integration-anchors.md), [fr](https://postext.dev/fr/docs/integration-anchors.md), [it](https://postext.dev/it/docs/integration-anchors.md), [pt](https://postext.dev/pt/docs/integration-anchors.md), [ru](https://postext.dev/ru/docs/integration-anchors.md), [zh](https://postext.dev/zh/docs/integration-anchors.md), [ja](https://postext.dev/ja/docs/integration-anchors.md), [ar](https://postext.dev/ar/docs/integration-anchors.md)

## In short

This page is for developers. A comment points at some words, but the author keeps changing the text, so the words move. The page shows how to save a comment with the words it covers and a few words on each side. Later the system finds those words again, or says plainly that it cannot. It also shows how to write corrections into the text so the other comments stay in place.

**Store an anchor, not an offset: the words of a comment and some context, found again in the next revision of the text.**

An offset is only good for the text it was taken from. If the author adds a
sentence at the top of the chapter, every comment below points 80 characters
too early. A content system has to carry its comments through each revision,
and two calls in `postext/annotate` do that.

## Making an anchor: `textAnchor`

```js
import { textAnchor } from 'postext/annotate';

const range = { start: 52, end: 61 }; // from a selection in the viewer
const anchor = textAnchor(markdown, range);
// { start: 52, end: 61, text: 'the hives', before: '…The keeper walked ', after: ' at dawn, lifting each…' }
```

The anchor keeps the range, the text it covered, and up to 32 characters on
each side (`{ context: 64 }` for more). It is plain JSON: store it in the
comment's row as it is. A context never splits a surrogate pair, so emoji and
the characters outside the Basic Multilingual Plane survive a round trip
through a database.

An insertion point is an anchor with `start === end` and empty `text`: it is
found by its context alone.

## Finding it again: `resolveAnchor`

```js
import { resolveAnchor } from 'postext/annotate';

const found = resolveAnchor(newMarkdown, anchor);
switch (found.status) {
  case 'exact':     /* same place, same text, same context */ break;
  case 'moved':     /* found.start, found.end: the one place it fits */ break;
  case 'ambiguous': /* found.candidates: several places, ask a person */ break;
  case 'orphaned':  /* its text is gone */ break;
}
```

The search goes from strict to loose and stops at the first step that gives
an answer:

1. In place: the text and its whole context are still at the stored offsets. `exact`.
2. The text with its whole context, anywhere in the file. One place: `moved`.
3. The same with shorter context, halved each time down to 4 characters a side (`minContext`). One place: `moved`.
4. The text alone. One place: `moved`.

At any step, two or more places give `ambiguous` with the candidates, and
the search stops there. `resolveAnchor` never guesses between two places
that fit equally well: a comment on "the queen" in a chapter where "the
queen" appears six times stays ambiguous until the context tells them apart.
When the text is nowhere, the answer is `orphaned`.

What to do with each answer is the host's choice. Common ones:

| Status | A typical host |
| --- | --- |
| `exact` | Shows the comment. |
| `moved` | Shows it at the new range and stores the new anchor (`textAnchor(newMarkdown, found)`). |
| `ambiguous` | Lists it as "needs a place" with the candidates highlighted. |
| `orphaned` | Keeps it in the thread, marked as detached from the text. |

## Writing changes: `applyEdits`

When the host accepts corrections and writes them into the Markdown, the
anchors of the other comments move with the text, and no search is needed:

```js
import { applyEdits } from 'postext/annotate';

const edits = [
  { start: 87, end: 92, text: 'young bees' },  // replace "brood"
  { start: 140, end: 140, text: ' gently' },   // insert
];
const { markdown: next, map, anchors } = applyEdits(markdown, edits, storedAnchors);
// anchors[i] = { anchor, touched }: storedAnchors[i] with its offsets, text and context
// as they are in `next`; touched when an edit fell inside it
```

- Edits are given in offsets of the text before any of them; their order does not matter. Overlapping edits throw a `RangeError`: two corrections that overlap need a person to choose.
- An anchor before or after every edit keeps its words and moves by the length the edits added or removed.
- An anchor an edit falls inside is `touched`: it then covers the whole changed stretch, and its `text` is what is there now. The host decides whether the comment still applies.
- An insertion exactly at an anchor's edge stays outside it.

`map` turns any other offset of the old text into the new one:

```js
import { mapOffset } from 'postext/annotate';
mapOffset(map, 200);           // where offset 200 is now
mapOffset(map, 140, 'before'); // at an insertion point: before the inserted text
```

Use it for the viewer's selection, a reader's place, or offsets kept outside
anchors.

## Revisions the host did not make

`applyEdits` is for changes the host writes. When the Markdown changed
elsewhere (an author's editor, a Git merge, an import), there is no edit
list: call `resolveAnchor` on every stored anchor against the new text. Each
anchor costs a few `indexOf` passes over the file.

## Across chapters

Anchors count in one chapter's Markdown. A host that keeps comments for a
whole book stores the chapter next to each anchor and resolves it against
that chapter's file. When the viewer shows a chapter that carries text from
another one, the `chapter` of `caretAt` and of the selection says which file
the offsets are in (see [Text on the page](https://postext.dev/en/docs/integration-geometry.md#chapters-and-books)).

## Examples in the Cookbook

### Anchors and edits

- [Nº 224 A reader's notebook: notes on what they select](https://postext.dev/en/cookbook/reader-notebook.md)
- [Nº 217 Anchors through a revision: moved, ambiguous, orphaned](https://postext.dev/en/cookbook/anchors-through-a-revision.md)
- [Nº 218 Applying accepted edits and carrying the other notes](https://postext.dev/en/cookbook/applying-accepted-edits.md)
- [Nº 223 A reader's highlighter: a passage and a colour](https://postext.dev/en/cookbook/reader-highlighter.md)
