| 503 | |
| 504 | /** the words around the first matched term, with matches marked */ |
| 505 | const excerpt = (text: string, result: SearchResult): ReactNode => { |
| 506 | const lower = text.toLowerCase() |
| 507 | const first = Math.min( |
| 508 | ...result.terms.map(term => lower.indexOf(term)).filter(i => i !== -1), |
| 509 | text.length |
| 510 | ) |
| 511 | const found = first < text.length |
| 512 | const start = |
| 513 | found ? wordStart(text, Math.max(0, first - excerptRadius), first) : 0 |
| 514 | const end = wordEnd(text, start + excerptRadius * 3, found ? first : 0) |
| 515 | const slice = |
| 516 | (start > 0 ? "…" : "") + |
| 517 | text.slice(start, end).trimEnd() + |
| 518 | (end < text.length ? "…" : "") |
| 519 | return marked(slice, matcherOf(result)) |
| 520 | } |