This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/lib/highlight-text.js.es6
Guo Xiang Tan e0cb2a434a Fix regression introduced in dae0bb4c67.
Add tests so it doesn't bite me again.
2019-03-26 17:52:37 +08:00

17 lines
530 B
JavaScript

import { PHRASE_MATCH_REGEXP_PATTERN } from "discourse/lib/concerns/search-constants";
export const CLASS_NAME = "search-highlight";
export default function($elem, term) {
if (!_.isEmpty(term)) {
// special case ignore "l" which is used for magic sorting
let words = _.reject(
term.match(new RegExp(`${PHRASE_MATCH_REGEXP_PATTERN}|[^\\s]+`, "g")),
t => t === "l"
);
words = words.map(w => w.replace(/^"(.*)"$/, "$1"));
$elem.highlight(words, { className: CLASS_NAME, wordsOnly: true });
}
}