Skip to content

Commit 84644de

Browse files
bors[bot]bidoubiwa
andauthored
Merge #344
344: Fix nested object being transfom into [object, object] in highlight r=bidoubiwa a=bidoubiwa The body of _highlightedResult used to look like this: ![Capture d’écran 2021-03-25 à 14 42 51](https://user-images.githubusercontent.com/33010418/112482638-90364600-8d78-11eb-93f3-bdfff1f4563a.png) The field should be transformed to String using `JSON.stringify` to keep all information. <img width="923" alt="Screenshot 2021-03-25 at 15 20 33" src="https://user-images.githubusercontent.com/33010418/112488467-d93cc900-8d7d-11eb-9bc2-b1d4e26299a8.png"> It looks like this with this fix. Co-authored-by: Charlotte Vermandel <[email protected]>
2 parents 30417a9 + 97edacb commit 84644de

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/format.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ function replaceHighlightTags(
66
highlightPreTag?: string,
77
highlightPostTag?: string
88
): string {
9-
let newHighlightValue = value || ''
10-
// If the value of the attribute is a string,
11-
// the highlight is applied by MeiliSearch (<em> tags)
12-
// and we replace the <em> by the expected tag for InstantSearch
9+
// Value has to be a string to have highlight.
10+
// Highlight is applied by MeiliSearch (<em> tags)
11+
// We replace the <em> by the expected tag for InstantSearch
1312
highlightPreTag = highlightPreTag || '__ais-highlight__'
1413
highlightPostTag = highlightPostTag || '__/ais-highlight__'
1514
if (isString(value)) {
16-
newHighlightValue = value
15+
return value
1716
.replace(/<em>/g, highlightPreTag)
1817
.replace(/<\/em>/g, highlightPostTag)
1918
}
20-
return newHighlightValue.toString()
19+
// We JSON stringify to avoid loss of nested information
20+
return JSON.stringify(value)
2121
}
2222

2323
function createHighlighResult<T extends Record<string, any>>({

0 commit comments

Comments
 (0)