Skip to content

Commit 42ae084

Browse files
committed
OpenConceptLab/ocl_issues#2183 | Search results | showing best match synonym from highlight as part of name
1 parent 485a3c8 commit 42ae084

1 file changed

Lines changed: 19 additions & 2 deletions

File tree

src/components/map-projects/Concept.jsx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,21 @@ import Retired from '../common/Retired'
55
import Score from './Score'
66
import MapButton from './MapButton'
77

8+
const getBestSynonym = synonyms => {
9+
return synonyms
10+
.map(text => {
11+
const matches = [...text.matchAll(/<em>(.*?)<\/em>/g)];
12+
const longestMatch = matches.reduce((a, b) => (b[1].length > a.length ? b[1] : a), "");
13+
const startsWithMatch = text.indexOf(`<em>${longestMatch}</em>`) === 0;
14+
return { text, longestMatch, length: longestMatch.length, startsWithMatch };
15+
})
16+
.sort((a, b) => {
17+
if (b.length !== a.length) return b.length - a.length; // longest match first
18+
if (b.startsWithMatch !== a.startsWithMatch) return b.startsWithMatch ? 1 : -1; // prefer start
19+
return 0;
20+
})[0].text; // return best match's text
21+
}
22+
823
const Concept = ({firstChild, concept, setShowHighlights, isShown, onCardClick, onMap, isSelectedForMap, noScore}) => {
924
const id = concept?.version_url || concept?.url || concept?.id
1025
const isSelectedToShow = isShown(id)
@@ -13,8 +28,10 @@ const Concept = ({firstChild, concept, setShowHighlights, isShown, onCardClick,
1328
const highlights = concept?.search_meta?.search_highlight
1429
const synonymHighlight = highlights?.synonyms
1530
const nameHighlight = highlights?.name
16-
if(!nameHighlight?.length && synonymHighlight?.length)
17-
synonymPrefix = synonymHighlight[0].replace('<em>', "<b className='searchable'>").replace('</em>', '</b>')
31+
if(!nameHighlight?.length && synonymHighlight?.length) {
32+
const bestMatch = getBestSynonym(synonymHighlight) || synonymHighlight[0]
33+
synonymPrefix = bestMatch.replace('<em>', "<b className='searchable'>").replace('</em>', '</b>')
34+
}
1835

1936
return (
2037
<ListItemButton selected={isSelectedToShow} onClick={event => onCardClick(event, id)} sx={{padding: '8px', borderTop: firstChild ? undefined : '1px solid rgba(0, 0, 0, 0.1)'}}>

0 commit comments

Comments
 (0)