TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/angezid/advanced-mark.js/llms.txt
Use this file to discover all available pages before exploring further.
filter callback gives you fine-grained control over which matches actually get highlighted. Instead of marking every occurrence found by mark(), markRegExp(), or markRanges(), you can inspect each match in context and decide — on a per-match basis — whether to highlight it, skip it, or stop the search entirely.
How the filter callback works
Every highlighting method accepts afilter function in its options object. The callback fires once per match (or, when acrossElements: true is used, once per text node that is part of the match). It receives contextual information about the current match and must return true to highlight or false to skip.
info object passed to every filter callback contains:
| Property | Type | Description |
|---|---|---|
match | array | The result of RegExp.exec() |
count | number | Number of matches highlighted so far |
matchStart | boolean | true when this call marks the start of a match (acrossElements only) |
abort | boolean | Set to true to stop the entire search immediately |
When
acrossElements: true is enabled and a match spans multiple text nodes, the filter callback fires once for each text node that is part of the match — not just once for the whole match. Use info.matchStart to detect the start of a new match when maintaining an external counter.Filtering in mark()
Themark() filter signature is:
nodeOrArray— theTextnode containing the match (or an array of nodes when using the Highlight API withacrossElementsandrangeAcrossElements)term— the search term currently being matchedmatchesSoFar— total matches highlighted across all terms so fartermMatchesSoFar— matches for the current term so farinfo— the info object described above
Filtering in markRegExp()
ThemarkRegExp() filter signature is slightly different — term is replaced by matchString and termMatchesSoFar is omitted:
matchString— the matched string (whole match, or the current group’s string whenseparateGroupsis active)info.groupIndex— present whenseparateGroups: true; the index of the capturing group currently being processed
Filtering in markRanges()
ThemarkRanges() filter callback has a distinct signature:
nodeOrArray— the text node(s) that contain the rangerange— the range object{ start, length }currently being processedmatchString— the text content matched by this rangeindex— the zero-based index of this range in the original ranges array
Filtering via the each callback
Theeach callback — which fires after a match element has been created — also exposes info.abort. This lets you cap highlights after the fact, without needing to use filter at all:
Aborting early
Two mechanisms are available to stop highlighting mid-search:info.abort = true — works in both filter and each and is the universal approach. Setting it stops the search after the current match is processed.
regex.lastIndex = Infinity — available only in markRegExp() when you hold a direct reference to the RegExp object. Assigning Infinity to lastIndex causes the next exec() call to return null, ending the search naturally.
info.abort and regex.lastIndex = Infinity are equivalent when used inside filter. Prefer info.abort if you don’t have a reference to the RegExp outside the callback, or when using mark().