From 67a7b23b85d2e3ce6b094b682ba9f07dc453d355 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Mon, 29 Jun 2026 05:26:34 -0500 Subject: [PATCH] refac --- src/lib/components/layout/SearchModal.svelte | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/lib/components/layout/SearchModal.svelte b/src/lib/components/layout/SearchModal.svelte index 8a56ba714c..8146750fad 100644 --- a/src/lib/components/layout/SearchModal.svelte +++ b/src/lib/components/layout/SearchModal.svelte @@ -296,16 +296,23 @@ const getHighlightedSnippet = (snippet: string, query: string) => { const match = getSnippetQuery(query).toLowerCase(); - const index = match ? snippet.toLowerCase().indexOf(match) : -1; + const matchIndex = match ? snippet.toLowerCase().indexOf(match) : -1; - if (index === -1) { + if (matchIndex === -1) { return [{ text: snippet, highlight: false }]; } + const start = Math.max(matchIndex - 60, 0); + const end = Math.min(matchIndex + match.length + 80, snippet.length); + const visibleSnippet = `${start > 0 ? '...' : ''}${snippet.slice(start, end)}${ + end < snippet.length ? '...' : '' + }`; + const index = visibleSnippet.toLowerCase().indexOf(match); + return [ - { text: snippet.slice(0, index), highlight: false }, - { text: snippet.slice(index, index + match.length), highlight: true }, - { text: snippet.slice(index + match.length), highlight: false } + { text: visibleSnippet.slice(0, index), highlight: false }, + { text: visibleSnippet.slice(index, index + match.length), highlight: true }, + { text: visibleSnippet.slice(index + match.length), highlight: false } ].filter((part) => part.text); };