跳到主要内容
食谱编号214

排版食谱 · 第13章 · CMS集成

校对符号:删除、替换和插入

在 postext-viewer 中把编辑的改动画在排好的小说页面上:删除线、写在上方的替换文字和插入符号,都来自 Markdown 范围。

本页内容
输出
Canvas
难度
中级
Postext
已用Postext 1.103.1测试
需要≥ 1.103.0
许可证
更新于2026年12月4日
代码MIT · 文本CC BY 4.0
一校样,页面上画了五处改正:划掉的词、写在行上方的替换词和一个插入符,每处都在旁边的列表里写明。
一校样,页面上画了五处改正:划掉的词、写在行上方的替换词和一个插入符,每处都在旁边的列表里写明。
  • 英文样例:尚无中文版本
  • 成品尺寸130 × 200 mm
  • 1栏
  • Libre Caslon Text 10/16
  • 1页
  • 难度
  • Postext 1.103.1
  • 排版用时60 ms
  • 107行代码

简单来说

一篇短篇小说的第一页,上面有编辑的改动,像校对工具那样显示:划掉的字词、写在旧词上方的新词,以及缺字处的一个小符号。旁边的列表逐条列出。

成品一览

短篇小说 Platform Two 的首页,带着文字编辑在一校中做的改动。一个重复的词和多余的 very 用红线划掉;lain 和 recieved 也被划掉,正确的词写在正上方的小标签里;行下的绿色符号标出缺少 in 的地方。右边的列表列出同样五条改动,每条显示现有的字词和替换的内容。在页面上点一个标记,或用 Tab 键移过去再按 Enter,它的条目就会被选中。

这道食谱解答

  • 怎样在排好的页面上显示校对符号(删除、替换、插入)?

简短回答

script.js · 第107–116行在完整代码中
const view = createViewer(document.getElementById('viewer'), {
  document: doc, markdown, zoom: 'fit-width', labels: L.viewer,
  annotations: edits, // strike, replace (text over the struck words) and insert (a caret)
});
const entries = new Map(edits.map((e) => [e.id, entry(e)]));
const pick = (id) => {
  for (const [key, li] of entries) li.classList.toggle('active', key === id);
};
view.on('annotationclick', ({ id }) => pick(id)); // pointer, Enter or Space
pick('e2');

用料

类型
Libre Caslon Text(SIL OFL 1.1)
素材
无:所有图片都用代码绘制

做法

#1 · 行间留出空间

script.js · 第30–51行在完整代码中
const config = {
  locale: t({ en: 'en-gb', es: 'es' }),
  colorPalette,
  page: { sizePreset: 'custom', width: mm(130), height: mm(200), dpi: 150,
    margins: { top: mm(22), bottom: mm(24), left: mm(18), right: mm(18) } },
  layout: { layoutType: 'single' },
  bodyText: { fontFamily: 'Libre Caslon Text', fontSize: pt(10), lineHeight: pt(16),
    color: col('ink'), boldColor: col('ink'), italicColor: col('ink'), referenceColor: col('ink'),
    textAlign: 'justify', firstLineIndent: mm(5), indentAfterHeading: false,
    hyphenation: { enabled: true }, optimalLineBreaking: true,
    avoidWidows: true, avoidOrphans: true, avoidRunts: true },
  headings: { fontFamily: 'Libre Caslon Text', color: col('accent'), fontWeight: 400, levels: [
    { level: 1, fontSize: pt(22), lineHeight: pt(28), italic: true,
      marginTop: pt(16), marginBottom: pt(24) },
  ] },
  header: { elements: [] },
  footer: { elements: [
    { kind: 'text', id: 'folio', content: '{pageNumber}', fontFamily: 'Libre Caslon Text',
      fontSize: pt(9), color: col('accent'), align: 'center',
      placement: { anchor: { to: 'page', edge: 'bottom-center' }, offset: { y: mm(-12) } } },
  ] },
};

替换和插入的标签按正文字号的 0.62 排,立在所改那一行的正上方,也就是行距里。10 pt 的字配 16 pt 的行距,空间足够,标签只遮住上一行很少的部分;行距紧时,标签带有纸色的底,两行都能看清。

#2 · 把每条改动存为范围

script.js · 第81–95行在完整代码中
// [kind, a quote that finds the place, the words marked in it, the new text]
// A tool stores the offsets; the pen finds them from quotes so that both editions run.
const edits = t({
  en: [['strike', 'for for forty', 'for '], ['replace', 'had lain her', 'lain', 'laid'],
    ['strike', 'the very last', 'very '], ['replace', 'had recieved a', 'recieved', 'received'],
    ['insert', 'nothing the dark', 'the dark', 'in ']],
  es: [['replace', 'habían dos faroles', 'habían', 'había'],
    ['replace', 'había recivido', 'recivido', 'recibido'], ['insert', 'segura que', 'que', 'de '],
    ['strike', 'todo el el valle', 'el '], ['strike', 'subió arriba', ' arriba']],
}).map(([kind, quote, words, text], i) => {
  const start = markdown.indexOf(quote) + quote.indexOf(words);
  // An insertion marks a point: the range is empty, before the words that follow it.
  const end = kind === 'insert' ? start : start + words.length;
  return { id: `e${i + 1}`, kind, range: { start, end }, text };
});

删除和替换标出要改的字词,替换还带着新的 text。插入标的是一个点,所以范围是空的(start === end),符号画在该偏移处那一行的下方。接受改动时 applyEdits 接收的,也正是这些记录。

#3 · 画出来,并监听点击

代码就是上面的简短回答。查看器按每一行自身的方向排布标记,所以删除线穿过字母中部,替换文字立在原词之上,在从右到左和竖排的页面上也一样。annotationclick 在指针点击时触发,在标记上按 Enter 或空格时也触发,因为每个标记都是一个可用键盘操作的按钮,名为“Replacement: laid”。

完整食谱

// ═══ Postext Cookbook · Nº 214 · Proofreading marks: strike, replace and insert ══════
// https://postext.dev/en/cookbook/proofreading-marks-on-pages
// Code: MIT · Text: original (CC BY 4.0) · Pictures: none
// Fonts: Libre Caslon Text (SIL OFL 1.1) · Needs postext ≥ 1.103.0
//
// The first page of a short story with an editor's corrections drawn on it, as a proofing
// tool shows them: struck words, a replacement written over the words it replaces, and a
// caret where a word goes in. The list beside the page is the same corrections; pressing a
// mark, with the pointer or the keyboard, picks its entry.
import { buildDocumentWithFonts, renderPageToCanvas } from 'https://esm.sh/postext';
import { createViewer } from 'https://esm.sh/postext-viewer';

const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'proofreading-marks-on-pages';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: semantic colours, every one linked by id
const palette = {
  ink: '#1d1b19', // text
  accent: '#5b4a8a', // a dusk violet: the story's title and the folios
};
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

// #region page: a short-story page with generous leading, room for the marks between lines
const config = {
  locale: t({ en: 'en-gb', es: 'es' }),
  colorPalette,
  page: { sizePreset: 'custom', width: mm(130), height: mm(200), dpi: 150,
    margins: { top: mm(22), bottom: mm(24), left: mm(18), right: mm(18) } },
  layout: { layoutType: 'single' },
  bodyText: { fontFamily: 'Libre Caslon Text', fontSize: pt(10), lineHeight: pt(16),
    color: col('ink'), boldColor: col('ink'), italicColor: col('ink'), referenceColor: col('ink'),
    textAlign: 'justify', firstLineIndent: mm(5), indentAfterHeading: false,
    hyphenation: { enabled: true }, optimalLineBreaking: true,
    avoidWidows: true, avoidOrphans: true, avoidRunts: true },
  headings: { fontFamily: 'Libre Caslon Text', color: col('accent'), fontWeight: 400, levels: [
    { level: 1, fontSize: pt(22), lineHeight: pt(28), italic: true,
      marginTop: pt(16), marginBottom: pt(24) },
  ] },
  header: { elements: [] },
  footer: { elements: [
    { kind: 'text', id: 'folio', content: '{pageNumber}', fontFamily: 'Libre Caslon Text',
      fontSize: pt(9), color: col('accent'), align: 'center',

组合好的script.js可以直接运行:把它粘贴到任何页面的模块脚本中,或在CodePen上打开这道食谱。 GitHub上的食谱文件夹 ↗ (在新标签页中打开)

变化

#接受全部改动

从 https://esm.sh/postext/annotate 导入 applyEdits,把改动写进 Markdown,再显示新的版面。

 view.on('annotationclick', ({ id }) => pick(id)); // pointer, Enter or Space
+const { markdown: fixed } = applyEdits(markdown, edits.map((e) => ({ ...e.range, text: e.text ?? '' })));
+view.setDocument(await buildDocumentWithFonts({ markdown: fixed }, config, kitFonts(FONTS)), fixed);
+view.setAnnotations([]);

#用书的字体排标签

若不另行指定,标记自己的文字用 system-ui 排。

   document: doc, markdown, zoom: 'fit-width', labels: L.viewer,
+  marks: { fontFamily: 'Libre Caslon Text' },

致谢

文本
原创文字, CC BY 4.0
字体
Libre Caslon Text (SIL OFL 1.1)