# 集成：锚点与修改

> textAnchor和resolveAnchor在Markdown被改写时让评论留在它所指的文字上（exact、moved、ambiguous、orphaned）；applyEdits和mapOffset把修改写进Markdown，并连带移动其他锚点

- HTML版本: https://postext.dev/zh/docs/integration-anchors
- 最后更新: 2026-12-04
- 阅读时间: 6分钟
- 其他语言: [en](https://postext.dev/en/docs/integration-anchors.md), [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), [ja](https://postext.dev/ja/docs/integration-anchors.md), [ar](https://postext.dev/ar/docs/integration-anchors.md)

## 简单来说

本页写给开发者。一条评论指向几个词，可是作者一直在改文字，这些词的位置也跟着变。本页说明怎样保存评论：连同它覆盖的词，以及两侧的几个词。之后系统能再次找到这些词，找不到时也会明确说出来。本页还说明怎样把修改写进文字，同时让其他评论留在原处。

**存锚点，不要存偏移量：评论所指的文字加上一些上下文，在下一版文字中重新找到。**

偏移量只对取得它的那份文字有效。作者在章首加一句话，下面的每条评论就都往前错了80个字符。内容系统必须让评论跟着每一次修订走，`postext/annotate`中的两个调用负责这件事。

## 生成锚点：`textAnchor`

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

const range = { start: 52, end: 61 }; // 来自查看器中的选区
const anchor = textAnchor(markdown, range);
// { start: 52, end: 61, text: 'the hives', before: '…The keeper walked ', after: ' at dawn, lifting each…' }
```

锚点保存范围、范围覆盖的文字，以及两侧各最多32个字符（要更多，用`{ context: 64 }`）。它是普通的JSON，原样存进评论那一行即可。上下文从不拆开代理对，所以emoji和基本多文种平面以外的字符在数据库里存取一遍也不会损坏。

插入点是`start === end`且`text`为空的锚点，只靠上下文来查找。

## 重新找到它：`resolveAnchor`

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

const found = resolveAnchor(newMarkdown, anchor);
switch (found.status) {
  case 'exact':     /* 位置、文字、上下文都没变 */ break;
  case 'moved':     /* found.start、found.end：唯一符合的位置 */ break;
  case 'ambiguous': /* found.candidates：有几个位置，请人来选 */ break;
  case 'orphaned':  /* 文字已经不在了 */ break;
}
```

查找从严到宽，在第一个给出答案的步骤停下：

1. 原位：文字和完整的上下文仍在所存的偏移量处。`exact`。
2. 在整个文件中查找文字加完整上下文。只有一处：`moved`。
3. 同上，但缩短上下文，每次减半，直到每侧4个字符（`minContext`）。只有一处：`moved`。
4. 只查找文字本身。只有一处：`moved`。

任何一步中找到两处或更多，都返回`ambiguous`并附上候选位置，查找就此停止。`resolveAnchor`从不在两个同样符合的位置之间猜测：如果一章里“the queen”出现了六次，针对“the queen”的评论会一直是ambiguous，直到上下文能把它们区分开。文字哪里都找不到时，回答是`orphaned`。

每种回答怎样处理，由宿主系统决定。常见的做法：

| 状态 | 典型的宿主系统 |
| --- | --- |
| `exact` | 显示评论。 |
| `moved` | 在新范围处显示，并存下新锚点（`textAnchor(newMarkdown, found)`）。 |
| `ambiguous` | 列为“待定位”，并高亮各个候选位置。 |
| `orphaned` | 保留在讨论串中，标为已与文字脱离。 |

## 写入修改：`applyEdits`

宿主系统接受修改并把它们写进Markdown时，其他评论的锚点随文字一起移动，不需要查找：

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

const edits = [
  { start: 87, end: 92, text: 'young bees' },  // 替换“brood”
  { start: 140, end: 140, text: ' gently' },   // 插入
];
const { markdown: next, map, anchors } = applyEdits(markdown, edits, storedAnchors);
// anchors[i] = { anchor, touched }：storedAnchors[i]在`next`中的偏移量、文字和上下文；
// 有修改落在它里面时，touched为真
```

- 修改用所有修改之前那份文字的偏移量给出，顺序无关紧要。重叠的修改会抛出`RangeError`：两处重叠的修改需要由人来选择。
- 位于所有修改之前或之后的锚点保留原来的文字，只按修改增删的长度移动。
- 有修改落在里面的锚点标为`touched`：它会覆盖整段改动过的文字，`text`是那里现在的内容。评论是否仍然适用，由宿主系统判断。
- 正好插在锚点边缘的文字留在锚点之外。

`map`把旧文字中的任何其他偏移量换算成新的偏移量：

```js
import { mapOffset } from 'postext/annotate';
mapOffset(map, 200);           // 偏移量200现在的位置
mapOffset(map, 140, 'before'); // 在插入点处：位于插入文字之前
```

查看器的选区、读者读到的位置，或锚点以外保存的偏移量，都可以用它换算。

## 不是由宿主系统做出的修订

`applyEdits`用于宿主系统自己写入的修改。Markdown在别处被改过时（作者的编辑器、一次Git合并、一次导入），没有修改列表：对每个已存的锚点，用新文字调用`resolveAnchor`。每个锚点只需要在文件上做几次`indexOf`。

## 跨章

锚点按一章的Markdown计数。为整本书保存评论的宿主系统，在每个锚点旁边存下章节，并在那一章的文件中解析它。查看器显示的章节里带有另一章的文字时，`caretAt`和选区中的`chapter`说明偏移量属于哪个文件（见[页面上的文字](https://postext.dev/zh/docs/integration-geometry.md#章与书)）。

## 排版食谱中的示例

### 锚点与修改

- [Nº 224 读者的笔记本：给选中的文字写笔记](https://postext.dev/zh/cookbook/reader-notebook.md)
- [Nº 217 跨修订的锚点：原位、移动、歧义或孤立](https://postext.dev/zh/cookbook/anchors-through-a-revision.md)
- [Nº 218 应用已接受的修改，并让其余批注随之移动](https://postext.dev/zh/cookbook/applying-accepted-edits.md)
- [Nº 223 读者的荧光笔：一段文字，一种颜色](https://postext.dev/zh/cookbook/reader-highlighter.md)
