[feat] algolia_search filter (#434)
This commit is contained in:
parent
f36e6b940f
commit
461e961c0e
|
@ -9,6 +9,12 @@ utils.jq(() => {
|
||||||
var client = algoliasearch(window.searchConfig.appId, window.searchConfig.apiKey);
|
var client = algoliasearch(window.searchConfig.appId, window.searchConfig.apiKey);
|
||||||
var index = client.initIndex(window.searchConfig.indexName);
|
var index = client.initIndex(window.searchConfig.indexName);
|
||||||
|
|
||||||
|
function filterResults(hits, filterPath) {
|
||||||
|
if (!filterPath || filterPath === '/') return hits;
|
||||||
|
var regex = new RegExp(filterPath);
|
||||||
|
return hits.filter(hit => regex.test(hit.url));
|
||||||
|
}
|
||||||
|
|
||||||
function displayResults(hits) {
|
function displayResults(hits) {
|
||||||
var $resultList = $("<ul>").addClass("search-result-list");
|
var $resultList = $("<ul>").addClass("search-result-list");
|
||||||
if (hits.length === 0) {
|
if (hits.length === 0) {
|
||||||
|
@ -27,6 +33,7 @@ utils.jq(() => {
|
||||||
|
|
||||||
$inputArea.on("input", function() {
|
$inputArea.on("input", function() {
|
||||||
var query = $(this).val().trim();
|
var query = $(this).val().trim();
|
||||||
|
var filterPath = $inputArea.data('filter');
|
||||||
|
|
||||||
if (query.length <= 0) {
|
if (query.length <= 0) {
|
||||||
$searchWrapper.attr('searching', 'false');
|
$searchWrapper.attr('searching', 'false');
|
||||||
|
@ -44,7 +51,7 @@ utils.jq(() => {
|
||||||
highlightPostTag: '</span>',
|
highlightPostTag: '</span>',
|
||||||
restrictSearchableAttributes: ['content']
|
restrictSearchableAttributes: ['content']
|
||||||
}).then(function(responses) {
|
}).then(function(responses) {
|
||||||
displayResults(responses.hits);
|
displayResults(filterResults(responses.hits, filterPath));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -66,4 +73,3 @@ utils.jq(() => {
|
||||||
|
|
||||||
observer.observe($resultArea[0], { childList: true });
|
observer.observe($resultArea[0], { childList: true });
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue