Skip to main content
Recipe number 213

Cookbook · Chapter 13 · CMS integration

Highlights from a list of ranges

A review tool's records, each a range of the Markdown and a verdict, painted over the laid-out pages as highlights in three colours with setAnnotations.

On this page
Output
Canvas
Level
Basic
Postext
Tested with Postext 1.103.1
Needs ≥ 1.103.0
Licence
Updated 4 Dec 2026
Code MIT · Text CC BY 4.0
Five claims in a newsletter tinted by verdict: checked in yellow, needs a source in blue, query the author in pink, each also listed beside the pages.
Five claims in a newsletter tinted by verdict: checked in yellow, needs a source in blue, query the author in pink, each also listed beside the pages.
  • Trim 160 × 230 mm
  • 1 column
  • Source Serif 4 10.5/15
  • Source Sans 3
  • 1 page
  • Level
  • Postext 1.103.1
  • Laid out in 46 ms
  • 102 lines of code

In short

A newsletter article on London's plane trees, as a fact-checker sees it: the claims already looked at are shaded yellow, blue or pink by verdict, and a list beside the page names each one. Clicking an entry scrolls to its words.

What you'll build

A newsletter article on the London plane, as the fact-checking desk sees it in a review tool. Five claims carry a verdict: yellow for checked, blue where the writer still owes a source, pink for a question to the author. The highlights sit on the laid-out page, across line breaks where a claim breaks, and the list on the right holds the same five records in reading order. Click an entry and the viewer scrolls its words into view.

This recipe answers

  • How do I draw highlights on the pages from a list of Markdown ranges?

The short answer

script.js · lines 112–128in full code
const view = createViewer(document.getElementById('viewer'), {
  document: doc, markdown, zoom: 'fit-page', labels,
});
view.setAnnotations(claims.map(({ id, verdict, range }) => ({
  id, kind: 'highlight', range, color: VERDICTS[verdict].color, // 35 % over the text
})));
// The list beside the page: an entry brings its words into view.
const list = document.getElementById('claims');
for (const { id, verdict, range } of claims) {
  const item = document.createElement('button');
  item.style.setProperty('--tag', VERDICTS[verdict].color);
  item.innerHTML = '<b></b><span></span>';
  item.querySelector('b').textContent = VERDICTS[verdict].name;
  item.querySelector('span').textContent = markdown.slice(range.start, range.end);
  item.addEventListener('click', () => view.revealAnnotation(id, { behavior: 'smooth' }));
  list.append(item);
}

One highlight per record, coloured by its verdict, and the same records listed

Ingredients

Type
Source Serif 4, Source Sans 3 (SIL OFL 1.1)
Assets
None: every picture is drawn in code

Method

#1 · Keep the records as ranges

script.js · lines 85–103in full code
// A CMS stores the offsets; the pen finds them from quotes so that both editions run.
const VERDICTS = {
  checked: { color: '#d39b00', name: t({ en: 'Checked', es: 'Comprobado' }) },
  source: { color: '#2f7fd6', name: t({ en: 'Needs a source', es: 'Falta la fuente' }) },
  query: { color: '#d64c86', name: t({ en: 'Query the author', es: 'Preguntar al autor' }) },
};
const claims = t({
  en: [['source', 'reached England from Virginia'],
    ['query', 'perhaps in Spain, perhaps in an English garden'], ['checked', 'set out in 1789'],
    ['source', 'it became the commonest street tree'],
    ['checked', 'first recorded in London in 2008']],
  es: [['source', 'llegó a Inglaterra desde Virginia'],
    ['query', 'quizá en España, quizá en un jardín inglés'], ['checked', 'plantados en 1789'],
    ['source', 'llegó a ser el árbol de calle más común'],
    ['checked', 'registrada por primera vez en Londres en 2008']],
}).map(([verdict, quote], i) => {
  const start = markdown.indexOf(quote);
  return { id: `c${i + 1}`, verdict, range: { start, end: start + quote.length } };
});

A record is what the tool already stores: a range { start, end } in the chapter's Markdown and the reviewer's verdict. The marks know nothing of verdicts; the host turns each one into a colour. Here the offsets come from quoting the text, so that the English and the Spanish editions both run, where a content system would read them from its database.

#2 · Paint them as highlights

The code is the short answer above. setAnnotations replaces the marks the viewer shows; each highlight is a translucent fill over every piece of its range, at 35 % of its color, so the text under it stays black. Call it again with a new list whenever the records change, and the viewer repaints only the pages in view. The list on the right is the same records: revealAnnotation scrolls a mark into view by the id the host gave it.

#3 · Name the marks in the reader's language

script.js · lines 107–108in full code
const labels = t({ en: {}, es: { viewer: 'Páginas del boletín', page: 'Página {n} de {count}',
  kinds: { highlight: 'Resaltado' } } });

The viewer makes each mark a button for the keyboard, named from labels.kinds and the marked text, so a screen reader announces "Highlight: set out in 1789". The Spanish edition passes its own names; the English one needs none.

The whole recipe

// ═══ Postext Cookbook · Nº 213 · Highlights from a list of ranges ═══════════════════
// https://postext.dev/en/cookbook/highlights-from-ranges
// Code: MIT · Text: original (CC BY 4.0) · Pictures: none
// Fonts: Source Serif 4, Source Sans 3 (SIL OFL 1.1) · Needs postext ≥ 1.103.0
//
// A newsletter article on the London plane, as a fact-checker sees it in a review tool: the
// claims the desk has looked at are highlighted in three colours, one per verdict, and the
// list beside the page is the same records. A click on an entry brings its words into view.
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 = 'highlights-from-ranges';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: semantic colours, every one linked by id
const palette = {
  ink: '#22201c', // text
  accent: '#3d6b35', // leaf green: headings and folio
};
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 newsletter page, one wide column
const config = {
  locale: t({ en: 'en-gb', es: 'es' }),
  colorPalette,
  page: { sizePreset: 'custom', width: mm(160), height: mm(230), dpi: 150,
    margins: { top: mm(20), bottom: mm(24), left: mm(24), right: mm(24) } },
  layout: { layoutType: 'single' },
  bodyText: { fontFamily: 'Source Serif 4', fontSize: pt(10.5), lineHeight: pt(15),
    color: col('ink'), boldColor: col('ink'), italicColor: col('ink'), referenceColor: col('ink'),
    textAlign: 'justify', firstLineIndent: mm(4.5), indentAfterHeading: false,
    hyphenation: { enabled: true }, optimalLineBreaking: true,
    avoidWidows: true, avoidOrphans: true, avoidRunts: true },
  headings: { fontFamily: 'Source Sans 3', color: col('accent'), fontWeight: 700, levels: [
    { level: 1, fontSize: pt(26), lineHeight: pt(30), marginTop: pt(0), marginBottom: pt(15) },
    { level: 2, fontSize: pt(10.5), lineHeight: pt(15), marginTop: pt(15), marginBottom: pt(0) },
  ] },
  header: { elements: [] },
  footer: { elements: [
    { kind: 'text', id: 'folio', content: '{pageNumber}', fontFamily: 'Source Sans 3',
      fontSize: pt(8), fontWeight: 700, color: col('accent'), align: 'center',
      placement: { anchor: { to: 'page', edge: 'bottom-center' }, offset: { y: mm(-12) } } },

The composed script.js runs as it is: paste it into any page’s module script, or open the recipe on CodePen. Recipe folder on GitHub ↗ (opens in a new tab)

Variations

#Make the fills stronger

On a cream or tinted page a 35 % fill can read too faint.

-  document: doc, markdown, zoom: 'fit-page', labels,
+  document: doc, markdown, zoom: 'fit-page', labels, marks: { highlightOpacity: 0.5 },

#Underline instead of filling

For a quieter proof, underline the claims in the verdict's colour.

-  id, kind: 'highlight', range, color: VERDICTS[verdict].color, // 35 % over the text
+  id, kind: 'underline', range, color: VERDICTS[verdict].color, // under the baseline

Credits

Text
Original prose, CC BY 4.0
Fonts
Source Serif 4 (SIL OFL 1.1) · Source Sans 3 (SIL OFL 1.1)