From bf9588061a3f948f6a0acb3abd953d50e9bfbf6f Mon Sep 17 00:00:00 2001
From: weru <fromweru@gmail.com>
Date: Tue, 28 Feb 2023 15:10:44 +0000
Subject: [PATCH] allow site owners to enable global-search #104
---
assets/js/search/index.js | 4 +++-
assets/js/search/variables.js | 2 ++
exampleSite/content/docs/compose/search.md | 13 ++++++++++++-
exampleSite/config/_default/params.toml | 3 ++-
4 files changed, 19 insertions(+), 3 deletions(-)
diff --git a/assets/js/search/index.js b/assets/js/search/index.js
index 11d4d69..466a1f2 100644
--- a/assets/js/search/index.js
+++ b/assets/js/search/index.js
@@ -11,6 +11,7 @@
}
function search(index, scope = null, passive = false) {
+ scope = search_scope_global ? null : scope;
if(search_term.length) {
let raw_results = index;
if(!algolia_config.on) {
@@ -38,8 +39,9 @@
function liveSearch(index) {
if (search_field) {
- const search_scope = search_field.dataset.scope;
+ let search_scope = search_field.dataset.scope;
search(index, search_scope);
+ search_scope = search_scope_global ? null : search_scope;
if(!search_page_element) {
search_field.addEventListener('search', function(){
search_term = search_field.value.trim().toLowerCase();
diff --git a/assets/js/search/variables.js b/assets/js/search/variables.js
index 758829c..6328c78 100644
--- a/assets/js/search/variables.js
+++ b/assets/js/search/variables.js
@@ -24,6 +24,8 @@
}
const search_page_element = elem('#searchpage');
+let search_scope_global = `{{ trim site.Params.search.global " " }}`;
+search_scope_global = search_scope_global == 'true' ? true : false;
// Fuse specific
let search_keys = ['body', 'title', 'link', 'section', 'id',];
diff --git a/exampleSite/config/_default/params.toml b/exampleSite/config/_default/params.toml
index b1200e2..5e2e612 100644
--- a/exampleSite/config/_default/params.toml
+++ b/exampleSite/config/_default/params.toml
@@ -26,13 +26,14 @@
# search
[search]
on = true
-# Site logo
+global = false # turn to `true` to enable global search
[search.algolia]
enable = false # if false search will default to fusejs
id = "Q40WQQX84U" # Application ID
index = "compose" # Index name
key = "da87401a458102ec6bbd6cc5e5cf8d95" # Search-Only API Key
+# Site logo
[logo]
lightMode = "images/compose.svg"
darkMode = "images/compose-light.svg"
diff --git a/exampleSite/content/docs/compose/search.md b/exampleSite/content/docs/compose/search.md
index 6de7c83..0d038a5 100644
--- a/exampleSite/content/docs/compose/search.md
+++ b/exampleSite/content/docs/compose/search.md
@@ -17,7 +17,7 @@
# search
[search]
on = true
-# Site logo
+global = false
[search.algolia]
enable = false # if false search will default to fusejs
id = "Q40WQQX84U" # Application ID
@@ -29,6 +29,17 @@
Algolia will require you to build and host your index. For those using Github, this theme ships with an [algolia github action](/docs/compose/github-actions/#algolia-ci).
+By default, search will return results from the current content section. Searches from the top level section e.g the homepage, will return global results. This way, the results are scoped. You can override this behaviour using this setting
+
+```toml
+...
+[search]
+...
+global = false # turn to `true` to enable global search
+...
+```
+
+
At the time of this writing, search on these theme takes either of this forms:
### 1. Passive search
--
Gitblit v1.10.0