Skip to content

Commit 9a56dce

Browse files
committed
init yarn upgrade
1 parent 095dfeb commit 9a56dce

File tree

4 files changed

+2462
-2536
lines changed

4 files changed

+2462
-2536
lines changed

js/algolia/highlight.ts

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
}

js/algolia/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { html } from 'htm/preact';
22
import algoliasearch from 'algoliasearch/lite';
3-
import { autocomplete, getAlgoliaHits, highlightHit } from '@algolia/autocomplete-js';
3+
import { autocomplete, getAlgoliaHits } 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';
67

78

89
const appId = 'UINQ2M4D9S';

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
"scripts": {
1414
"develop": "bundle exec jekyll clean && webpack --mode production && JEKYLL_ENV=testing bundle exec jekyll build --trace -V",
1515
"develop-inc": "webpack --mode production && JEKYLL_ENV=testing bundle exec jekyll build --trace -V -I"
16-
17-
},
16+
},
1817
"devDependencies": {
1918
"@babel/cli": "^7.6.0",
2019
"@babel/core": "^7.6.0",

0 commit comments

Comments
 (0)