diff options
author | Chip Zoller <chipzoller@gmail.com> | 2022-05-19 15:02:32 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-19 15:02:32 +0300 |
commit | e511e14db7a11b726ccc6c4456bf5ec5ebdd3720 (patch) | |
tree | 47de67587d6d81976efe71142a6d30027b27880b | |
parent | ba091e275759d8c7289b7757c8146de25825239f (diff) | |
parent | bd7752d3d7f0bb7c34ab518cc4984c0af7c44ddc (diff) |
Merge pull request #301 from chipzoller/issue-290-allow-granular-search-by-sections
Issue 290 allow granular search by sections
-rw-r--r-- | README.md | 9 | ||||
-rw-r--r-- | assets/js/search.js | 19 | ||||
-rw-r--r-- | layouts/partials/search/widget.html | 2 | ||||
-rw-r--r-- | layouts/partials/sidebar.html | 2 |
4 files changed, 23 insertions, 9 deletions
@@ -886,3 +886,12 @@ enableSearch = true Live search works even for multilingual sites. For Chinese-like languages, it may or may not work. + +__Search Scope__ + +- Searching within a section will yield results from that section. + + For example, if you have 3 sections in your content i.e `blog`, `docs` & `examples`, searching in the `docs` section will only produce results for that section. +- Searching outside a section will search the entire site. + + For example, with the above setup, searching from the homepage will produce results from the entire site.
\ No newline at end of file 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() { diff --git a/layouts/partials/search/widget.html b/layouts/partials/search/widget.html index afcc0d2..d5dbdef 100644 --- a/layouts/partials/search/widget.html +++ b/layouts/partials/search/widget.html @@ -9,7 +9,7 @@ {{- $simple = false }} {{ end }} <div class="search"> - <input type="search" class="search_field form_field" placeholder='{{ $placeholder }}' id="find" autocomplete="off" data-scope='{{ delimit $params.mainSections "" }}'> + <input type="search" class="search_field form_field" placeholder='{{ $placeholder }}' id="find" autocomplete="off" data-scope='{{ .Section }}'> <label for="find" class="search_label"> {{- partial "sprite" (dict "icon" "search") }} </label> diff --git a/layouts/partials/sidebar.html b/layouts/partials/sidebar.html index e849852..30fd676 100644 --- a/layouts/partials/sidebar.html +++ b/layouts/partials/sidebar.html @@ -2,7 +2,7 @@ <aside class="sidebar"> <section class="sidebar_inner"> <br> - {{ partialCached "search/widget" . }} + {{ partial "search/widget" . }} {{- $introDescription := $s.introDescription }} {{- with .Params.introDescription }} {{- $introDescription = . }} |