Skip to content

fix(search): snap snippet boundaries to whole words - #15

Open
ashishbhateja wants to merge 1 commit into
mainfrom
fix/snippet-word-boundaries
Open

ashishbhateja wants to merge 1 commit into
mainfrom
fix/snippet-word-boundaries

Conversation

@ashishbhateja

Copy link
Copy Markdown
Owner

What

The snippet() function in src/search.js centred its context window on the first matched term and then sliced the text at arbitrary character positions. When rawStart or rawEnd fell mid-word, the result was fragments like:

… refixword search endwo …

After this fix:

… search …

How

Two small adjustments to the boundary calculation, applied only when the boundary is not already at the edge of the text:

  • rawStart mid-word → advance to after the next space (nextSpace + 1), so the snippet begins at a whole word.
  • rawEnd mid-word → retreat to the preceding space, so the snippet ends at a whole word.

A guard on the end adjustment (prevSpace > idx) ensures we never retreat past the start of the matched term, even with a very tight contextChars setting.

The fallback path (no match found — returns the start of the text) is unchanged.

Tests

Two new smoke tests added to scripts/smoke.mjs:

  • snippet snaps start boundary to a whole word — rawStart inside 'longprefixword', verifies snippet begins with a whole token.
  • snippet snaps end boundary to a whole word — rawEnd inside 'endword', verifies snippet ends with a whole token.

All 21 checks pass (npm test); npm run build succeeds.

Issue

Advances #3 — improves the display quality of search result snippets shown in the reading list.


Generated by Claude Code

The snippet() window was centred on the start of the first matched term
and sliced at arbitrary character positions, producing fragments like
"… refixword search endwo …" when rawStart or rawEnd fell mid-word.

Now, when a boundary lands inside a word:
- rawStart: advance to after the next space (next word start)
- rawEnd: retreat to the preceding space (end of previous word)

A guard on the end snap (prevSpace > idx) ensures we never retreat past
the start of the matched term itself, even when the context window is
very tight.

Two smoke tests assert the new behaviour: one per boundary direction.
Advances issue #3 (search result display quality).

This branch has not been deployed

No deployments
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.

2 participants