Skip to content

Commit

Permalink
feat(search): allow complete section customization
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
james-d-elliott committed Jun 25, 2022
1 parent 75d250b commit cdca33b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
8 changes: 7 additions & 1 deletion assets/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.RegularPages "Type" "in" .Site.Params.options.searchSectionsIndex) }}
{{- end }}

{{ $len := (len $list) -}}

{{ range $index, $element := $list -}}
Expand Down
2 changes: 2 additions & 0 deletions config/_default/params.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ lastMod = false
clipBoard = true
instantPage = true
flexSearch = true
searchSectionsShow = []
searchSectionsIndex = []
darkMode = true
bootStrapJs = true
breadCrumb = false
Expand Down
8 changes: 5 additions & 3 deletions layouts/partials/footer/script-footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 -}}
Expand Down Expand Up @@ -77,7 +79,7 @@
{{ with .Params.mermaid -}}
<script src="{{ $mermaid.RelPermalink }}" defer></script>
{{ end -}}
{{ if .Site.Params.options.flexSearch -}}
{{ if $includeFlexSearch -}}
<script src="{{ $index.RelPermalink }}" defer></script>
{{ end -}}
{{ else -}}
Expand All @@ -102,7 +104,7 @@
{{ with .Params.mermaid -}}
<script src="{{ $mermaid.RelPermalink }}" integrity="{{ $mermaid.Data.Integrity }}" crossorigin="anonymous" defer></script>
{{ end -}}
{{ if .Site.Params.options.flexSearch -}}
{{ if $includeFlexSearch -}}
<script src="{{ $index.Permalink }}" integrity="{{ $index.Data.Integrity }}" crossorigin="anonymous" defer></script>
{{ end -}}
{{ end -}}
{{ end -}}
2 changes: 1 addition & 1 deletion layouts/partials/header/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ <h2 class="h5 offcanvas-title ps-2" id="offcanvasDoksLabel"><a class="text-dark"
{{ end -}}
</ul>

{{ 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)) -}}
<hr class="text-black-50 my-4 d-lg-none">
<form class="doks-search position-relative flex-grow-1 ms-lg-auto me-lg-2">
<input id="search" class="form-control is-search" type="search" placeholder="Search docs..." aria-label="Search docs..." autocomplete="off">
Expand Down

0 comments on commit cdca33b

Please sign in to comment.