|
296 | 296 |
|
297 | 297 | const getHighlightedSnippet = (snippet: string, query: string) => { |
298 | 298 | const match = getSnippetQuery(query).toLowerCase(); |
299 | | - const index = match ? snippet.toLowerCase().indexOf(match) : -1; |
| 299 | + const matchIndex = match ? snippet.toLowerCase().indexOf(match) : -1; |
300 | 300 |
|
301 | | - if (index === -1) { |
| 301 | + if (matchIndex === -1) { |
302 | 302 | return [{ text: snippet, highlight: false }]; |
303 | 303 | } |
304 | 304 |
|
| 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 | +
|
305 | 312 | 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 } |
309 | 316 | ].filter((part) => part.text); |
310 | 317 | }; |
311 | 318 |
|
|
0 commit comments