From bba9312386885a39ef8b1131c21cdd76d6574bf1 Mon Sep 17 00:00:00 2001
From: weru <fromweru@gmail.com>
Date: Thu, 29 Dec 2022 20:35:18 +0000
Subject: [PATCH] allow custom searchable fields
---
layouts/_default/index.json | 19 +++++++++++++++++--
assets/js/search.js | 7 +++++--
assets/js/variables.js | 13 +++++++++++++
exampleSite/config/_default/params.toml | 2 ++
4 files changed, 37 insertions(+), 4 deletions(-)
diff --git a/assets/js/search.js b/assets/js/search.js
index 9dde9b0..614efe9 100644
--- a/assets/js/search.js
+++ b/assets/js/search.js
@@ -1,5 +1,8 @@
function initializeSearch(index) {
- const searchKeys = ['title', 'link', 'body', 'id', 'section', 'tags'];
+ let searchKeys = ['title', 'id', 'link', 'body', 'section'];
+ searchKeys = searchKeys.concat(otherSearchableFields);
+
+ console.log(searchKeys);
const searchPageElement = elem('#searchpage');
@@ -9,7 +12,7 @@
includeScore: true,
shouldSort: true,
keys: searchKeys,
- threshold: 0.0
+ threshold: 0.5
};
index = new Fuse(index, searchOptions);
diff --git a/assets/js/variables.js b/assets/js/variables.js
index b7f50b7..3a9636c 100644
--- a/assets/js/variables.js
+++ b/assets/js/variables.js
@@ -14,6 +14,19 @@
const codeBlockConfig = JSON.parse('{{ partial "functions/getCodeConfig" . }}');
const iconsPath = `{{ partialCached "functions/getIconPath" . }}`;
+// values defined under config/_default/params.toml
+let otherSearchableFields = '{{ delimit (default slice site.Params.otherSearchableFields) ", " }}'
+
+if(otherSearchableFields.length > 2) {
+ otherSearchableFields = otherSearchableFields
+ .split(",")
+ .map(search_value => search_value.toLowerCase().trim());
+} else {
+ otherSearchableFields = [];
+}
+
+console.log(otherSearchableFields);
+
// defined in i18n / translation files
const quickLinks = '{{ T "quick_links" }}';
const searchResultsLabel = '{{ T "search_results_label" }}';
diff --git a/exampleSite/config/_default/params.toml b/exampleSite/config/_default/params.toml
index 2059339..44ca38a 100644
--- a/exampleSite/config/_default/params.toml
+++ b/exampleSite/config/_default/params.toml
@@ -20,6 +20,8 @@
# By default the template will look for icons under the icons directory. In some situations you might wanna change that. edit the line below
# iconsPath = 'icons/'
+otherSearchableFields = ["Tags", "Categories", "CustomField"] # As they appear in frontmatter
+
# Site logo
[logo]
lightMode = "images/compose.svg"
diff --git a/layouts/_default/index.json b/layouts/_default/index.json
index 34e9dc3..7f15558 100644
--- a/layouts/_default/index.json
+++ b/layouts/_default/index.json
@@ -1,7 +1,22 @@
{{- $.Scratch.Add "index" slice -}}
-{{- range .Site.Pages -}}
+{{- $searchableExtras := site.Params.otherSearchableFields }}
+{{- range site.Pages -}}
+ {{ $params := .Params }}
{{- if ne .Type "search" -}}
- {{- $.Scratch.Add "index" (dict "title" .Title "body" .Plain "link" .Permalink "section" .Section) -}}
+ {{- $searchEntry := dict "title" .Title "body" .Plain "link" .Permalink "section" .Section }}
+ {{- range $index, $value := $searchableExtras }}
+ {{- $extraFieldValue := index $params $value }}
+ {{- $extraFieldValues := dict }}
+ {{- with $extraFieldValue }}
+ {{- $v := . }}
+ {{- if reflect.IsSlice . }}
+ {{- $v = delimit . "," }}
+ {{- end }}
+ {{- $extraFieldValues = dict (lower $value) $v }}
+ {{- end }}
+ {{- $searchEntry = merge $searchEntry $extraFieldValues }}
+ {{- end }}
+ {{- $.Scratch.Add "index" $searchEntry -}}
{{- end -}}
{{- end -}}
{{- jsonify (uniq ($.Scratch.Get "index")) -}}
\ No newline at end of file
--
Gitblit v1.10.0