Chapter 24 · Part III · In practice
Integration: Postext inside a content system
What Postext gives a content management, editorial or review system, or a reading app, that shows laid-out pages: text geometry, anchors that survive revisions, annotation marks and an embeddable page viewer, and what it leaves to the host
In short
This page is for developers who build a content system, such as a tool where authors write and editors correct. It explains what Postext gives them to show the real pages of a book inside their own tool. Readers can select words on a page, and the tool learns which words of the text they are. The tool can draw highlights, crossed-out words and notes on the pages. Who may change what, and what happens to a note, stays the tool's own business.
A content system shows the book's real pages, lets people point at its words, and draws its own marks on them. Postext does the page work; the system keeps the workflow.
A team that writes and corrects books in a content management system wants to see the pages as they will print, select a sentence on page 37, attach a comment to it, strike a word and write the correction between the lines, and find that comment again after the author has rewritten the paragraph. Each of these steps needs to know where a piece of text lies on a laid-out page, and that is what Postext computes.
The same questions come from the other side of publishing. A reading app, an e-reader or a study platform shows the finished book and lets its readers highlight passages, write notes in the margin and find them again in the next edition. Reading apps and other viewers works through both, a highlighter and a reader's notebook, and shows the same marks in a viewer the app draws itself.
The integration API answers four questions in terms of the Markdown the book is written in:
| Question | Answer | Where |
|---|---|---|
| Where on the pages is this range of the text? | Rotated rectangles, one per piece of a line, on any page and in any writing mode | Text on the page |
| Which offset of the text, which word, which block is under this point? | caretAt, wordAt, blockAt | Text on the page |
| Where is a comment's text now that the author has edited it? | Anchors with context, resolved as exact, moved, ambiguous or orphaned; edits that carry the other anchors along | Anchors and edits |
| How do I draw a highlight, a strike, a replacement, a note? | Marks laid out in the text's own frame, painted on a canvas or written as SVG, found under a point | Annotation marks |
The page viewer (postext-viewer) puts the
four together: it shows the pages in a scrolling column, reports selections
and presses as Markdown ranges and offsets, paints the marks and gives the
host layers of its own over each page.
#What stays in the host
Postext has no idea of people, roles, states or decisions. There is no "reviewer", no "pending change", no "approve" button in any of these packages, and there will not be. A content system already has its own model of who may do what. It stores its comments and suggestions in its own database. It decides whether a struck word is a suggestion, an order or a change already made.
The host maps its records onto what Postext understands:
- a range of the Markdown (
{ start, end }, UTF-16 offsets in the chapter's file, front matter counted); - an anchor, the range with the text it covered and some context on each side, to store beside the record;
- an annotation, a range with a kind of mark (
highlight,underline,strike,replace,insert,note,box), an optional colour, label or text.
Everything else (threads, authors, notifications, accepting a suggestion and
writing it into the Markdown) is the host's code. applyEdits writes the
Markdown and moves the other anchors, but deciding to call it belongs to the
host.
#The packages
| Entry | For |
|---|---|
postext | rangeRects, caretRect, caretAt, wordAt, blockAt, wordRangeAt, beside buildDocument, renderPage and sourceAt |
postext/geometry | The same, plus the pieces they are built from (line placement, band titles, frame matrices), for a host that paints its own caret |
postext/annotate | textAnchor, resolveAnchor, applyEdits, mapOffset, layoutAnnotations, paintAnnotations, annotationsSvg, annotationAt |
postext-viewer | createViewer, the embeddable viewer; <PostextViewer> from postext-viewer/react |
npm install postext postext-viewerAll of them run in the browser. postext/geometry and postext/annotate
also run in Node on a document laid out there (with an OffscreenCanvas
such as @napi-rs/canvas), for a server that writes page images with their
marks for an e-mail or a PDF of comments.
#Where to start
- To show pages and react to selections, start with the viewer. It is the shortest path: one call, a few events.
- To draw on pages your system already shows (PNG files from the command line, the HTML renderer, your own canvas), use Text on the page and Annotation marks, which give SVG to lay over an image.
- To keep comments in place while the text changes, read Anchors and edits before you design your tables: store anchors, not bare offsets.
- To let readers highlight and annotate a book in your reading app or viewer, follow the two worked examples in Reading apps and other viewers.
The Cookbook has a part for developers with small runnable examples of each step, each with its own CodePen.