From 00e4a7886e830b26838b5a33793d5e75086e4ce8 Mon Sep 17 00:00:00 2001 From: James Elliott Date: Sat, 25 Jun 2022 11:07:03 +1000 Subject: [PATCH] feat(search): allow complete section customization This allows configuring the searchable sections and which sections show the search box. In addition the javascript is only included on pages where the search box is visible. --- assets/js/index.js | 8 +++++++- config/_default/params.toml | 2 ++ layouts/partials/footer/script-footer.html | 8 +++++--- layouts/partials/header/header.html | 2 +- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/assets/js/index.js b/assets/js/index.js index f3d8da1..dbaaa28 100644 --- a/assets/js/index.js +++ b/assets/js/index.js @@ -94,7 +94,13 @@ Source: // https://discourse.gohugo.io/t/range-length-or-last-element/3803/2 - {{ $list := (where .Site.Pages "Section" "docs") -}} + {{ $list := slice }} + {{- if eq (len .Site.Params.options.searchSectionsIndex) 0 }} + {{- $list = (where .Site.Pages "Section" "docs") }} + {{- else }} + {{- $list = (where .Site.Pages "Type" "in" .Site.Params.options.searchSectionsIndex) }} + {{- end }} + {{ $len := (len $list) -}} {{ range $index, $element := $list -}} diff --git a/config/_default/params.toml b/config/_default/params.toml index 5780d96..df9903b 100644 --- a/config/_default/params.toml +++ b/config/_default/params.toml @@ -83,6 +83,8 @@ lastMod = false clipBoard = true instantPage = true flexSearch = true + searchSectionsShow = [] + searchSectionsIndex = [] darkMode = true bootStrapJs = true breadCrumb = false diff --git a/layouts/partials/footer/script-footer.html b/layouts/partials/footer/script-footer.html index f57261a..8cfe03e 100644 --- a/layouts/partials/footer/script-footer.html +++ b/layouts/partials/footer/script-footer.html @@ -39,6 +39,8 @@ {{ $slice = $slice | append $flexSearch -}} {{ end -}} +{{ $includeFlexSearch := and .Site.Params.options.flexSearch (or (eq (len .Site.Params.options.searchSectionsShow) 0) (in .Site.Params.options.searchSectionsShow .Section)) }} + {{ if .Site.Params.options.darkMode -}} {{ $darkMode := resources.Get "js/darkmode.js" -}} {{ $darkMode := $darkMode | js.Build -}} @@ -77,7 +79,7 @@ {{ with .Params.mermaid -}} {{ end -}} - {{ if .Site.Params.options.flexSearch -}} + {{ if $includeFlexSearch -}} {{ end -}} {{ else -}} @@ -102,7 +104,7 @@ {{ with .Params.mermaid -}} {{ end -}} - {{ if .Site.Params.options.flexSearch -}} + {{ if $includeFlexSearch -}} {{ end -}} -{{ end -}} \ No newline at end of file +{{ end -}} diff --git a/layouts/partials/header/header.html b/layouts/partials/header/header.html index 65744ec..5dfae5b 100644 --- a/layouts/partials/header/header.html +++ b/layouts/partials/header/header.html @@ -77,7 +77,7 @@ {{ end -}} - {{ if .Site.Params.options.flexSearch -}} + {{ if and .Site.Params.options.flexSearch (or (eq (len .Site.Params.options.searchSectionsShow) 0) (in .Site.Params.options.searchSectionsShow .Section)) -}}