1
+ import {
2
+ parseAlgoliaHitHighlight ,
3
+ parseAlgoliaHitReverseHighlight ,
4
+ parseAlgoliaHitSnippet ,
5
+ parseAlgoliaHitReverseSnippet ,
6
+ } from '@algolia/autocomplete-preset-algolia' ;
7
+ import { Hit } from '@algolia/client-search' ;
8
+
9
+ type ParsedAttribute = {
10
+ value : string ;
11
+ isHighlighted : boolean ;
12
+ } ;
13
+
14
+ function concatParts (
15
+ parts : ParsedAttribute [ ] ,
16
+ { highlightPreTag, highlightPostTag }
17
+ ) {
18
+ return parts . reduce < string > ( ( acc , current ) => {
19
+ return (
20
+ acc +
21
+ ( current . isHighlighted
22
+ ? `${ highlightPreTag } ${ current . value } ${ highlightPostTag } `
23
+ : current . value )
24
+ ) ;
25
+ } , '' ) ;
26
+ }
27
+
28
+ type HighlightItemParams < TItem > = {
29
+ hit : TItem ;
30
+ attribute : keyof TItem ;
31
+ highlightPreTag ?: string ;
32
+ highlightPostTag ?: string ;
33
+ ignoreEscape ?: string [ ] ;
34
+ } ;
35
+
36
+ /**
37
+ * Highlights and escapes the matching parts of an Algolia hit.
38
+ */
39
+ export function highlightHit < TItem extends Hit < { } > > ( {
40
+ hit,
41
+ attribute,
42
+ highlightPreTag = '<mark>' ,
43
+ highlightPostTag = '</mark>' ,
44
+ ignoreEscape,
45
+ } : HighlightItemParams < TItem > ) {
46
+ return concatParts (
47
+ parseAlgoliaHitHighlight < TItem > ( {
48
+ hit,
49
+ attribute,
50
+ ignoreEscape,
51
+ } ) ,
52
+ { highlightPreTag, highlightPostTag }
53
+ ) ;
54
+ }
55
+
56
+ /**
57
+ * Highlights and escapes the non-matching parts of an Algolia hit.
58
+ *
59
+ * This is a common pattern for Query Suggestions.
60
+ */
61
+ export function reverseHighlightHit < TItem extends Hit < { } > > ( {
62
+ hit,
63
+ attribute,
64
+ highlightPreTag = '<mark>' ,
65
+ highlightPostTag = '</mark>' ,
66
+ ignoreEscape,
67
+ } : HighlightItemParams < TItem > ) {
68
+ return concatParts (
69
+ parseAlgoliaHitReverseHighlight < TItem > ( {
70
+ hit,
71
+ attribute,
72
+ ignoreEscape,
73
+ } ) ,
74
+ { highlightPreTag, highlightPostTag }
75
+ ) ;
76
+ }
77
+
78
+ /**
79
+ * Highlights and escapes the matching parts of an Algolia hit snippet.
80
+ */
81
+ export function snippetHit < TItem extends Hit < { } > > ( {
82
+ hit,
83
+ attribute,
84
+ highlightPreTag = '<mark>' ,
85
+ highlightPostTag = '</mark>' ,
86
+ ignoreEscape,
87
+ } : HighlightItemParams < TItem > ) {
88
+ return concatParts (
89
+ parseAlgoliaHitSnippet < TItem > ( {
90
+ hit,
91
+ attribute,
92
+ ignoreEscape,
93
+ } ) ,
94
+ { highlightPreTag, highlightPostTag }
95
+ ) ;
96
+ }
97
+
98
+ /**
99
+ * Highlights and escapes the non-matching parts of an Algolia hit snippet.
100
+ *
101
+ * This is a common pattern for Query Suggestions.
102
+ */
103
+ export function reverseSnippetHit < TItem extends Hit < { } > > ( {
104
+ hit,
105
+ attribute,
106
+ highlightPreTag = '<mark>' ,
107
+ highlightPostTag = '</mark>' ,
108
+ ignoreEscape,
109
+ } : HighlightItemParams < TItem > ) {
110
+ return concatParts (
111
+ parseAlgoliaHitReverseSnippet < TItem > ( {
112
+ hit,
113
+ attribute,
114
+ ignoreEscape,
115
+ } ) ,
116
+ { highlightPreTag, highlightPostTag }
117
+ ) ;
118
+ }
0 commit comments