@@ -5,6 +5,21 @@ import Retired from '../common/Retired'
55import Score from './Score'
66import MapButton from './MapButton'
77
8+ const getBestSynonym = synonyms => {
9+ return synonyms
10+ . map ( text => {
11+ const matches = [ ...text . matchAll ( / < e m > ( .* ?) < \/ e m > / 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+
823const 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