Skip to content

Commit 144246b

Browse files
committed
working highlight import
1 parent 9a56dce commit 144246b

File tree

3 files changed

+101
-121
lines changed

3 files changed

+101
-121
lines changed

js/algolia/highlight.js

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

js/algolia/highlight.ts

Lines changed: 0 additions & 118 deletions
This file was deleted.

js/algolia/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { html } from 'htm/preact';
22
import algoliasearch from 'algoliasearch/lite';
3-
import { autocomplete, getAlgoliaHits } from '@algolia/autocomplete-js';
3+
import { autocomplete, getAlgoliaResults } from '@algolia/autocomplete-js';
44
import {createAlgoliaInsightsPlugin} from '@algolia/autocomplete-plugin-algolia-insights';
55
import insightsClient from 'search-insights';
6-
import { highlightHit } from './highlight.ts';
6+
import { highlightHit } from './highlight.js';
77

88

99
const appId = 'UINQ2M4D9S';
@@ -39,7 +39,7 @@ function initAutocomplete(item){
3939
return itemUrl;
4040
},
4141
getItems() {
42-
return getAlgoliaHits({
42+
return getAlgoliaResults({
4343
searchClient,
4444
queries: [
4545
{

0 commit comments

Comments
 (0)