Skip to content

Commit a8713db

Browse files
committed
Hugo: add searchTerm option to search shortcode
* Add searchTerm to example page * Pass searchTerm from search shortcode, through search partial, to filter partial * Set initial searchItem with search filter To test: * Navigate to /examples/shortcodes/search and verify that the search results are accurate. Fixes cue-lang/cue#3566 Signed-off-by: juan Stelling <juan.stelling@usmedia.nl>
1 parent 1b95080 commit a8713db

File tree

6 files changed

+21
-4
lines changed

6 files changed

+21
-4
lines changed

content/examples/shortcodes/search/en.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ With this shortcode you can embed a search results widget on the page.
88
## Example
99

1010
```
11-
{{</* search contentType="How-to Guides" showContentTypes=false tags="encodings,commented cue" */>}}
11+
{{</* search contentType="How-to Guides" showContentTypes=false tags="encodings,commented cue" searchTerm="json" */>}}
1212
```
1313

14-
{{< search contentType="How-to Guides" showContentTypes=false tags="encodings,commented cue" >}}
14+
{{< search contentType="How-to Guides" showContentTypes=false tags="encodings,commented cue" searchTerm="json" >}}
1515

1616

1717
## Attributes
@@ -24,3 +24,6 @@ showContentTypes
2424

2525
tags
2626
: optional - Adds preselected tags
27+
28+
searchTerm
29+
: optional - Prefills the search bar

hugo/assets/ts/widgets/search-filter.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,11 @@ export class SearchFilter extends BaseWidget {
173173
this.selectedItems = selectedItems;
174174

175175
if (urlParamsChanged) {
176+
const initialSearchTerm = this.element.dataset.initialSearchTerm;
177+
if (initialSearchTerm) {
178+
this.parsedQuery.cleanQuery = initialSearchTerm;
179+
}
180+
176181
this.updateUrl(false);
177182
}
178183
}

hugo/content/en/examples/shortcodes/search/index.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ With this shortcode you can embed a search results widget on the page.
88
## Example
99

1010
```
11-
{{</* search contentType="How-to Guides" showContentTypes=false tags="encodings,commented cue" */>}}
11+
{{</* search contentType="How-to Guides" showContentTypes=false tags="encodings,commented cue" searchTerm="json" */>}}
1212
```
1313

14-
{{< search contentType="How-to Guides" showContentTypes=false tags="encodings,commented cue" >}}
14+
{{< search contentType="How-to Guides" showContentTypes=false tags="encodings,commented cue" searchTerm="json" >}}
1515

1616

1717
## Attributes
@@ -24,3 +24,6 @@ showContentTypes
2424

2525
tags
2626
: optional - Adds preselected tags
27+
28+
searchTerm
29+
: optional - Prefills the search bar

hugo/layouts/partials/filter.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
{{ $inputLabel := printf "search_%s_input" $translationKey }}
99
{{ $noResults := printf "search_%s_no-results" $translationKey }}
1010
{{ $preselectedItems := .preselectedItems }}
11+
{{ $searchTerm := .searchTerm }}
1112

1213
<details class="filter{{ if $cssClass }} {{ $cssClass }}{{ end }} is-loading"
1314
data-dropdown data-filter="{{ $name }}"
1415
data-filter-items="{{ $filters | jsonify }}"
1516
{{ if $preselectedItems }} data-preselected-items="{{ $preselectedItems }}"{{ end }}
17+
{{ if $searchTerm }} data-initial-search-term="{{ $searchTerm }}"{{ end }}
1618
{{ if $open }} open{{ end }}
1719
>
1820
<summary class="filter__title button button--small button--white">

hugo/layouts/partials/search.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{{ $contentTypes := slice }}
44
{{ $contentType := .contentType | default "" }}
55
{{ $preselectedTags := .preselectedTags | default "" }}
6+
{{ $searchTerm := .searchTerm | default "" }}
67
{{ $type := .type | default "full" }}
78
{{ $placeholder := (T "search_placeholder" ) }}
89

@@ -48,6 +49,7 @@
4849
"name" "tags"
4950
"filters" .Site.Params.tags
5051
"preselectedItems" $preselectedTags
52+
"searchTerm" $searchTerm
5153
) -}}
5254

5355
{{ if $showContentTypes }}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
{{- $contentType := .Get "contentType" | default "" -}}
22
{{- $showContentTypes := .Get "showContentTypes" | default true -}}
33
{{- $preselectedTags := .Get "tags" | default "" -}}
4+
{{- $searchTerm := .Get "searchTerm" | default "" -}}
45

56
{{- partial "search" (dict
67
"type" "widget"
78
"contentType" $contentType
89
"showContentTypes" $showContentTypes
910
"preselectedTags" $preselectedTags
11+
"searchTerm" $searchTerm
1012
"context" .
1113
) -}}

0 commit comments

Comments
 (0)