[feat] algolia_search filter (#434)

This commit is contained in:
星日语 2024-04-18 12:18:13 +08:00 committed by GitHub
parent f36e6b940f
commit 461e961c0e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 70 additions and 64 deletions

View File

@ -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));
}); });
}); });
@ -65,5 +72,4 @@ utils.jq(() => {
}); });
observer.observe($resultArea[0], { childList: true }); observer.observe($resultArea[0], { childList: true });
}); });