Skip to content

Commit f9603f5

Browse files
docs(parseAlgoliaHitReverseSnippet): rewrite guides (#481)
* docs(parseAlgoliaHitReverseSnippet): rewrite guides * docs(parseAlgoliaHitReverseSnippet): add descriptions * docs(parseAlgoliaHitReverseSnippet): fix text * fix: apply suggestions from code review Co-authored-by: François Chalifour <[email protected]> * docs(reverseSnippetHit): fix example Co-authored-by: François Chalifour <[email protected]>
1 parent 090bd93 commit f9603f5

File tree

2 files changed

+100
-18
lines changed

2 files changed

+100
-18
lines changed

packages/website/docs/parseAlgoliaHitReverseSnippet.md

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,44 @@
22
id: parseAlgoliaHitReverseSnippet
33
---
44

5+
import PresetAlgoliaNote from './partials/preset-algolia/note.md'
6+
57
Returns the non-matching parts of an Algolia hit snippet.
68

7-
This is a common pattern for Query Suggestions.
9+
The `parseAlgoliaHitReverseSnippet` function lets you parse the non-highlighted parts of an Algolia hit's snippet. This is a common pattern for [Query Suggestions](https://www.algolia.com/doc/guides/building-search-ui/ui-and-ux-patterns/query-suggestions/js/), where you want to highlight the differences between each suggestion.
10+
11+
<PresetAlgoliaNote />
12+
13+
## Installation
14+
15+
First, you need to install the preset.
16+
17+
```bash
18+
yarn add @algolia/autocomplete-preset-algolia@alpha
19+
# or
20+
npm install @algolia/autocomplete-preset-algolia@alpha
21+
```
22+
23+
Then import it in your project:
24+
25+
```js
26+
import { parseAlgoliaHitReverseSnippet } from '@algolia/autocomplete-preset-algolia';
27+
```
28+
29+
If you don't use a package manager, you can use a standalone endpoint:
30+
31+
```html
32+
<script src="https://cdn.jsdelivr.net/npm/@algolia/autocomplete-preset-algolia@alpha"></script>
33+
```
834

9-
## Example with a single string
35+
## Examples
36+
37+
### With a single string
1038

1139
```js
1240
import { parseAlgoliaHitReverseSnippet } from '@algolia/autocomplete-preset-algolia';
1341

14-
// Fetch an Algolia hit
42+
// An Algolia hit for query "lap"
1543
const hit = {
1644
name: 'Laptop',
1745
_snippetResult: {
@@ -20,20 +48,20 @@ const hit = {
2048
},
2149
},
2250
};
23-
const snippetParts = parseAlgoliaHitReverseSnippet({
51+
const reverseSnippetedParts = parseAlgoliaHitReverseSnippet({
2452
hit,
2553
attribute: 'name',
2654
});
2755

28-
// => [{ value: 'Lap', isHighlighted: false }, { value: 'top', isHighlighted: true }]
56+
// [{ value: 'Lap', isHighlighted: false }, { value: 'top', isHighlighted: true }]
2957
```
3058

3159
## Example with nested attributes
3260

3361
```js
3462
import { parseAlgoliaHitReverseSnippet } from '@algolia/autocomplete-preset-algolia';
3563

36-
// Fetch an Algolia hit
64+
// An Algolia hit for query "lap"
3765
const hit = {
3866
name: {
3967
type: 'Laptop',
@@ -46,24 +74,30 @@ const hit = {
4674
},
4775
},
4876
};
49-
const snippetParts = parseAlgoliaHitReverseSnippet({
77+
const reverseSnippetedParts = parseAlgoliaHitReverseSnippet({
5078
hit,
5179
attribute: ['name', 'type'],
5280
});
5381

54-
// => [{ value: 'Lap', isHighlighted: false }, { value: 'top', isHighlighted: true }]
82+
// [{ value: 'Lap', isHighlighted: false }, { value: 'top', isHighlighted: true }]
5583
```
5684

57-
## Params
85+
## Parameters
5886

5987
### `hit`
6088

6189
> `AlgoliaHit` | required
6290
63-
The Algolia hit to retrieve the attribute value from.
91+
The Algolia hit whose attribute to retrieve the reverse snippet from.
6492

6593
### `attribute`
6694

6795
> `string | string[]` | required
6896
69-
The attribute to retrieve the reverse snippet value from. You can use the array syntax to reference the nested attributes.
97+
The attribute to retrieve the reverse snippet from. You can use the array syntax to reference nested attributes.
98+
99+
## Returns
100+
101+
> `ParsedAttribute[]`
102+
103+
An array of the parsed attribute's parts.

packages/website/docs/reverseSnippetHit.md

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,84 @@
22
id: reverseSnippetHit
33
---
44

5-
Returns a virtual node with non-matching parts of an Algolia hit snippet.
5+
Returns virtual nodes with highlighted non-matching matching parts of an Algolia hit's snippet.
66

7-
## Example
7+
The `reverseSnippetHit` function lets you turn an Algolia hit's snippet into a virtual node with highlighted non-matching parts for a given attribute.
8+
9+
## Examples
10+
11+
### With a single string
12+
13+
To determine what attribute to parse, you can pass it as a string.
814

915
```js
1016
import { reverseSnippetHit } from '@algolia/autocomplete-js';
1117

12-
const hit = {}; // fetch an Algolia hit
18+
// An Algolia hit for query "zelda"
19+
const hit = {
20+
query: 'zelda switch',
21+
_snippetResult: {
22+
query: {
23+
value:
24+
'__aa-highlight__zelda__/aa-highlight__ switch',
25+
},
26+
},
27+
};
1328
const reverseSnippetedValue = reverseSnippetHit({
1429
hit,
1530
attribute: 'query',
1631
});
1732
```
1833

19-
## Params
34+
### With nested attributes
35+
36+
If you're referencing a nested attribute, you can use the array syntax.
37+
38+
```js
39+
import { reverseSnippetHit } from '@algolia/autocomplete-js';
40+
41+
// An Algolia hit for query "hello"
42+
const hit = {
43+
hierarchicalCategories: {
44+
lvl1: 'Video games',
45+
},
46+
_snippetResult: {
47+
hierarchicalCategories: {
48+
lvl1: {
49+
value:
50+
'__aa-highlight__Video__/aa-highlight__ games',
51+
},
52+
},
53+
},
54+
};
55+
const reverseSnippetedValue = reverseSnippetHit({
56+
hit,
57+
attribute: ['hierarchicalCategories', 'lvl1'],
58+
});
59+
```
60+
61+
## Parameters
2062

2163
### `hit`
2264

2365
> `AlgoliaHit` | required
2466
25-
The Algolia hit to retrieve the attribute value from.
67+
The Algolia hit whose attribute to retrieve the reverse snippet parts from.
2668

2769
### `attribute`
2870

29-
> `string` | required
71+
> `string | string[]` | required
3072
31-
The attribute to retrieve the snippet value from.
73+
The attribute to retrieve the reverse snippet from. You can use the array syntax to reference nested attributes.
3274

3375
### `tagName`
3476

3577
> `string` | defaults to `mark`
3678
3779
The tag name of the virtual node.
80+
81+
## Returns
82+
83+
> `HighlightItemParams<THit>`
84+
85+
Virtual nodes with the snippet.

0 commit comments

Comments
 (0)