2
2
id : parseAlgoliaHitSnippet
3
3
---
4
4
5
- Returns the snippeted parts of an Algolia hit.
5
+ import PresetAlgoliaNote from './partials/preset-algolia/note.md'
6
6
7
- ## Example with a single string
7
+ Returns the highlighted parts of an Algolia hit's snippet.
8
+
9
+ The ` parseAlgoliaHitSnippet ` function lets you parse the highlighted parts of an Algolia hit's snippet.
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:
8
24
9
25
``` js
10
26
import { parseAlgoliaHitSnippet } from ' @algolia/autocomplete-preset-algolia' ;
27
+ ```
11
28
12
- // Fetch an Algolia hit
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
+ ```
34
+
35
+ ## Examples
36
+
37
+ ### With a single string
38
+
39
+ ``` js
40
+ import { parseAlgoliaHitSnippet } from ' @algolia/autocomplete-preset-algolia' ;
41
+
42
+ // An Algolia hit for query "lap"
13
43
const hit = {
14
44
name: ' Laptop' ,
15
45
_snippetResult: {
@@ -18,20 +48,20 @@ const hit = {
18
48
},
19
49
},
20
50
};
21
- const snippetParts = parseAlgoliaHitSnippet ({
51
+ const snippetedParts = parseAlgoliaHitSnippet ({
22
52
hit,
23
53
attribute: ' name' ,
24
54
});
25
55
26
- // => [{ value: 'Lap', isHighlighted: true }, { value: 'top', isHighlighted: false }]
56
+ // [{ value: 'Lap', isHighlighted: true }, { value: 'top', isHighlighted: false }]
27
57
```
28
58
29
- ## Example with nested attributes
59
+ ### With nested attributes
30
60
31
61
``` js
32
62
import { parseAlgoliaHitSnippet } from ' @algolia/autocomplete-preset-algolia' ;
33
63
34
- // Fetch an Algolia hit
64
+ // An Algolia hit for query "lap"
35
65
const hit = {
36
66
name: {
37
67
type: ' Laptop' ,
@@ -44,24 +74,30 @@ const hit = {
44
74
},
45
75
},
46
76
};
47
- const snippetParts = parseAlgoliaHitSnippet ({
77
+ const snippetedParts = parseAlgoliaHitSnippet ({
48
78
hit,
49
79
attribute: [' name' , ' type' ],
50
80
});
51
81
52
- // => [{ value: 'Lap', isHighlighted: true }, { value: 'top', isHighlighted: false }]
82
+ // [{ value: 'Lap', isHighlighted: true }, { value: 'top', isHighlighted: false }]
53
83
```
54
84
55
- ## Params
85
+ ## Parameters
56
86
57
87
### ` hit `
58
88
59
89
> ` AlgoliaHit ` | required
60
90
61
- The Algolia hit to retrieve the attribute value from.
91
+ The Algolia hit whose attribute to retrieve the snippet from.
62
92
63
93
### ` attribute `
64
94
65
95
> ` string | string[] ` | required
66
96
67
- The attribute to retrieve the snippet value from. You can use the array syntax to reference the nested attributes.
97
+ The attribute to retrieve the 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.
0 commit comments