diff --git a/source/js/search/algolia-search.js b/source/js/search/algolia-search.js index 573a960..2398bf0 100644 --- a/source/js/search/algolia-search.js +++ b/source/js/search/algolia-search.js @@ -1,69 +1,75 @@ utils.jq(() => { - var $inputArea = $("input#search-input"); - if ($inputArea.length === 0) { + var $inputArea = $("input#search-input"); + if ($inputArea.length === 0) { + return; + } + + var $resultArea = $("#search-result"); + var $searchWrapper = $("#search-wrapper"); + var client = algoliasearch(window.searchConfig.appId, window.searchConfig.apiKey); + 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) { + var $resultList = $("
${contentSnippet}
`); + $resultList.append($item); + }); + } + $resultArea.html($resultList); + } + + $inputArea.on("input", function() { + var query = $(this).val().trim(); + var filterPath = $inputArea.data('filter'); + + if (query.length <= 0) { + $searchWrapper.attr('searching', 'false'); + $resultArea.empty(); return; } - - var $resultArea = $("#search-result"); - var $searchWrapper = $("#search-wrapper"); - var client = algoliasearch(window.searchConfig.appId, window.searchConfig.apiKey); - var index = client.initIndex(window.searchConfig.indexName); - - function displayResults(hits) { - var $resultList = $("${contentSnippet}
`); - $resultList.append($item); - }); - } - $resultArea.html($resultList); - } - - $inputArea.on("input", function() { - var query = $(this).val().trim(); - - if (query.length <= 0) { - $searchWrapper.attr('searching', 'false'); - $resultArea.empty(); - return; - } - - $searchWrapper.attr('searching', 'true'); - - index.search(query, { - hitsPerPage: window.searchConfig.hitsPerPage, - attributesToHighlight: ['content'], - attributesToSnippet: ['content:30'], - highlightPreTag: '', - highlightPostTag: '', - restrictSearchableAttributes: ['content'] - }).then(function(responses) { - displayResults(responses.hits); - }); + + $searchWrapper.attr('searching', 'true'); + + index.search(query, { + hitsPerPage: window.searchConfig.hitsPerPage, + attributesToHighlight: ['content'], + attributesToSnippet: ['content:30'], + highlightPreTag: '', + highlightPostTag: '', + restrictSearchableAttributes: ['content'] + }).then(function(responses) { + displayResults(filterResults(responses.hits, filterPath)); }); - - $inputArea.on("keydown", function(e) { - if (e.which == 13) { - e.preventDefault(); - } - }); - - 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'); - } - } - }); - - observer.observe($resultArea[0], { childList: true }); }); - \ No newline at end of file + + $inputArea.on("keydown", function(e) { + if (e.which == 13) { + e.preventDefault(); + } + }); + + 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'); + } + } + }); + + observer.observe($resultArea[0], { childList: true }); +});