import truncate from '../utils/truncate'; export default function(string, phrase, length) { if (!phrase) { return string; } const regexp = phrase instanceof RegExp ? phrase : new RegExp(phrase, 'gi'); let highlightedString = string; let start = 0; if (length) { start = Math.max(0, string.search(regexp) - length / 2); highlightedString = truncate(highlightedString, length, start); } highlightedString = $('
').text(highlightedString).html().replace(regexp, '$&'); return m.trust(highlightedString); }