Skip to content

Commit 965138c

Browse files
docs(parseAlgoliaHitReverseHighlight): rewrite guides (#479)
* docs(autocomplete-preset-algolia): add common note * docs(parseAlgoliaHitReverseHighlight): rewrite guides * docs(parseAlgoliaHitReverseHighlight): fix examples * docs(parseAlgoliaHitReverseHighlight): fix params * docs(parseAlgoliaHitReverseHighlight): fix examples * docs(parseAlgoliaHitReverseHighlight): add descriptions * docs(parseAlgoliaHitReverseHighlight): fix text * fix: apply suggestions from code review Co-authored-by: François Chalifour <[email protected]> * fix: apply suggestions from code review Co-authored-by: François Chalifour <[email protected]> * docs(reverseHighlightHit): change examples * fix: apply suggestions from code review Co-authored-by: François Chalifour <[email protected]> Co-authored-by: François Chalifour <[email protected]>
1 parent d652483 commit 965138c

File tree

2 files changed

+103
-23
lines changed

2 files changed

+103
-23
lines changed

packages/website/docs/parseAlgoliaHitReverseHighlight.md

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,44 @@
22
id: parseAlgoliaHitReverseHighlight
33
---
44

5-
Returns the highlighted parts of an Algolia hit.
5+
import PresetAlgoliaNote from './partials/preset-algolia/note.md'
66

7-
This is a common pattern for Query Suggestions.
7+
Returns the highlighted non-matching parts of an Algolia hit.
88

9-
## Example with a single string
9+
The `parseAlgoliaHitReverseHighlight` function lets you parse the non-highlighted parts of an Algolia hit. 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:
1024

1125
```js
1226
import { parseAlgoliaHitReverseHighlight } 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+
```
1334

14-
// Fetch an Algolia hit
35+
## Examples
36+
37+
### With a single string
38+
39+
```js
40+
import { parseAlgoliaHitReverseHighlight } from '@algolia/autocomplete-preset-algolia';
41+
42+
// An Algolia hit for query "lap"
1543
const hit = {
1644
name: 'Laptop',
1745
_highlightResult: {
@@ -20,20 +48,20 @@ const hit = {
2048
},
2149
},
2250
};
23-
const snippetParts = parseAlgoliaHitReverseHighlight({
51+
const reverseHighlightedParts = parseAlgoliaHitReverseHighlight({
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

31-
## Example with nested attributes
59+
### With nested attributes
3260

3361
```js
3462
import { parseAlgoliaHitReverseHighlight } 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,26 +74,30 @@ const hit = {
4674
},
4775
},
4876
};
49-
const snippetParts = parseAlgoliaHitReverseHighlight({
77+
const reverseHighlightedParts = parseAlgoliaHitReverseHighlight({
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-
# Reference
58-
59-
## Params
85+
## Parameters
6086

6187
### `hit`
6288

6389
> `AlgoliaHit` | required
6490
65-
The Algolia hit to retrieve the attribute value from.
91+
The Algolia hit whose attribute to retrieve the reverse highlighted parts from.
6692

6793
### `attribute`
6894

6995
> `string | string[]` | required
7096
71-
The attribute to retrieve the highlight value from. You can use the array syntax to reference the nested attributes.
97+
The attribute to retrieve the reverse highlighted parts 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/reverseHighlightHit.md

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,84 @@
22
id: reverseHighlightHit
33
---
44

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

7-
## Example
7+
The `reverseHighlightHit` function lets you turn an Algolia hit 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 { reverseHighlightHit } from '@algolia/autocomplete-js';
1117

12-
const hit = {}; // fetch an Algolia hit
13-
const highlightedValue = reverseHighlightHit({
18+
// An Algolia hit for query "zelda"
19+
const hit = {
20+
query: 'zelda switch',
21+
_highlightResult: {
22+
query: {
23+
value:
24+
'__aa-highlight__zelda__/aa-highlight__ switch',
25+
},
26+
},
27+
};
28+
const reverseHighlightedValue = reverseHighlightHit({
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 { reverseHighlightHit } from '@algolia/autocomplete-js';
40+
41+
// An Algolia hit for query "video"
42+
const hit = {
43+
hierarchicalCategories: {
44+
lvl1: 'Video games',
45+
}
46+
_highlightResult: {
47+
hierarchicalCategories: {
48+
lvl1: {
49+
value:
50+
'__aa-highlight__Video__/aa-highlight__ games',
51+
},
52+
},
53+
},
54+
};
55+
const reverseHighlightedValue = reverseHighlightHit({
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 highlighted parts from.
2668

2769
### `attribute`
2870

29-
> `string` | required
71+
> `string | string[]` | required
3072
31-
The attribute to retrieve the highlight value from.
73+
The attribute to retrieve the highlighted parts 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 highlighted matching parts.

0 commit comments

Comments
 (0)