Documentation Index
Fetch the complete documentation index at: https://mintlify.com/johnfactotum/foliate-js/llms.txt
Use this file to discover all available pages before exploring further.
textWalker bridges string-based operations and DOM Range objects. It walks the text nodes of a Range, Document, or DocumentFragment, collects their string values into an array, and exposes a makeRange factory so that any index and offset computed on those strings can be converted back into a real DOM Range. This is the mechanism that lets search.js run Intl.Segmenter and Intl.Collator operations over document text while still returning selectable ranges. It is also used internally for text-to-speech segmentation.
Import
textWalker(node, func, filterFunc?)
A generator function. Walks all text nodes within node, passes them to func, and yields whatever func yields.
The DOM node or range to walk. When a
Range is passed, only text nodes within that range are collected. When a Document or DocumentFragment is passed, all text nodes in the document body are collected.A generator function called with
(strings, makeRange):strings— an array of strings, one per text node (using the node’snodeValue, or''for empty nodes).makeRange(startIndex, startOffset, endIndex, endOffset)— a factory that constructs a DOMRangefrom indices and offsets into thestringsarray.
func should yield results; textWalker re-yields them to the caller.An optional custom node filter for the internal
TreeWalker. Same signature as a TreeWalker acceptNode filter: (node: Node) => NodeFilter.FILTER_ACCEPT | NodeFilter.FILTER_REJECT | NodeFilter.FILTER_SKIP. When omitted, <script> and <style> elements are rejected and all other elements are skipped (only text nodes are accepted).func yields.
makeRange(startIndex, startOffset, endIndex, endOffset)
The factory passed as the second argument to func. Creates a DOM Range anchored to the text nodes collected during the walk.
Index into the
strings array for the range start.Character offset within
strings[startIndex].Index into the
strings array for the range end.Character offset within
strings[endIndex].Range with its start and end set to the corresponding text nodes and offsets in the live document.
Examples
Word segmentation
Segment all text in a document into words and get a DOMRange for each word:
Searching within a Range
Pass aRange instead of a Document to restrict the walk to a specific region:
Notes
textWalkeris a generator — the internal walk andfuncare both executed lazily as you consume the iterator.- The default filter rejects
<script>and<style>subtrees entirely and skips all other elements, so only text and CDATA nodes reachfunc. stringsand the corresponding DOM nodes remain in the same order, sostrings[i]always corresponds to thei-th text node collected during the walk.