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.
search.js provides locale-aware full-text search over DOM documents. It can be used as a standalone module or via the higher-level view.search() method. The core implementation uses Intl.Collator and Intl.Segmenter to support diacritic-insensitive matching, case-insensitive matching, and whole-word search. Matches can span multiple adjacent text nodes. The search is inherently incremental — you should consume results lazily rather than waiting for the entire book to be scanned.
Import
searchMatcher(textWalker, options)
Creates a reusable matcher generator function that searches a single document at a time.
The
textWalker function from text-walker.js. Passed in as a dependency so the module remains loosely coupled.BCP 47 locale tag used when the document has no
lang attribute (e.g. 'en', 'ja'). Falls back to 'en' if not set.When
true, the search is case-sensitive.When
true, diacritics are treated as significant (e.g. é ≠ e).When
true, uses Intl.Segmenter with granularity: 'word' so that only complete word matches are returned.An optional node filter passed to
textWalker to restrict which text nodes are searched. Same signature as a TreeWalker filter.matcher generator function.
The returned matcher(doc, query)
The matcher returned by searchMatcher is a generator function. Call it with a section document and a query string to iterate over all matches within that document.
The section
Document to search. Its lang attribute (on <html> or <body>) is used to select the locale for Intl.Collator and Intl.Segmenter.The search string.
A DOM
Range spanning the matched text in the document.Context strings surrounding the match.
Sensitivity mapping
The four option flags map toIntl.Collator sensitivity as follows:
matchDiacritics | matchCase | sensitivity |
|---|---|---|
false | false | 'base' — ignore diacritics and case |
true | false | 'accent' — distinguish diacritics, ignore case |
false | true | 'case' — ignore diacritics, distinguish case |
true | true | 'variant' — exact match |
Intl.Segmenter is unavailable, or when granularity is 'grapheme' and sensitivity is 'variant' or 'accent', the implementation falls back to a simple String.prototype.indexOf-based search.
Incremental usage
Searching an entire book section-by-section is slow. Load results one section at a time and yield control between sections:view.search() does this automatically and draws search highlights via the overlayer. Use searchMatcher directly when you need the raw ranges or excerpts without the rendering layer.