AddThisFeature

Search Match Highlighting

Show users why each result matched instead of making them hunt.

simple Search & Discovery

What it adds

Highlighted spans in search results marking the text that matched the query, rendered safely.

What your agent is told to do

5
  1. 1

    Escape the record text first, then insert highlight markup. Escaping afterwards destroys your own markup, and skipping it gives you stored XSS.

  2. 2

    Escape the query too before using it to match. A raw user query containing regex metacharacters will either throw or match the wrong thing.

  3. 3

    Match case-insensitively but render the original casing from the source text — never the casing the user typed.

  4. 4

    For long fields, truncate around the first match rather than from the start, so the snippet actually contains the highlighted term.

  5. 5

    Do NOT highlight every occurrence in a long body. Cap the number of highlighted spans per snippet; a page of yellow is unreadable.

Edge cases it handles

6
  • Overlapping and adjacent matches must merge into one span, not nest into broken markup.
  • The query may match in a field that is not displayed. Either show that field's snippet or do not claim a match.
  • Highlight markup must be decorative to assistive tech — do not make a screen reader announce a mark on every word.
  • Accented and multi-byte characters must not be split mid-character by truncation.
  • When the match came from a synonym or stem, the literal query string is absent; highlight the term that actually matched.
  • Truncation must show that text was cut, and must not cut in the middle of a highlighted span.

Definition of done

8
  • Result text is escaped before highlight markup is inserted.
  • Queries containing regex or HTML characters neither throw nor render as markup.
  • Matching is case-insensitive and the original casing is preserved.
  • Long fields truncate around the match, with an ellipsis indicating the cut.
  • Overlapping matches merge into a single span.
  • Screen readers do not announce the highlighting as separate content.
  • The feature matches the existing design system.
  • No existing functionality is broken.

Related features

How it works

  1. 1

    Copy the link

    Grab the Markdown instruction URL for this feature.

  2. 2

    Give it to your AI

    Paste it into Claude Code, Cursor, v0, Lovable — whatever you build with.

  3. 3

    It inspects, then implements

    Your agent reads your existing app first, then adds the feature to fit it.

Works with your stack

These instructions are written to adapt. They tell the agent to detect your framework, match your existing design system, and reuse what you already have — rather than assuming a particular stack.

Need it tighter than that? Customize the feature and tell it exactly what you're running.