Skip to content

Commit 6051baa

Browse files
committed
Update highlighting metadata logic
1 parent a63c9bf commit 6051baa

File tree

2 files changed

+26
-27
lines changed

2 files changed

+26
-27
lines changed

packages/autocomplete-client/src/search/__tests__/fetchMeilisearchResults.test.ts

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -79,26 +79,22 @@ describe('fetchMeilisearchResults', () => {
7979
})
8080
})
8181

82-
// test('with fully highlighted match', async () => {
83-
// const results = await fetchMeilisearchResults({
84-
// searchClient,
85-
// queries: [
86-
// {
87-
// indexName: 'testUid',
88-
// query: 'Hit 2',
89-
// params: {
90-
// highlightPreTag: '<test>',
91-
// highlightPostTag: '</test>',
92-
// },
93-
// },
94-
// ],
95-
// })
82+
test('with fully highlighted match', async () => {
83+
const results = await fetchMeilisearchResults({
84+
searchClient,
85+
queries: [
86+
{
87+
indexName: 'testUid',
88+
query: 'Hit',
89+
},
90+
],
91+
})
9692

97-
// expect(results[0].hits[0]._highlightResult?.label).toEqual({
98-
// value: '<test>Hit</test> <test>2</test>',
99-
// fullyHighlighted: true,
100-
// matchLevel: 'full',
101-
// matchedWords: ['Hit', '2'],
102-
// })
103-
// })
93+
expect(results[0].hits[0]._highlightResult?.label).toEqual({
94+
value: `${HIGHLIGHT_PRE_TAG}Hit${HIGHLIGHT_POST_TAG} ${HIGHLIGHT_PRE_TAG}2${HIGHLIGHT_POST_TAG}`,
95+
fullyHighlighted: true,
96+
matchLevel: 'partial',
97+
matchedWords: ['Hit'],
98+
})
99+
})
104100
})

packages/autocomplete-client/src/search/fetchMeilisearchResults.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,26 @@ function calculateHighlightMetadata(
7878
preTag: string = HIGHLIGHT_PRE_TAG,
7979
postTag: string = HIGHLIGHT_POST_TAG
8080
): HighlightMetadata {
81-
// Find all highlighted words
81+
// Extract all highlighted segments
8282
const highlightRegex = new RegExp(`${preTag}(.*?)${postTag}`, 'g')
8383
const matches: string[] = []
8484
let match
8585
while ((match = highlightRegex.exec(value)) !== null) {
8686
matches.push(match[1])
8787
}
88-
const matchedWords = matches
8988

90-
// Remove highlight tags for comparison
89+
// Remove highlight tags to get the original, unhighlighted text
9190
const cleanValue = value.replace(new RegExp(`${preTag}|${postTag}`, 'g'), '')
9291

93-
// Calculate if fully highlighted
92+
// Determine if the entire attribute is highlighted
93+
// fullyHighlighted = true if cleanValue and the concatenation of all matched segments are identical
9494
const highlightedText = matches.join('')
9595
const fullyHighlighted = cleanValue === highlightedText
9696

97-
// Determine match level
97+
// Determine match level:
98+
// - 'none' if no matches
99+
// - 'partial' if some matches but not fully highlighted
100+
// - 'full' if all text is fully highlighted
98101
let matchLevel: 'none' | 'partial' | 'full' = 'none'
99102
if (matches.length > 0) {
100103
matchLevel = fullyHighlighted ? 'full' : 'partial'
@@ -104,6 +107,6 @@ function calculateHighlightMetadata(
104107
value,
105108
fullyHighlighted,
106109
matchLevel,
107-
matchedWords,
110+
matchedWords: matches,
108111
}
109112
}

0 commit comments

Comments
 (0)