Skip to content

Commit 67a7b23

Browse files
committed
refac
1 parent edf2c6c commit 67a7b23

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

src/lib/components/layout/SearchModal.svelte

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,16 +296,23 @@
296296
297297
const getHighlightedSnippet = (snippet: string, query: string) => {
298298
const match = getSnippetQuery(query).toLowerCase();
299-
const index = match ? snippet.toLowerCase().indexOf(match) : -1;
299+
const matchIndex = match ? snippet.toLowerCase().indexOf(match) : -1;
300300
301-
if (index === -1) {
301+
if (matchIndex === -1) {
302302
return [{ text: snippet, highlight: false }];
303303
}
304304
305+
const start = Math.max(matchIndex - 60, 0);
306+
const end = Math.min(matchIndex + match.length + 80, snippet.length);
307+
const visibleSnippet = `${start > 0 ? '...' : ''}${snippet.slice(start, end)}${
308+
end < snippet.length ? '...' : ''
309+
}`;
310+
const index = visibleSnippet.toLowerCase().indexOf(match);
311+
305312
return [
306-
{ text: snippet.slice(0, index), highlight: false },
307-
{ text: snippet.slice(index, index + match.length), highlight: true },
308-
{ text: snippet.slice(index + match.length), highlight: false }
313+
{ text: visibleSnippet.slice(0, index), highlight: false },
314+
{ text: visibleSnippet.slice(index, index + match.length), highlight: true },
315+
{ text: visibleSnippet.slice(index + match.length), highlight: false }
309316
].filter((part) => part.text);
310317
};
311318

0 commit comments

Comments
 (0)