diff options
author | Jimmy Cai <jimmehcai@gmail.com> | 2021-01-01 15:18:58 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-01 15:18:58 +0300 |
commit | ced387cbd5d478e8b269f0e99270c66449334b69 (patch) | |
tree | e79333b60f78b77a0b3b2027fc80956ca962d9e2 | |
parent | ae477ab224489c9a5ff56705da5f84296ca82bb1 (diff) | |
parent | efa3452cdbabe25984aea87472fb135ec0fd755e (diff) |
Merge pull request #109 from CaiJimmy/refactor-widget
feat(widget): detect automatically path to archives / search page
-rw-r--r-- | exampleSite/config.yaml | 3 | ||||
-rw-r--r-- | layouts/partials/widget/archives.html | 58 | ||||
-rw-r--r-- | layouts/partials/widget/search.html | 26 |
3 files changed, 49 insertions, 38 deletions
diff --git a/exampleSite/config.yaml b/exampleSite/config.yaml index a0266c8..fb89f51 100644 --- a/exampleSite/config.yaml +++ b/exampleSite/config.yaml @@ -60,7 +60,6 @@ params: archives: limit: 5 - path: archives tagCloud: limit: 10 @@ -82,7 +81,7 @@ params: colorScheme: # Display toggle toggle: true - + # Available values: auto, light, dark default: auto diff --git a/layouts/partials/widget/archives.html b/layouts/partials/widget/archives.html index 950dbf2..4a92a30 100644 --- a/layouts/partials/widget/archives.html +++ b/layouts/partials/widget/archives.html @@ -1,27 +1,33 @@ -<section class="widget archives"> - <div class="widget-icon"> - {{ partial "helper/icon" "infinity" }} - </div> - <h2 class="widget-title section-title">{{ T "widget.archives.title" }}</h2> +{{- $query := first 1 (where .Site.Pages "Layout" "==" "archives") -}} +{{- if $query -}} + {{- $archivesPage := index $query 0 -}} + <section class="widget archives"> + <div class="widget-icon"> + {{ partial "helper/icon" "infinity" }} + </div> + <h2 class="widget-title section-title">{{ T "widget.archives.title" }}</h2> - {{ $pages := where .Site.RegularPages "Type" "in" .Site.Params.mainSections }} - {{ $notHidden := where .Site.RegularPages "Params.hidden" "!=" true }} - {{ $filtered := ($pages | intersect $notHidden) }} - {{ $archives := $filtered.GroupByDate "2006" }} - - <div class="widget-archive--list"> - {{ range $index, $item := first (add .Site.Params.widgets.archives.limit 1) ($archives) }} - {{- $id := lower (replace $item.Key " " "-") -}} - <div class="archives-year"> - <a href="{{ $.Site.Params.widgets.archives.path | relLangURL }}#{{ $id }}"> - {{ if eq $index $.Site.Params.widgets.archives.limit }} - <span class="year">{{ T "widget.archives.more" }}</span> - {{ else }} - <span class="year">{{ .Key }}</span> - <span class="count">{{ len $item.Pages }}</span> - {{ end }} - </a> - </div> - {{ end }} - </div> -</section>
\ No newline at end of file + {{ $pages := where .Site.RegularPages "Type" "in" .Site.Params.mainSections }} + {{ $notHidden := where .Site.RegularPages "Params.hidden" "!=" true }} + {{ $filtered := ($pages | intersect $notHidden) }} + {{ $archives := $filtered.GroupByDate "2006" }} + + <div class="widget-archive--list"> + {{ range $index, $item := first (add .Site.Params.widgets.archives.limit 1) ($archives) }} + {{- $id := lower (replace $item.Key " " "-") -}} + <div class="archives-year"> + <a href="{{ $archivesPage.RelPermalink }}#{{ $id }}"> + {{ if eq $index $.Site.Params.widgets.archives.limit }} + <span class="year">{{ T "widget.archives.more" }}</span> + {{ else }} + <span class="year">{{ .Key }}</span> + <span class="count">{{ len $item.Pages }}</span> + {{ end }} + </a> + </div> + {{ end }} + </div> + </section> +{{- else -}} + {{- warnf "Archives page not found. Create a page with layout: archives." -}} +{{- end -}}
\ No newline at end of file diff --git a/layouts/partials/widget/search.html b/layouts/partials/widget/search.html index 2275eb9..833f740 100644 --- a/layouts/partials/widget/search.html +++ b/layouts/partials/widget/search.html @@ -1,10 +1,16 @@ -<form action="/search" class="search-form widget" {{ with .OutputFormats.Get "json" -}}data-json="{{ .Permalink }}" {{- end }}> - <p> - <label>{{ T "search.title" }}</label> - <input name="keyword" required placeholder="{{ T `search.placeholder` }}" /> - - <button title="Search"> - {{ partial "helper/icon" "search" }} - </button> - </p> -</form>
\ No newline at end of file +{{- $query := first 1 (where .Site.Pages "Layout" "==" "search") -}} +{{- if $query -}} + {{- $searchPage := index $query 0 -}} + <form action="{{ $searchPage.RelPermalink }}" class="search-form widget" {{ with .OutputFormats.Get "json" -}}data-json="{{ .Permalink }}" {{- end }}> + <p> + <label>{{ T "search.title" }}</label> + <input name="keyword" required placeholder="{{ T `search.placeholder` }}" /> + + <button title="{{ T `search.title` }}"> + {{ partial "helper/icon" "search" }} + </button> + </p> + </form> +{{- else -}} + {{- warnf "Search page not found. Create a page with layout: search." -}} +{{- end -}}
\ No newline at end of file |