~bigbes/lethe

ref: d2a77459be9c2a2a61326e3bce1e64b749bda522 lethe/web/src/features/search/highlightSnippet.ts -rw-r--r-- 614 bytes
d2a77459 — Eugene Blikh fix: store full opencode tool output and display it in session view 23 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import React from 'react'

const MARKER_RUNES = /\x02|\x03/g

/**
 * Splits a snippet string on \x02/\x03 marker runes and interleaves plain text
 * with `<mark>` elements for matched segments — safe against XSS.
 */
export function highlightSnippet(snippet: string): (string | React.JSX.Element)[] {
  const parts = snippet.split(MARKER_RUNES)
  const out: (string | React.JSX.Element)[] = []
  let inMatch = false
  for (const p of parts) {
    if (p === '') continue
    if (inMatch) out.push(React.createElement('mark', { key: out.length }, p))
    else out.push(p)
    inMatch = !inMatch
  }
  return out
}