> **Living Document Notice** > *Published 2026-09-21.* The evolving architecture, revisions, and connected notes for this dispatch live in the Stax Digital Garden.
Tree-sitter Token Extraction Across EPUB and PDF DOMs
Reading applications that operate over reflowable EPUB or fixed-layout PDF documents routinely introduce byte drift during text selection. Browser WebViews and PDF renderers mangle soft hyphens, merge ligature glyphs (such as fi or ffl), and discard underlying XML node boundaries. The Bosun Commonplace subsystem resolves highlight drift by executing Tree-sitter tokenization passes directly over decomposed EPUB XHTML streams and extracted PDF content streams.
Structural Grammar Mapping
Rather than capturing plain unformatted strings, the extraction engine aligns user selections against the document's Concrete Syntax Tree (CST). For EPUB publications, the parser applies an HTML/XML grammar to index parent element hierarchies, node offsets, and sibling indices:
pub struct DomHighlightSpan {
pub node_path: Vec<String>,
pub start_byte: usize,
pub end_byte: usize,
pub anchor_hash: [u8; 32],
}
impl DomHighlightSpan {
pub fn extract_token_slice<'a>(&self, source: &'a [u8]) -> &'a [u8] {
assert!(self.end_byte <= source.len(), "Byte boundary exceeds DOM node length");
&source[self.start_byte..self.end_byte]
}
}
By anchoring selections to byte offsets within individual DOM nodes rather than global character counts, text extracts remain stable across reader viewport resizes, font substitution, and layout recalculations.
PDF Content Stream Deserialization
PDF documents lack semantic DOM trees, storing text as raw PostScript layout operators (Tj, TJ) placed at arbitrary Cartesian coordinates. Commonplace normalizes PDF text extraction by sorting operator operands into structural reading order before computing token boundaries:
commonplace-cli pdf extract-tokens input/paper.pdf --page 12 --mode cst
TokenStream: 1420 glyph nodes, 42 line boxes, coordinate precision 0.01pt
Anchored: sha256:d8f4e1... (offset 1042..1188)
The resulting token spans serialize into structured frontmatter sidecars, ensuring that annotations link to byte-exact document coordinates without relying on proprietary PDF viewer highlights.