hexo-theme-stellar/source/js/search/algolia-search.js

78 lines
2.5 KiB
JavaScript
Raw Normal View History

utils.js(window.searchConfig.js).then(() => {
utils.jq(() => {
var $inputArea = $("input#search-input");
if ($inputArea.length === 0) {
return;
}
2024-04-18 12:18:13 +08:00
var $resultArea = $("#search-result");
var $searchWrapper = $("#search-wrapper");
var client = algoliasearch(window.searchConfig.appId, window.searchConfig.apiKey);
var index = client.initIndex(window.searchConfig.indexName);
2024-04-18 12:18:13 +08:00
function filterResults(hits, filterPath) {
if (!filterPath || filterPath === '/') return hits;
var regex = new RegExp(filterPath);
return hits.filter(hit => regex.test(hit.url));
}
2024-04-18 12:18:13 +08:00
function displayResults(hits) {
var $resultList = $("<ul>").addClass("search-result-list");
if (hits.length === 0) {
$searchWrapper.addClass('noresult');
} else {
$searchWrapper.removeClass('noresult');
hits.forEach(function(hit) {
var contentSnippet = hit._snippetResult.content.value;
var title = hit.hierarchy.lvl1 || 'Untitled';
var $item = $("<li>").html(`<a href="${hit.url}"><span class='search-result-title'>${title}</span><p class="search-result-content">${contentSnippet}</p></a>`);
$resultList.append($item);
});
}
$resultArea.html($resultList);
2024-04-18 12:18:13 +08:00
}
$inputArea.on("input", function() {
var query = $(this).val().trim();
var filterPath = $inputArea.data('filter');
2024-04-18 12:18:13 +08:00
if (query.length <= 0) {
$searchWrapper.attr('searching', 'false');
$resultArea.empty();
return;
}
2024-04-18 12:18:13 +08:00
$searchWrapper.attr('searching', 'true');
2024-04-18 12:18:13 +08:00
index.search(query, {
hitsPerPage: window.searchConfig.hitsPerPage,
attributesToHighlight: ['content'],
attributesToSnippet: ['content:30'],
highlightPreTag: '<span class="search-keyword">',
highlightPostTag: '</span>',
restrictSearchableAttributes: ['content']
}).then(function(responses) {
displayResults(filterResults(responses.hits, filterPath));
});
2024-04-18 12:18:13 +08:00
});
$inputArea.on("keydown", function(e) {
if (e.which == 13) {
e.preventDefault();
}
});
2024-04-18 12:18:13 +08:00
var observer = new MutationObserver(function(mutationsList) {
if (mutationsList.length === 1) {
if (mutationsList[0].addedNodes.length) {
$searchWrapper.removeClass('noresult');
} else if (mutationsList[0].removedNodes.length) {
$searchWrapper.addClass('noresult');
}
2024-04-05 15:41:39 +08:00
}
});
2024-04-18 12:18:13 +08:00
observer.observe($resultArea[0], { childList: true });
});
2024-04-18 12:18:13 +08:00
});