Zum Hauptinhalt springen
Rezept Nummer 217

Rezepte · Kapitel 13 · CMS-Integration

Anker nach einer Revision: verschoben, mehrdeutig, verwaist

textAnchor speichert jede Notiz mit ihren Wörtern und Kontext; resolveAnchor findet sie in der nächsten Fassung: exakt, verschoben, mehrdeutig, verwaist.

Auf dieser Seite
Stufe
Mittel
Postext
Getestet mit Postext 1.103.1
Erfordert ≥ 1.103.0
Lizenz
Aktualisiert am 4. Dez. 2026
Code MIT · Text CC BY 4.0
Fünf Notizen zum Entwurf, in der Überarbeitung gesucht: an ihrem Platz (grün), verschoben (bernsteinfarben), mehrdeutig mit einem Rahmen um jeden Kandidaten (violett) und eine verwaiste, deren Wörter fehlen.
Fünf Notizen zum Entwurf, in der Überarbeitung gesucht: an ihrem Platz (grün), verschoben (bernsteinfarben), mehrdeutig mit einem Rahmen um jeden Kandidaten (violett) und eine verwaiste, deren Wörter fehlen.
  • Englisches Beispiel: noch keine deutsche Ausgabe
  • Endformat 135 × 200 mm
  • 1 Spalte
  • Newsreader 10/13,6
  • Archivo
  • 2 Seiten
  • Stufe
  • Postext 1.103.1
  • In 60 ms gesetzt
  • 148 Zeilen Code

Kurz gesagt

Eine Lektorin schrieb fünf Notizen in den Entwurf eines Wanderführers, dann überarbeitete ihn der Autor. Die Seite sucht die Wörter jeder Notiz im neuen Text. Manche stehen noch da, andere sind gewandert, eine passt dreimal, eine ist weg.

Was Sie setzen

Eine Wanderung aus Walks from the Valley Station, einem Führer auf einer Seite von 135 × 200 mm. Eine Lektorin hat fünf Notizen in den ersten Entwurf geschrieben; danach hat der Autor einen Satz ergänzt, einen umgeschrieben und einen gestrichen. Der Pen sucht jede Notiz im überarbeiteten Text und zeigt die Antwort an zwei Stellen. Die Liste rechts gibt jeder Notiz ihren Status: eine an ihrem Platz, zwei verschoben, eine mehrdeutig zwischen drei Zauntritten, eine verwaist. Die Seiten links tragen die Notizen, die überleben, nummeriert am Rand, und umrahmen die drei Stellen, die die mehrdeutige meinen könnte.

Dieses Rezept beantwortet

  • Wie bleiben Kommentare an ihrer Stelle, wenn der Text überarbeitet wird?

Die kurze Antwort

script.js · Zeilen 163–179im vollständigen Code
const COLOR = { exact: '#1f7a45', moved: '#b0640a', ambiguous: '#7b4cc4' };
const found = NOTES.map((note) => resolveAnchor(markdown, note.anchor));
const marks = NOTES.flatMap((note, i) => {
  const at = found[i];
  const label = String(i + 1);
  // In place or moved: one range, the note's mark with its number in the margin.
  if (at.status === 'exact' || at.status === 'moved') {
    return [{ id: note.id, kind: 'note', range: at, label, color: COLOR[at.status] }];
  }
  // Ambiguous: every place the words fit, boxed, for a person to choose.
  if (at.status === 'ambiguous') {
    return at.candidates.map((range, k) => ({ id: `${note.id}.${k}`, kind: 'box', range,
      color: COLOR.ambiguous }));
  }
  return []; // orphaned: the words are gone, the note stays in the thread
});
view.setAnnotations(marks);

Zutaten

Schrift
Newsreader, Archivo (SIL OFL 1.1)
Dateien
Keine: Jedes Bild wird im Code gezeichnet

Zubereitung

#1 · Einen Anker speichern, keine Position

script.js · Zeilen 121–144im vollständigen Code
// [the words to find in the draft, the words noted, the note]. A CMS reads the anchors from
// its database; here they are made on the draft so the pen runs on its own.
const NOTES = t({
  en: [
    ['follow the wall up the field', 'the wall', 'Rebuilt last winter: does it reach the top?'],
    ['a cairn that walkers have built', 'a cairn', 'A photograph of the cairn would help here.'],
    ['Cross the stile at', 'the stile', 'Which way does this stile face?'],
    ['the bog on either side is deeper', 'the bog on either side is deeper',
      'Rangers say knee-deep at most. Too alarming?'],
    ['The stepping stones at the ford', 'The stepping stones', 'Give the detour in minutes.'],
  ],
  es: [
    ['sigue el muro prado arriba', 'el muro', '¿Lo rehicieron este invierno? ¿Sigue hasta arriba?'],
    ['un hito de piedras que los caminantes', 'un hito de piedras', 'Aquí iría bien una foto.'],
    ['Salta la escalera del', 'la escalera', '¿Hacia dónde da esta escalera?'],
    ['la turbera de los lados es más honda', 'la turbera de los lados es más honda',
      'Los guardas dicen que no pasa de la rodilla.'],
    ['Las piedras del vado resbalan', 'Las piedras del vado', 'Da el rodeo en minutos.'],
  ],
}).map(([around, words, body], i) => {
  const start = earlier.indexOf(around) + around.indexOf(words);
  const anchor = textAnchor(earlier, { start, end: start + words.length }); // 32 chars a side
  return { id: `n${i + 1}`, body, anchor };
});

textAnchor behält den Bereich, die Wörter darin und 32 Zeichen auf jeder Seite. Das ist einfaches JSON, gedacht für eine Datenbankzeile neben der Notiz. Ein CMS speichert ihn, wenn die Notiz geschrieben wird; der Pen erzeugt die Anker auf dem Entwurf, damit er für sich allein läuft.

#2 · Jeden Anker im neuen Text suchen

Der Code steht in der kurzen Antwort oben. resolveAnchor prüft zuerst die gespeicherten Positionen, dann die Wörter mit ihrem ganzen Kontext irgendwo in der Datei, dann mit kürzerem Kontext und zuletzt die Wörter allein, und hält beim ersten Schritt an, der eine Antwort gibt. Zwischen zwei Stellen wählt es nie: Die Notiz zu „the stile“ hat beim Umschreiben ihren Kontext verloren, und die Wörter allein passen auf drei Zauntritte, also lautet die Antwort ambiguous mit den drei Kandidaten. Was für jeden Status gezeichnet wird, entscheidet die Website.

#3 · Sagen, was mit jeder Notiz geschehen ist

script.js · Zeilen 183–211im vollständigen Code
const STATUS = t({
  en: { exact: 'In place', moved: 'Moved', ambiguous: 'Ambiguous', orphaned: 'Orphaned' },
  es: { exact: 'En su sitio', moved: 'Movida', ambiguous: 'Ambigua', orphaned: 'Huérfana' },
});
const detail = (note, at) => ({
  exact: () => `${at.start}`,
  moved: () => `${note.anchor.start} → ${at.start}`,
  ambiguous: () => at.candidates.map((c) => c.start).join(' · '),
  orphaned: () => t({ en: 'the words are gone', es: 'las palabras ya no están' }),
})[at.status]();
document.getElementById('notes').replaceChildren(...NOTES.map((note, i) => {
  const at = found[i];
  const li = document.createElement('li');
  li.className = at.status;
  li.innerHTML = '<button type="button"><b></b><span class="status"></span><q></q>'
    + '<span class="body"></span><small></small></button>';
  const [n, status, quote, body, small] = li.querySelectorAll('b, .status, q, .body, small');
  n.textContent = String(i + 1);
  status.textContent = STATUS[at.status];
  quote.textContent = note.anchor.text;
  body.textContent = note.body;
  small.textContent = detail(note, at);
  const id = at.status === 'ambiguous' ? `${note.id}.0` : note.id;
  li.firstChild.addEventListener('click', () => view.revealAnnotation(id, { behavior: 'smooth' }));
  li.firstChild.disabled = at.status === 'orphaned';
  return li;
}));
document.getElementById('title').textContent = t({ en: 'Notes on the draft',
  es: 'Notas del borrador' });

Eine verschobene Notiz zeigt ihre alte und neue Position, eine mehrdeutige ihre Kandidaten, und die verwaiste behält ihre Wörter durchgestrichen, damit die Lektorin noch lesen kann, worum es ging. Ein Druck auf eine Notiz scrollt den Betrachter mit revealAnnotation zu ihrem Zeichen.

Das ganze Rezept

// ═══ Postext Cookbook · Nº 217 · Anchors through a revision ═══════════════════════════
// https://postext.dev/en/cookbook/anchors-through-a-revision
// Code: MIT · Text: original (CC BY 4.0) · Pictures: none
// Fonts: Newsreader, Archivo (SIL OFL 1.1) · Needs postext ≥ 1.103.0
//
// An editor left five notes on the first draft of a walking guide; the author then revised the
// text. Each note was stored as an anchor (its words and some context), and each is looked for
// in the new revision: found in place, moved, ambiguous between several places, or orphaned.
// The notes that survive are drawn on the new pages, the others wait in the list for a person.
import { buildDocumentWithFonts, renderPageToCanvas } from 'https://esm.sh/postext';
import { textAnchor, resolveAnchor } from 'https://esm.sh/postext/annotate';
import { createViewer } from 'https://esm.sh/postext-viewer';

const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'anchors-through-a-revision';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: semantic colours, every one linked by id
const palette = {
  ink: '#202421', // text
  accent: '#3d6b45', // moorland green: headings, folios
  muted: '#6b7068', // running heads
};
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
const colorPalette = [
  ...Object.entries(palette).map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } })),
  { id: 'main-color', name: 'accent (defaults)', value: { hex: palette.accent, model: 'hex' } },
];
// #endregion
const LEAD = 13.6; // pt of body leading

const head = (id, content, parity, edge, x, extra = {}) => ({ kind: 'text', id, content, parity,
  pages: 'body', fontFamily: 'Archivo', fontSize: pt(7), fontWeight: 500, letterSpacing: pt(0.8),
  textTransform: 'uppercase', color: col('muted'),
  placement: { anchor: { to: 'page', edge }, offset: { x: mm(x), y: mm(10) } }, ...extra });
const folio = { fontWeight: 600, color: col('accent') };

const config = {
  locale: t({ en: 'en-gb', es: 'es' }),
  colorPalette,
  page: { sizePreset: 'custom', width: mm(135), height: mm(200), dpi: 150,
    margins: { top: mm(19), bottom: mm(20), left: mm(17), right: mm(15), mirror: true } },
  layout: { layoutType: 'single' },
  bodyText: { fontFamily: 'Newsreader', fontSize: pt(10), lineHeight: pt(LEAD),
    color: col('ink'), boldColor: col('accent'), italicColor: col('ink'),
    referenceColor: col('ink'), textAlign: 'justify', firstLineIndent: mm(4),
    indentAfterHeading: false, hyphenation: { enabled: true }, optimalLineBreaking: true,
    avoidWidows: true, avoidOrphans: true, avoidRunts: true },

Das zusammengesetzte script.js läuft unverändert: Fügen Sie es in das Modulskript einer beliebigen Seite ein, oder öffnen Sie das Rezept auf CodePen. Ordner des Rezepts auf GitHub ↗ (öffnet sich in einem neuen Tab)

Varianten

#Mehr Kontext behalten

Ein längerer Kontext unterscheidet Stellen mit denselben Wörtern, scheitert aber früher, wenn der Text um eine Notiz umgeschrieben wird.

-  const anchor = textAnchor(earlier, { start, end: start + words.length }); // 32 chars a side
+  const anchor = textAnchor(earlier, { start, end: start + words.length }, { context: 64 });

#Vor den Wörtern allein aufhören

Mit minContext 8 braucht eine Notiz 8 unveränderte Zeichen auf jeder Seite; kurze Notizen werden so orphaned, statt auf häufige Wörter an anderer Stelle zu passen.

-  const found = NOTES.map((note) => resolveAnchor(markdown, note.anchor));
+  const found = NOTES.map((note) => resolveAnchor(markdown, note.anchor, { minContext: 8 }));

Nachweise

Text
Eigener Text, CC BY 4.0
Schriften
Newsreader (SIL OFL 1.1) · Archivo (SIL OFL 1.1)