From 2f2ffc00257e914933af3b0fd04de89d6da7f7d8 Mon Sep 17 00:00:00 2001
From: weru <fromweru@gmail.com>
Date: Mon, 13 Jun 2022 15:31:19 +0000
Subject: [PATCH] update search function

---
 layouts/_default/index.json          |    2 +-
 layouts/partials/scripts/bundle.html |    2 +-
 layouts/partials/search.html         |    7 ++++++-
 assets/js/search.js                  |   40 +++++++++++++++++++++++++++++-----------
 assets/js/variables.js               |    2 ++
 layouts/search/single.html           |    2 +-
 assets/js/functions.js               |    2 +-
 7 files changed, 41 insertions(+), 16 deletions(-)

diff --git a/assets/js/functions.js b/assets/js/functions.js
index e57e804..43acbb9 100644
--- a/assets/js/functions.js
+++ b/assets/js/functions.js
@@ -161,7 +161,7 @@
   }
 }
 
-function parseBoolean(string) {
+function parseBoolean(string = "") {
   string = string.trim().toLowerCase();
   switch (string) {
     case 'true':
diff --git a/assets/js/search.js b/assets/js/search.js
index 89990b6..9dde9b0 100644
--- a/assets/js/search.js
+++ b/assets/js/search.js
@@ -1,5 +1,5 @@
 function initializeSearch(index) {
-  const searchKeys = ['title', 'link', 'body', 'id'];
+  const searchKeys = ['title', 'link', 'body', 'id', 'section', 'tags'];
 
   const searchPageElement = elem('#searchpage');
 
@@ -36,15 +36,21 @@
       let resultsTitle = createEl('h3');
       resultsTitle.className = 'search_title';
       resultsTitle.innerText = quickLinks;
+
+      let goBackButton = createEl('button');
+      goBackButton.textContent = 'Go Back';
+      goBackButton.className = goBackClass;
       if(passive) {
         resultsTitle.innerText = searchResultsLabel;
       }
-      resultsFragment.appendChild(resultsTitle);
       if(!searchPageElement) {
         results = results.slice(0,8);
       } else {
+        resultsFragment.appendChild(goBackButton);
         results = results.slice(0,12);
       }
+      resultsFragment.appendChild(resultsTitle);
+
       results.forEach(function(result){
         let item = createEl('a');
         item.href = `${result.link}?query=${query}`;
@@ -59,7 +65,7 @@
           let itemDescription = createEl('p');
           // position of first search term instance
           let queryInstance = result.body.indexOf(query);
-          itemDescription.textContent = `... ${result.body.substring(queryInstance, queryInstance + 200)} ...`;
+          itemDescription.textContent = `${result.body.substring(queryInstance, queryInstance + 200)}`;
           item.appendChild(itemDescription);
         } else {
           item.textContent = result.title;
@@ -79,15 +85,21 @@
     showResults.appendChild(resultsFragment);
   }
 
-  function search(searchTerm, 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;
-      });
+        return resultItem ;
+      })
+
+      if(scope) {
+        rawResults = rawResults.filter(resultItem => {
+          return resultItem.section == scope;
+        });
+      }
 
       passive ? searchResults(rawResults, searchTerm, true) : searchResults(rawResults, searchTerm);
 
@@ -100,16 +112,19 @@
     const searchField = elem(searchFieldClass);
 
     if (searchField) {
+      const searchScope = searchField.dataset.scope;
       searchField.addEventListener('input', function() {
         const searchTerm = searchField.value.trim().toLowerCase();
-        search(searchTerm);
+        search(searchTerm, searchScope);
+        // console.log(searchTerm);
       });
 
       if(!searchPageElement) {
         searchField.addEventListener('search', function(){
           const searchTerm = searchField.value.trim().toLowerCase();
           if(searchTerm.length)  {
-            window.location.href = new URL(`search/?query=${searchTerm}`, rootURL).href;
+            const scopeParameter = searchScope ? `&scope=${searchScope}` : '';
+            window.location.href = new URL(`search/?query=${searchTerm}${ scopeParameter }`, rootURL).href;
           }
         });
       }
@@ -128,11 +143,12 @@
   function passiveSearch() {
     if(searchPageElement) {
       const searchTerm = findQuery();
-      search(searchTerm, true);
-
+      const searchScope = findQuery('scope');
       // search actively after search page has loaded
       const searchField = elem(searchFieldClass);
 
+      search(searchTerm, searchScope, true);
+
       if(searchField) {
         searchField.addEventListener('input', function() {
           const searchTerm = searchField.value.trim().toLowerCase();
@@ -193,7 +209,9 @@
 }
 
 window.addEventListener('load', function() {
-  fetch(new URL("index.json", rootURL).href)
+  const pageLanguage = document.documentElement.lang;
+  const searchIndex = `${ pageLanguage === 'en' ? '': pageLanguage}/index.json`;
+  fetch(new URL(searchIndex, rootURL).href)
   .then(response => response.json())
   .then(function(data) {
     data = data.length ? data : [];
diff --git a/assets/js/variables.js b/assets/js/variables.js
index 6deb393..b7f50b7 100644
--- a/assets/js/variables.js
+++ b/assets/js/variables.js
@@ -7,6 +7,8 @@
 const rootURL = window.location.protocol + "//" + window.location.host;
 const searchFieldClass = '.search_field';
 const searchClass = '.search';
+const goBackClass = 'button_back';
+const lineClass = '.line';
 
 // config defined values
 const codeBlockConfig = JSON.parse('{{ partial "functions/getCodeConfig" . }}');
diff --git a/layouts/_default/index.json b/layouts/_default/index.json
index 5642b61..34e9dc3 100644
--- a/layouts/_default/index.json
+++ b/layouts/_default/index.json
@@ -1,7 +1,7 @@
 {{- $.Scratch.Add "index" slice -}}
 {{- range .Site.Pages -}}
   {{- if ne .Type "search" -}}
-    {{- $.Scratch.Add "index" (dict "title" .Title "body" .Plain "link" .Permalink) -}}
+    {{- $.Scratch.Add "index" (dict "title" .Title "body" .Plain "link" .Permalink "section" .Section) -}}
   {{- end -}}
 {{- end -}}
 {{- jsonify (uniq ($.Scratch.Get "index")) -}}
\ No newline at end of file
diff --git a/layouts/partials/scripts/bundle.html b/layouts/partials/scripts/bundle.html
index 332eeab..5e715b2 100644
--- a/layouts/partials/scripts/bundle.html
+++ b/layouts/partials/scripts/bundle.html
@@ -22,7 +22,7 @@
 {{ end }}
 {{- $custom := resources.Get $customScriptPath | resources.ExecuteAsTemplate $customScriptPath . }}
 
-{{- $bundle := slice $variables $functions $code $main $fuse $search $custom | resources.Concat "js/bundle.js" | resources.Minify | resources.Fingerprint "sha512" }}
+{{- $bundle := slice $variables $functions $code $main $fuse $search $custom | resources.Concat "js/bundle.js" | resources.Fingerprint "sha512" }}
 <script src="{{ $bundle.Permalink }}"></script>
 
 {{- partialCached "hooks/scripts" . -}}
diff --git a/layouts/partials/search.html b/layouts/partials/search.html
index bcc56d2..0890675 100644
--- a/layouts/partials/search.html
+++ b/layouts/partials/search.html
@@ -3,7 +3,12 @@
   <label for="find" class="search_label">
     {{- partial "sprite" (dict "icon" "search") }}
   </label>
-  <input type="search" class="search_field" placeholder='{{ T "search_field_placeholder" }}' id="find" autocomplete="off">
+  {{ $placeholder := T "search_field_placeholder" (dict "section" .Section) }}
+  {{ if .Section }}
+  {{ else }}
+    {{ $placeholder =  printf "%s%s" $placeholder (T "site") }}
+  {{ end }}
+  <input type="search" class="search_field" placeholder='{{ $placeholder }}' id="find" autocomplete="off"  data-scope='{{ .Section }}'>
   {{- if ne .Params.searchPage true }}
   <div class="search_results results"></div>
   {{- end }}
diff --git a/layouts/search/single.html b/layouts/search/single.html
index 456a268..fcd2298 100644
--- a/layouts/search/single.html
+++ b/layouts/search/single.html
@@ -1,5 +1,5 @@
 {{- define "main" }}
 <main id="results">
-  <div class="results" id="searchpage"></div>
+  <div class="results wrap" id="searchpage"></div>
 </main>
 {{- end }}
\ No newline at end of file

--
Gitblit v1.10.0