Skip to content

Commit

Permalink
Merge pull request #301 from chipzoller/issue-290-allow-granular-sear…
Browse files Browse the repository at this point in the history
…ch-by-sections

Issue 290 allow granular search by sections
  • Loading branch information
chipzoller authored May 19, 2022
2 parents ba091e2 + bd7752d commit e511e14
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
19 changes: 12 additions & 7 deletions assets/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand Down Expand Up @@ -85,17 +85,21 @@ 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){
const score = result.score;
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);

Expand All @@ -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;
}
});
}
Expand All @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion layouts/partials/search/widget.html
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion layouts/partials/sidebar.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 = . }}
Expand Down

0 comments on commit e511e14

Please sign in to comment.