framework/ember/app/helpers/highlight-words.js

19 lines
528 B
JavaScript
Raw Normal View History

2014-12-20 14:26:46 +08:00
import Ember from 'ember';
2015-01-21 11:59:00 +08:00
export default Ember.Handlebars.makeBoundHelper(function(text, phrase) {
2014-12-20 14:26:46 +08:00
if (phrase) {
var words = phrase.split(' ');
var replacement = function(matched) {
return '<span class="highlight-keyword">'+matched+'</span>';
};
words.forEach(function(word) {
text = text.replace(
new RegExp("\\b"+word+"\\b", 'gi'),
replacement
);
});
}
2015-01-21 11:59:00 +08:00
return new Ember.Handlebars.SafeString(text);
2014-12-20 14:26:46 +08:00
});