mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 08:36:12 +01:00
feat: hightlight matches in quick search
fix title hightlights
This commit is contained in:
@@ -419,6 +419,10 @@ app.cacheBuster = null;
|
|||||||
quickSearchResults.toggleClass('hidden', !html.length || !inputEl.is(':focus'))
|
quickSearchResults.toggleClass('hidden', !html.length || !inputEl.is(':focus'))
|
||||||
.find('.quick-search-results-container')
|
.find('.quick-search-results-container')
|
||||||
.html(html.length ? html : '');
|
.html(html.length ? html : '');
|
||||||
|
var highlightEls = quickSearchResults.find(
|
||||||
|
'.quick-search-results .quick-search-title, .quick-search-results .snippet'
|
||||||
|
);
|
||||||
|
search.highlightMatches(options.searchOptions.term, highlightEls);
|
||||||
$(window).trigger('action:search.quick.complete', {
|
$(window).trigger('action:search.quick.complete', {
|
||||||
data: data,
|
data: data,
|
||||||
options: options,
|
options: options,
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ define('forum/search', ['search', 'autocomplete', 'storage'], function (searchMo
|
|||||||
updateFormItemVisiblity(searchIn.val());
|
updateFormItemVisiblity(searchIn.val());
|
||||||
});
|
});
|
||||||
|
|
||||||
highlightMatches(searchQuery);
|
searchModule.highlightMatches(searchQuery, $('.search-result-text p, .search-result-text.search-result-title a'));
|
||||||
|
|
||||||
$('#advanced-search').off('submit').on('submit', function (e) {
|
$('#advanced-search').off('submit').on('submit', function (e) {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
@@ -134,39 +134,6 @@ define('forum/search', ['search', 'autocomplete', 'storage'], function (searchMo
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function highlightMatches(searchQuery) {
|
|
||||||
if (!searchQuery) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
searchQuery = utils.escapeHTML(searchQuery.replace(/^"/, '').replace(/"$/, '').trim());
|
|
||||||
var regexStr = searchQuery.split(' ')
|
|
||||||
.map(function (word) { return utils.escapeRegexChars(word); })
|
|
||||||
.join('|');
|
|
||||||
var regex = new RegExp('(' + regexStr + ')', 'gi');
|
|
||||||
|
|
||||||
$('.search-result-text p, .search-result-text h4').each(function () {
|
|
||||||
var result = $(this);
|
|
||||||
var nested = [];
|
|
||||||
|
|
||||||
result.find('*').each(function () {
|
|
||||||
$(this).after('<!-- ' + nested.length + ' -->');
|
|
||||||
nested.push($('<div></div>').append($(this)));
|
|
||||||
});
|
|
||||||
|
|
||||||
result.html(result.html().replace(regex, function (match, p1) {
|
|
||||||
return '<strong class="search-match">' + p1 + '</strong>';
|
|
||||||
}));
|
|
||||||
|
|
||||||
nested.forEach(function (nestedEl, i) {
|
|
||||||
result.html(result.html().replace('<!-- ' + i + ' -->', function () {
|
|
||||||
return nestedEl.html();
|
|
||||||
}));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
$('.search-result-text').find('img:not(.not-responsive)').addClass('img-responsive');
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleSavePreferences() {
|
function handleSavePreferences() {
|
||||||
$('#save-preferences').on('click', function () {
|
$('#save-preferences').on('click', function () {
|
||||||
storage.setItem('search-preferences', JSON.stringify(getSearchDataFromDOM()));
|
storage.setItem('search-preferences', JSON.stringify(getSearchDataFromDOM()));
|
||||||
|
|||||||
@@ -106,6 +106,39 @@ define('search', ['navigator', 'translator', 'storage'], function (nav, translat
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Search.highlightMatches = function (searchQuery, els) {
|
||||||
|
if (!searchQuery || !els.length) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
searchQuery = utils.escapeHTML(searchQuery.replace(/^"/, '').replace(/"$/, '').trim());
|
||||||
|
var regexStr = searchQuery.split(' ')
|
||||||
|
.map(function (word) { return utils.escapeRegexChars(word); })
|
||||||
|
.join('|');
|
||||||
|
var regex = new RegExp('(' + regexStr + ')', 'gi');
|
||||||
|
|
||||||
|
els.each(function () {
|
||||||
|
var result = $(this);
|
||||||
|
var nested = [];
|
||||||
|
|
||||||
|
result.find('*').each(function () {
|
||||||
|
$(this).after('<!-- ' + nested.length + ' -->');
|
||||||
|
nested.push($('<div></div>').append($(this)));
|
||||||
|
});
|
||||||
|
|
||||||
|
result.html(result.html().replace(regex, function (match, p1) {
|
||||||
|
return '<strong class="search-match">' + p1 + '</strong>';
|
||||||
|
}));
|
||||||
|
|
||||||
|
nested.forEach(function (nestedEl, i) {
|
||||||
|
result.html(result.html().replace('<!-- ' + i + ' -->', function () {
|
||||||
|
return nestedEl.html();
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
$('.search-result-text').find('img:not(.not-responsive)').addClass('img-responsive');
|
||||||
|
};
|
||||||
|
|
||||||
Search.queryTopic = function (tid, term) {
|
Search.queryTopic = function (tid, term) {
|
||||||
socket.emit('topics.search', {
|
socket.emit('topics.search', {
|
||||||
tid: tid,
|
tid: tid,
|
||||||
|
|||||||
Reference in New Issue
Block a user