2023-05-11 00:53:05 +03:00
|
|
|
import Ember from 'ember';
|
2017-08-22 10:53:26 +03:00
|
|
|
import {helper} from '@ember/component/helper';
|
2021-05-12 14:33:36 +03:00
|
|
|
import {htmlSafe} from '@ember/template';
|
2016-06-11 19:52:36 +03:00
|
|
|
|
2023-05-11 00:53:05 +03:00
|
|
|
const {Handlebars} = Ember;
|
|
|
|
|
2016-04-19 18:55:10 +03:00
|
|
|
export function highlightedText([text, termToHighlight]) {
|
2017-09-28 13:25:13 +03:00
|
|
|
// replace any non-word character with an escaped character
|
|
|
|
let sanitisedTerm = termToHighlight.replace(new RegExp(/\W/ig), '\\$&');
|
2023-05-11 00:53:05 +03:00
|
|
|
let termMatcher = new RegExp(sanitisedTerm, 'ig');
|
|
|
|
|
|
|
|
let matches = text.match(termMatcher) || [];
|
|
|
|
let nonMatches = text.split(termMatcher);
|
|
|
|
|
|
|
|
let htmlSafeResult = '';
|
|
|
|
|
|
|
|
nonMatches.forEach((nonMatch, index) => {
|
|
|
|
htmlSafeResult += Handlebars.Utils.escapeExpression(nonMatch);
|
|
|
|
|
|
|
|
if (matches[index]) {
|
|
|
|
htmlSafeResult += `<span class="highlight">${Handlebars.Utils.escapeExpression(matches[index])}</span>`;
|
|
|
|
}
|
|
|
|
});
|
2017-09-28 13:25:13 +03:00
|
|
|
|
2023-05-11 00:53:05 +03:00
|
|
|
return htmlSafe(htmlSafeResult);
|
2016-04-19 18:55:10 +03:00
|
|
|
}
|
|
|
|
|
2016-06-30 13:21:47 +03:00
|
|
|
export default helper(highlightedText);
|