Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/chipzoller/hugo-clarity.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/assets
diff options
context:
space:
mode:
authorweru <fromweru@gmail.com>2022-05-15 00:02:50 +0300
committerweru <fromweru@gmail.com>2022-05-15 00:02:50 +0300
commitff520b7e58b0b1917bb5a3c924744e114e510b7c (patch)
tree8631331cc2dd3e48e1dd02fd35ffa55be9755733 /assets
parentede406b64d25d3ba36b0e33411a7a53a95129c67 (diff)
remove hardcoded search section
Signed-off-by: weru <fromweru@gmail.com>
Diffstat (limited to 'assets')
-rw-r--r--assets/js/search.js19
1 files changed, 12 insertions, 7 deletions
diff --git a/assets/js/search.js b/assets/js/search.js
index 05c60cb..9dde9b0 100644
--- a/assets/js/search.js
+++ b/assets/js/search.js
@@ -50,7 +50,7 @@ function initializeSearch(index) {
results = results.slice(0,12);
}
resultsFragment.appendChild(resultsTitle);
- console.log(results);
+
results.forEach(function(result){
let item = createEl('a');
item.href = `${result.link}?query=${query}`;
@@ -85,7 +85,7 @@ function initializeSearch(index) {
showResults.appendChild(resultsFragment);
}
- function search(searchTerm, scope = 'post', passive = false) {
+ function search(searchTerm, scope = null, passive = false) {
if(searchTerm.length) {
let rawResults = index.search(searchTerm);
rawResults = rawResults.map(function(result){
@@ -93,9 +93,13 @@ function initializeSearch(index) {
const resultItem = result.item;
resultItem.score = (parseFloat(score) * 50).toFixed(0);
return resultItem ;
- }).filter(resultItem => {
- return resultItem.section == scope;
- });
+ })
+
+ if(scope) {
+ rawResults = rawResults.filter(resultItem => {
+ return resultItem.section == scope;
+ });
+ }
passive ? searchResults(rawResults, searchTerm, true) : searchResults(rawResults, searchTerm);
@@ -119,7 +123,8 @@ function initializeSearch(index) {
searchField.addEventListener('search', function(){
const searchTerm = searchField.value.trim().toLowerCase();
if(searchTerm.length) {
- window.location.href = new URL(`search/?query=${searchTerm}&scope=${searchScope}`, rootURL).href;
+ const scopeParameter = searchScope ? `&scope=${searchScope}` : '';
+ window.location.href = new URL(`search/?query=${searchTerm}${ scopeParameter }`, rootURL).href;
}
});
}
@@ -142,7 +147,7 @@ function initializeSearch(index) {
// search actively after search page has loaded
const searchField = elem(searchFieldClass);
- searchScope ? search(searchTerm, searchScope, true) : false;
+ search(searchTerm, searchScope, true);
if(searchField) {
searchField.addEventListener('input', function() {