mirror of https://github.com/onweru/compose.git

weru
29.35.2022 bba9312386885a39ef8b1131c21cdd76d6574bf1
allow custom searchable fields

Signed-off-by: weru <fromweru@gmail.com>
4 files modified
41 ■■■■■ changed files
assets/js/search.js 7 ●●●● patch | view | raw | blame | history
assets/js/variables.js 13 ●●●●● patch | view | raw | blame | history
exampleSite/config/_default/params.toml 2 ●●●●● patch | view | raw | blame | history
layouts/_default/index.json 19 ●●●● patch | view | raw | blame | history
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);
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" }}';
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"
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")) -}}