import type { ReactNode } from 'react' /** * Render a server-side `ts_headline` snippet (whose matched terms are wrapped in * literal `…` markers) as highlighted React nodes WITHOUT * dangerouslySetInnerHTML: split on the markers and wrap the odd segments in * , so every text segment is React-escaped and the raw transcript/decision * body can never inject markup. * * This is the canonical match-evidence renderer shared by every filtered surface * (search tiles, decision/cause cards, policy-question meetings). The backend * produces the `` snippet; callers pass it straight through. */ export function highlightSnippet(text: string): ReactNode { return text.split(/<\/?mark>/).map((seg, i) => i % 2 === 1 ? ( {seg} ) : ( {seg} ), ) } /** About `max` visible characters of the passage around its first match, cut at word edges, with ellipses where cut. */ export function excerptAroundMark(text: string, max = 190): string { const plain = text.replace(/\s+/g, ' ').trim() if (plain.replace(/<\/?mark>/g, '').length <= max) return plain const at = Math.max(0, plain.indexOf('')) let start = Math.max(0, at - Math.round(max * 0.35)) let end = Math.min(plain.length, start + max + 26) if (start > 0) start = plain.indexOf(' ', start) + 1 if (end < plain.length) end = Math.max(start, plain.lastIndexOf(' ', end)) let cut = plain.slice(start, end) // never leave half a tag, or a without its end, at either edge cut = cut.replace(/<\/?m?a?r?k?$/, '') if (/^[^<]*<\/mark>/.test(cut)) cut = cut.replace('', '') if ((cut.match(//g)?.length ?? 0) > (cut.match(/<\/mark>/g)?.length ?? 0)) cut += '' return `${start > 0 ? '… ' : ''}${cut}${end < plain.length ? ' …' : ''}` } /** True when a quote has real words once markers and whitespace are gone: a quote block is never drawn without text (#1415). */ export function hasQuoteText(text: string | null | undefined): text is string { return typeof text === 'string' && text.replace(/<\/?mark>/g, '').replace(/[“”"'\s]/g, '').length > 0 }