Skip to content

Commit b56a8aa

Browse files
committed
OpenConceptLab/ocl_issues#2299 | updated highlighter for synonym
1 parent cce2f2e commit b56a8aa

1 file changed

Lines changed: 24 additions & 8 deletions

File tree

src/components/map-projects/Concept.jsx

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,43 @@ import Button from '@mui/material/Button'
66
import Chip from '@mui/material/Chip'
77
import isString from 'lodash/isString'
88
import map from 'lodash/map'
9-
import orderBy from 'lodash/orderBy'
109

1110
import Retired from '../common/Retired'
1211
import Score from './Score'
1312
import MapButton from './MapButton'
1413
import ConceptSummaryProperties from '../concepts/ConceptSummaryProperties'
1514

15+
1616
const getBestSynonym = synonyms => {
17-
return orderBy(synonyms, match => match.length)
17+
return synonyms
1818
.map(text => {
1919
const matches = [...text.matchAll(/<em>(.*?)<\/em>/g)];
20-
const longestMatch = matches.reduce((a, b) => (b[1].length > a.length ? b[1] : a), "");
21-
const startsWithMatch = text.indexOf(`<em>${longestMatch}</em>`) === 0;
22-
return { text, longestMatch, length: longestMatch.length, startsWithMatch };
20+
const longestMatch = matches.reduce(
21+
(a, b) => (b[1].length > a.length ? b[1] : a),
22+
""
23+
);
24+
25+
const emTag = `<em>${longestMatch}</em>`;
26+
const startsWithMatch = text.indexOf(emTag) === 0;
27+
28+
// prefer exact "<em>...</em>"
29+
const isExactEmOnly = text.trim() === emTag;
30+
31+
return {
32+
text,
33+
longestMatch,
34+
length: longestMatch.length,
35+
startsWithMatch,
36+
isExactEmOnly
37+
};
2338
})
2439
.sort((a, b) => {
25-
if (b.length !== a.length) return b.length - a.length; // longest match first
40+
if (b.length !== a.length) return b.length - a.length; // longest match first
41+
if (b.isExactEmOnly !== a.isExactEmOnly) return b.isExactEmOnly ? 1 : -1; // ✅ exact "<em>...</em>" wins
2642
if (b.startsWithMatch !== a.startsWithMatch) return b.startsWithMatch ? 1 : -1; // prefer start
2743
return 0;
28-
})[0].text; // return best match's text
29-
}
44+
})[0]?.text;
45+
};
3046

3147

3248
const Item = ({concept, setShowHighlights, onMap, isSelectedForMap, noScore, repoVersion, synonymPrefix, isAIRecommended, bridge, bridgeChild, mapping, showAlgo, candidatesScore, algoScoreFirst, placeholderMap}) => {

0 commit comments

Comments
 (0)