Skip to content

Commit 4447b1f

Browse files
docs(parseAlgoliaHitSnippet): rewrite guides (#480)
* docs(autocomplete-preset-algolia): add common note * docs(parseAlgoliaHitSnippet): rewrite guides * docs(parseAlgoliaHitSnippet): fix examples * docs(parseAlgoliaHitSnippet): add descriptions * docs(parseAlgoliaHitSnippet): fix text * fix: apply suggestions from code review Co-authored-by: François Chalifour <[email protected]> Co-authored-by: François Chalifour <[email protected]>
1 parent 1e48a2a commit 4447b1f

File tree

2 files changed

+103
-25
lines changed

2 files changed

+103
-25
lines changed

packages/website/docs/parseAlgoliaHitSnippet.md

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,44 @@
22
id: parseAlgoliaHitSnippet
33
---
44

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

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:
824

925
```js
1026
import { parseAlgoliaHitSnippet } from '@algolia/autocomplete-preset-algolia';
27+
```
1128

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"
1343
const hit = {
1444
name: 'Laptop',
1545
_snippetResult: {
@@ -18,20 +48,20 @@ const hit = {
1848
},
1949
},
2050
};
21-
const snippetParts = parseAlgoliaHitSnippet({
51+
const snippetedParts = parseAlgoliaHitSnippet({
2252
hit,
2353
attribute: 'name',
2454
});
2555

26-
// => [{ value: 'Lap', isHighlighted: true }, { value: 'top', isHighlighted: false }]
56+
// [{ value: 'Lap', isHighlighted: true }, { value: 'top', isHighlighted: false }]
2757
```
2858

29-
## Example with nested attributes
59+
### With nested attributes
3060

3161
```js
3262
import { parseAlgoliaHitSnippet } from '@algolia/autocomplete-preset-algolia';
3363

34-
// Fetch an Algolia hit
64+
// An Algolia hit for query "lap"
3565
const hit = {
3666
name: {
3767
type: 'Laptop',
@@ -44,24 +74,30 @@ const hit = {
4474
},
4575
},
4676
};
47-
const snippetParts = parseAlgoliaHitSnippet({
77+
const snippetedParts = parseAlgoliaHitSnippet({
4878
hit,
4979
attribute: ['name', 'type'],
5080
});
5181

52-
// => [{ value: 'Lap', isHighlighted: true }, { value: 'top', isHighlighted: false }]
82+
// [{ value: 'Lap', isHighlighted: true }, { value: 'top', isHighlighted: false }]
5383
```
5484

55-
## Params
85+
## Parameters
5686

5787
### `hit`
5888

5989
> `AlgoliaHit` | required
6090
61-
The Algolia hit to retrieve the attribute value from.
91+
The Algolia hit whose attribute to retrieve the snippet from.
6292

6393
### `attribute`
6494

6595
> `string | string[]` | required
6696
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.

packages/website/docs/snippetHit.md

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,84 @@
22
id: snippetHit
33
---
44

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

7-
## Example
7+
The `snippetHit` function lets you turn an Algolia hit's snippet into a virtual node with highlighted 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 { snippetHit } from '@algolia/autocomplete-js';
1117

12-
const hit = {}; // fetch an Algolia hit
18+
// An Algolia hit for query "he"
19+
const hit = {
20+
query: 'Hello there',
21+
_snippetResult: {
22+
query: {
23+
value:
24+
'__aa-highlight__He__/aa-highlight__llo t__aa-highlight__he__/aa-highlight__re',
25+
},
26+
},
27+
};
1328
const snippetedValue = snippetHit({
1429
hit,
1530
attribute: 'query',
1631
});
1732
```
1833

19-
## Params
34+
### With nested attributes
2035

21-
### `hit`
36+
If you're referencing a nested attribute, you can use the array syntax.
2237

23-
> `AlgoliaHit` | required
38+
```js
39+
import { snippetHit } from '@algolia/autocomplete-js';
2440

25-
The Algolia hit to retrieve the attribute value from.
41+
// An Algolia hit for query "he"
42+
const hit = {
43+
query: {
44+
title: 'Hello there',
45+
}
46+
_snippetResult: {
47+
query: {
48+
title: {
49+
value:
50+
'__aa-highlight__He__/aa-highlight__llo t__aa-highlight__he__/aa-highlight__re',
51+
},
52+
},
53+
},
54+
};
55+
const snippetedValue = snippetHit({
56+
hit,
57+
attribute: ['query', 'title'],
58+
});
59+
```
2660

27-
### `attribute`
61+
## Parameters
2862

29-
> `string` | required
63+
### `hit`
3064

31-
The attribute to retrieve the snippet value from.
65+
> `AlgoliaHit` | required
3266
33-
### `highlightPreTag`
67+
The Algolia hit whose attribute to retrieve the snippet from.
3468

35-
> `string` | defaults to `<mark>`
69+
### `attribute`
3670

37-
The HTML tag to prefix the value with.
71+
> `string | string[]` | required
72+
73+
The attribute to retrieve the snippet from. You can use the array syntax to reference nested attributes.
3874

3975
### `tagName`
4076

4177
> `string` | defaults to `mark`
4278
4379
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)