From ff520b7e58b0b1917bb5a3c924744e114e510b7c Mon Sep 17 00:00:00 2001 From: weru Date: Sun, 15 May 2022 00:02:50 +0300 Subject: [PATCH 1/5] remove hardcoded search section Signed-off-by: weru --- assets/js/search.js | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/assets/js/search.js b/assets/js/search.js index 05c60cb9..9dde9b08 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() { From 4f2035efaaa9180ae5bce43ab8a62f3d14d029c2 Mon Sep 17 00:00:00 2001 From: weru Date: Sun, 15 May 2022 00:03:53 +0300 Subject: [PATCH 2/5] set default search scope to page section Signed-off-by: weru --- layouts/partials/search/widget.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/layouts/partials/search/widget.html b/layouts/partials/search/widget.html index afcc0d22..d5dbdef0 100644 --- a/layouts/partials/search/widget.html +++ b/layouts/partials/search/widget.html @@ -9,7 +9,7 @@ {{- $simple = false }} {{ end }}