forked from segmentio/segment-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhighlight.js
98 lines (97 loc) · 1.97 KB
/
highlight.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import {
parseAlgoliaHitHighlight,
parseAlgoliaHitReverseHighlight,
parseAlgoliaHitSnippet,
parseAlgoliaHitReverseSnippet,
} from '@algolia/autocomplete-preset-algolia';
function concatParts(parts, {
highlightPreTag,
highlightPostTag
}) {
return parts.reduce((acc, current) => {
return (acc +
(current.isHighlighted ?
`${highlightPreTag}${current.value}${highlightPostTag}` :
current.value));
}, '');
}
/**
* Highlights and escapes the matching parts of an Algolia hit.
*/
export function highlightHit({
hit,
attribute,
highlightPreTag = '<mark>',
highlightPostTag = '</mark>',
ignoreEscape,
}) {
return concatParts(parseAlgoliaHitHighlight({
hit,
attribute,
ignoreEscape,
}), {
highlightPreTag,
highlightPostTag
});
}
/**
* Highlights and escapes the non-matching parts of an Algolia hit.
*
* This is a common pattern for Query Suggestions.
*/
export function reverseHighlightHit({
hit,
attribute,
highlightPreTag = '<mark>',
highlightPostTag = '</mark>',
ignoreEscape,
}) {
return concatParts(parseAlgoliaHitReverseHighlight({
hit,
attribute,
ignoreEscape,
}), {
highlightPreTag,
highlightPostTag
});
}
/**
* Highlights and escapes the matching parts of an Algolia hit snippet.
*/
export function snippetHit({
hit,
attribute,
highlightPreTag = '<mark>',
highlightPostTag = '</mark>',
ignoreEscape,
}) {
return concatParts(parseAlgoliaHitSnippet({
hit,
attribute,
ignoreEscape,
}), {
highlightPreTag,
highlightPostTag
});
}
/**
* Highlights and escapes the non-matching parts of an Algolia hit snippet.
*
* This is a common pattern for Query Suggestions.
*/
export function reverseSnippetHit({
hit,
attribute,
highlightPreTag = '<mark>',
highlightPostTag = '</mark>',
ignoreEscape,
}) {
return concatParts(parseAlgoliaHitReverseSnippet({
hit,
attribute,
ignoreEscape,
}), {
highlightPreTag,
highlightPostTag
});
}