fix(search-ui): show body-match highlight in the result preview#49
Conversation
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.
Reviewer's GuideAdds 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 matchedSnippetsequenceDiagram
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()
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
|
Caution Review failedAn error occurred during the review process. Please try again later. 📝 WalkthroughWalkthroughAdds a ChangesSearch Highlight Fix and v2.0.7 Release
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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
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 |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
packages/search-ui/src/__tests__/component.test.js (1)
348-414: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAdd one regression test for the modal rendering path too.
These tests cover
normalizeHit/matchedSnippetwell, but not the modalresults.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
📒 Files selected for processing (9)
CHANGELOG.mdapps/cli/package.jsonapps/webhook-handler/package.jsonpackage.jsonpackages/config/package.jsonpackages/core/package.jsonpackages/search-ui/package.jsonpackages/search-ui/src/__tests__/component.test.jspackages/search-ui/src/search.js
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.
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.
fix(search-ui): show body-match highlight in the result preview
No description provided.