Skip to main content

Documentation 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.

markRanges() highlights text by specifying character start positions and lengths within the context’s text content. It is especially useful for highlighting search results from a backend or full-text index, where you already know the exact byte or character offsets of matches. It also supports a line-range mode via the markLines option for highlighting entire lines by number.

Syntax

// Vanilla JS
const instance = new Mark(context);
const ranges = [{ start: 2, length: 5 }, { start: 10, length: 7 }];
instance.markRanges(ranges[, options]);

// jQuery
$(selector).markRanges(ranges[, options]);

Parameters

ranges
object[]
required
An array of range objects, each with:
  • start (number) — zero-based character offset into the context’s concatenated text content where the highlight begins. When markLines is true, this becomes a one-based line number instead.
  • length (number) — number of characters to highlight. When markLines is true, this is the number of lines to highlight starting from start.
// Text ranges (0-based start)
const ranges = [
  { start: 0,  length: 5 },
  { start: 10, length: 3 },
];

// Line ranges (1-based start, requires markLines: true)
const lineRanges = [
  { start: 1, length: 2 }, // lines 1–2
  { start: 5, length: 1 }, // line 5
];
Start positions are counted across the full text content of the context, including whitespace. Ranges that are out of bounds or cover only whitespace are reported via the noMatch callback.
options
object
Optional configuration object.

Default Options

const options = {
  element: 'mark',
  className: '',
  exclude: [],

  staticRanges: true,        // Highlight API only
  rangeAcrossElements: true, // Highlight API only
  shadowDOM: false,
  iframes: false,
  iframesTimeout: 5000,

  filter: (nodeOrArray, range, matchingString, index) => {
    return true; // must return either true or false
  },
  each: (elementOrRange, range, rangeInfo) => {},
  done: (total, totalRanges) => {},
  noMatch: (range) => {},
  debug: false,
  log: window.console,
};

Usage Examples

const instance = new Mark(document.querySelector('.content'));

const ranges = [
  { start: 0,  length: 5 },
  { start: 10, length: 7 },
];

instance.markRanges(ranges, {
  done: (total, totalRanges) => {
    console.log(`Highlighted ${totalRanges} ranges with ${total} elements.`);
  },
  noMatch: (range) => {
    console.warn('Range did not match:', range);
  },
});
Character positions in start include all whitespace characters (spaces, newlines, tabs). When computing offsets from backend results, make sure your indexing matches the browser’s textContent representation of the context element.
Use the markLines option together with a <pre> or <code> element to highlight entire lines of code by line number without needing to compute character offsets yourself.

Build docs developers (and LLMs) love