MCPcopy Create free account
hub / github.com/arktypeio/arktype / excerpt

Function excerpt

ark/docs/components/shell/SearchPanel.tsx:505–520  ·  view source on GitHub ↗
(text: string, result: SearchResult)

Source from the content-addressed store, hash-verified

503
504/** the words around the first matched term, with matches marked */
505const 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}

Callers 1

resultsOfFunction · 0.85

Calls 6

wordStartFunction · 0.85
wordEndFunction · 0.85
markedFunction · 0.85
matcherOfFunction · 0.85
filterMethod · 0.80
mapMethod · 0.65

Tested by

no test coverage detected