62f7600aa4
refs: https://github.com/TryGhost/Team/issues/2390 Escapes each matched and non-matched segment of the post title in the admin search field, to make sure they're displayed in plain text but still have matches highlighted.
29 lines
893 B
JavaScript
29 lines
893 B
JavaScript
import Ember from 'ember';
|
|
import {helper} from '@ember/component/helper';
|
|
import {htmlSafe} from '@ember/template';
|
|
|
|
const {Handlebars} = Ember;
|
|
|
|
export function highlightedText([text, termToHighlight]) {
|
|
// replace any non-word character with an escaped character
|
|
let sanitisedTerm = termToHighlight.replace(new RegExp(/\W/ig), '\\$&');
|
|
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>`;
|
|
}
|
|
});
|
|
|
|
return htmlSafe(htmlSafeResult);
|
|
}
|
|
|
|
export default helper(highlightedText);
|