/** @jsx h */ import { h } from 'preact'; import { Highlight as HighlightUiComponent } from '../../components/Highlight/Highlight'; import { getPropertyByPath, unescape, toArray, warning, getHighlightedParts, } from '../../lib/utils'; import type { BaseHit, Hit, HitAttributeHighlightResult, PartialKeys, } from '../../types'; import type { HighlightProps as HighlightUiComponentProps } from '../../components/Highlight/Highlight'; export type HighlightProps> = { hit: THit; attribute: keyof THit | string[]; cssClasses?: HighlightUiComponentProps['classNames']; } & PartialKeys< Omit, 'highlightedTagName' | 'nonHighlightedTagName' | 'separator' >; export function Highlight>({ hit, attribute, cssClasses, ...props }: HighlightProps) { const property: HitAttributeHighlightResult | HitAttributeHighlightResult[] = getPropertyByPath(hit._highlightResult, attribute as string) || []; const properties = toArray(property); warning( Boolean(properties.length), `Could not enable highlight for "${attribute.toString()}", will display an empty string. Please check whether this attribute exists and is either searchable or specified in \`attributesToHighlight\`. See: https://alg.li/highlighting ` ); const parts = properties.map(({ value }) => getHighlightedParts(unescape(value || '')) ); return ( ); }