Skip to content

Commit 97edacb

Browse files
committed
Avoid replacing on non string field
1 parent c1840b6 commit 97edacb

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/format.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ function replaceHighlightTags(
66
highlightPreTag?: string,
77
highlightPostTag?: string
88
): string {
9-
// Value has to be a string.
10-
// Some field may have highlight applied by MeiliSearch (<em> tags)
9+
// Value has to be a string to have highlight.
10+
// Highlight is applied by MeiliSearch (<em> tags)
1111
// We replace the <em> by the expected tag for InstantSearch
1212
highlightPreTag = highlightPreTag || '__ais-highlight__'
1313
highlightPostTag = highlightPostTag || '__/ais-highlight__'
14+
if (isString(value)) {
15+
return value
16+
.replace(/<em>/g, highlightPreTag)
17+
.replace(/<\/em>/g, highlightPostTag)
18+
}
19+
// We JSON stringify to avoid loss of nested information
1420
return JSON.stringify(value)
15-
.replace(/<em>/g, highlightPreTag)
16-
.replace(/<\/em>/g, highlightPostTag)
1721
}
1822

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

0 commit comments

Comments
 (0)