Skip to content

fix(search): snippet always fills the full context window - #13

Open
ashishbhateja wants to merge 1 commit into
mainfrom
fix/snippet-context-window
Open

ashishbhateja wants to merge 1 commit into
mainfrom
fix/snippet-context-window

Conversation

@ashishbhateja

Copy link
Copy Markdown
Owner

What

The snippet() function in src/search.js had an off-by-half bug: when a matched term appeared near the start or end of the text, the context window was clipped to half of contextChars instead of the full width.

Before (simplified):

const start = Math.max(0, idx - half);      // clamped to 0 near the start
const end   = Math.min(len, idx + half);    // only half the window remained

When the match was at idx = 0, the window was [0, 60] — 60 chars instead of the intended 120.
When the match was near the end (e.g. idx = len - 5), the window was [len - half - 5, len] — again roughly half.

After:

const start = Math.max(0, Math.min(idx - half, text.length - contextChars));
const end   = Math.min(text.length, start + contextChars);

The window slides to compensate for edge clamping, so it always spans contextChars characters when the text is long enough. The matched term is guaranteed to remain visible: start ≤ idx by construction.

Tests

Two new cases added to scripts/smoke.mjs (now 21 checks, up from 19) that would have caught this bug:

  • snippet fills the full context window when the matched term is near the start
  • snippet fills the full context window when the matched term is near the end

All 21 checks pass. npm run build succeeds.

Issue

Advances #3 — specifically the "result snippets" part: a term that leads an article's summary or body now shows the reader a useful 120-char window instead of a half-width stub.


Generated by Claude Code

When the matched term was near the start or end of the text,
the [idx - half, idx + half] window was clipped by the text
boundary, producing a snippet half the intended width.

Replace the two-variable window with a sliding approach:
compute start = max(0, min(idx - half, text.length - contextChars))
and derive end from start, so the window always spans contextChars
characters (or the full text when shorter). The matched term is
guaranteed to remain visible because the original half-window still
centres on it before clamping.

Advances issue #3 (search result snippets).

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.

1 participant