Skip to content

fix(search-ui): show body-match highlight in the result preview#49

Merged
betschki merged 2 commits into
mainfrom
fix/search-highlight-body-match
Jun 24, 2026
Merged

fix(search-ui): show body-match highlight in the result preview#49
betschki merged 2 commits into
mainfrom
fix/search-highlight-body-match

Conversation

@betschki

@betschki betschki commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

No description provided.

The preview chose its snippet from the excerpt field first, falling back to the
raw excerpt text — which is virtually always present — so the `||` chain
short-circuited there and never reached the plaintext (body) highlight. A term
that matched only in the body ranked the post but showed no highlight in the
preview. The snippet is now taken from whichever field actually matched
(excerpt, then body, gated on the highlight's matched_tokens), with the raw
excerpt used only when neither matched. A shared matchedSnippet() helper fixes
all layouts (modal, palette, discovery).

Bumps all packages to 2.0.7 with the matching CHANGELOG entry.
@sourcery-ai

sourcery-ai Bot commented Jun 24, 2026

Copy link
Copy Markdown

Reviewer's Guide

Adds a matchedSnippet helper to ensure search result previews use highlighted snippets only from fields that actually matched the query, updates both normalizeHit and modal result rendering to use this helper, and bumps all packages to version 2.0.7 with aligned internal dependencies and changelog entry.

Sequence diagram for search result preview highlighting with matchedSnippet

sequenceDiagram
    actor User
    participant SearchUI
    participant Typesense

    User->>SearchUI: submitSearch(query)
    SearchUI->>Typesense: search(query)
    Typesense-->>SearchUI: hits with highlight

    loop for each hit
        SearchUI->>SearchUI: normalizeHit(hit, doc)
        SearchUI->>SearchUI: matchedSnippet(hit, excerpt)
        alt [matchedSnippet returns snippet]
            SearchUI->>SearchUI: use excerpt snippet as excerptHtml
        else [no match in excerpt]
            SearchUI->>SearchUI: matchedSnippet(hit, plaintext)
            alt [matchedSnippet returns snippet]
                SearchUI->>SearchUI: use plaintext snippet as excerptHtml
            else [no match in excerpt or plaintext]
                SearchUI->>SearchUI: use raw excerpt/plaintext fallback
            end
        end
    end

    SearchUI-->>User: renderResultsWithHighlightedPreviews()
Loading

File-Level Changes

Change Details Files
Introduce matchedSnippet helper and use it to select excerpt/body snippets only when they truly matched, fixing missing body-only highlights in search-ui.
  • Add matchedSnippet(hit, fieldName) with guards for enableHighlighting, presence of highlight, and non-empty matched_tokens, returning snippet/value or null.
  • Refactor normalizeHit to prefer matchedSnippet for excerpt then plaintext, falling back to escaped raw excerpt/plaintext and truncating long snippets.
  • Update modal result rendering to use matchedSnippet for excerpt/plaintext instead of getHighlightedExcerpt, add truncation to ~200 chars, and remove the old helper.
packages/search-ui/src/search.js
Add focused tests covering snippet selection behavior for excerpt vs body matches and highlighting edge cases.
  • Add regression tests for body-only match showing highlighted plaintext snippet instead of raw excerpt.
  • Add tests verifying preference for excerpt snippet when both excerpt and plaintext match, and fallback to raw excerpt when neither matches.
  • Add tests ensuring matchedSnippet ignores fields with empty matched_tokens and returns null when highlighting is disabled.
packages/search-ui/src/__tests__/component.test.js
Publish release 2.0.7 and align package versions and internal dependency ranges across the monorepo.
  • Add 2.0.7 entry to CHANGELOG.md describing the search highlighting fix and its impact on layouts.
  • Bump root package version to 2.0.7.
  • Bump versions of config, core, search-ui, cli, and webhook-handler packages to 2.0.7 and update their internal dependency ranges from ^2.0.6 to ^2.0.7 where applicable.
CHANGELOG.md
package.json
packages/config/package.json
packages/core/package.json
packages/search-ui/package.json
apps/cli/package.json
apps/webhook-handler/package.json

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@coderabbitai

coderabbitai Bot commented Jun 24, 2026

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

An error occurred during the review process. Please try again later.

📝 Walkthrough

Walkthrough

Adds a matchedSnippet helper to MagicPagesSearchElement that gates highlight snippet selection on whether matched_tokens is non-empty for a given field. Updates normalizeHit and modal excerpt logic to use this helper with excerpt-first, plaintext-second priority and raw fallbacks. Adds regression tests, documents the fix in CHANGELOG, and bumps all packages to 2.0.7.

Changes

Search Highlight Fix and v2.0.7 Release

Layer / File(s) Summary
matchedSnippet helper, normalizeHit, and modal excerpt fix
packages/search-ui/src/search.js, packages/search-ui/src/__tests__/component.test.js
Adds matchedSnippet(hit, fieldName) that returns a snippet only when the field's matched_tokens is non-empty; updates normalizeHit and modal hits mapping to prefer excerpt then plaintext matched snippets with raw fallbacks; adds regression tests covering snippet priority, empty matched_tokens, and enableHighlighting: false.
Version bumps and CHANGELOG
CHANGELOG.md, package.json, packages/*/package.json, apps/*/package.json
Bumps all package versions from 2.0.6 to 2.0.7 and records the highlight snippet fix under a new [2.0.7] CHANGELOG entry.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐇 Hoppy fix for broken marks,
The snippet now lands where the match embarks!
Excerpt first, then plaintext plain,
No more highlighting in vain.
Version bumped, the bunny cheers—
2.0.7 appears! 🎉

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main fix: showing body-match highlights in search result previews.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/search-highlight-body-match

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install failed: one or more packages not found in the registry.

Warning

Billing warning: we have not been able to collect payment for this subscription for more than 72 hours. Please update the payment method or pay any pending invoices in Billing to avoid service interruption.


Comment @coderabbitai help to get the list of available commands.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
packages/search-ui/src/__tests__/component.test.js (1)

348-414: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add one regression test for the modal rendering path too.

These tests cover normalizeHit/matchedSnippet well, but not the modal results.hits.map(...) excerpt chain that was also changed. A focused modal-path assertion would prevent drift between paths.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/search-ui/src/__tests__/component.test.js` around lines 348 - 414,
Add a regression test for the modal rendering path, not just normalizeHit and
matchedSnippet. In component.test.js, cover the results.hits.map(...) flow used
by the modal so it asserts the excerptHtml chosen there matches the highlighted
body snippet when only plaintext matched, and still prefers the excerpt snippet
when excerpt matched. Use the existing mountWithConfig, normalizeHit, and
matchedSnippet setup as references to locate the affected path.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/search-ui/src/search.js`:
- Around line 1160-1163: The modal excerpt fallback in search.js is using raw
hit.document.excerpt/plaintext values without escaping before they reach the
innerHTML path. Update the excerpt construction in the search result rendering
logic to escape the fallback content the same way normalizeHit handles raw text,
and make sure the matchedSnippet chain still prefers already-safe values before
falling back to escaped document fields.

---

Nitpick comments:
In `@packages/search-ui/src/__tests__/component.test.js`:
- Around line 348-414: Add a regression test for the modal rendering path, not
just normalizeHit and matchedSnippet. In component.test.js, cover the
results.hits.map(...) flow used by the modal so it asserts the excerptHtml
chosen there matches the highlighted body snippet when only plaintext matched,
and still prefers the excerpt snippet when excerpt matched. Use the existing
mountWithConfig, normalizeHit, and matchedSnippet setup as references to locate
the affected path.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: d4c7e935-82a6-4c3c-a060-4641693914be

📥 Commits

Reviewing files that changed from the base of the PR and between 05e1811 and 24bcd45.

📒 Files selected for processing (9)
  • CHANGELOG.md
  • apps/cli/package.json
  • apps/webhook-handler/package.json
  • package.json
  • packages/config/package.json
  • packages/core/package.json
  • packages/search-ui/package.json
  • packages/search-ui/src/__tests__/component.test.js
  • packages/search-ui/src/search.js

Comment thread packages/search-ui/src/search.js Outdated
Address review on #49:
- Escape the raw excerpt/plaintext fallback in the modal render path before it
  reaches innerHTML, matching normalizeHit. matchedSnippet values stay
  unescaped as trusted highlight markup.
- Add modal-path regression tests so the excerpt chain there can't drift from
  normalizeHit: body-only match renders the highlighted body snippet; the raw
  fallback is escaped.
@betschki betschki merged commit fec68a8 into main Jun 24, 2026
3 of 4 checks passed
@betschki betschki deleted the fix/search-highlight-body-match branch June 24, 2026 17:39
betschki added a commit that referenced this pull request Jun 24, 2026
Address review on #49:
- Escape the raw excerpt/plaintext fallback in the modal render path before it
  reaches innerHTML, matching normalizeHit. matchedSnippet values stay
  unescaped as trusted highlight markup.
- Add modal-path regression tests so the excerpt chain there can't drift from
  normalizeHit: body-only match renders the highlighted body snippet; the raw
  fallback is escaped.
betschki added a commit that referenced this pull request Jun 24, 2026
fix(search-ui): show body-match highlight in the result preview
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant