Skip to content

Commit

Permalink
feat: support sort blog post list page (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
imfing authored Apr 2, 2024
1 parent 5103da4 commit 6d4bbac
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
5 changes: 4 additions & 1 deletion exampleSite/hugo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ params:
flexsearch:
# index page by: content | summary | heading | title
index: content
# full | forward | reverse | strict
# full | forward | reverse | strict
# https://github.com/nextapps-de/flexsearch/#tokenizer-prefix-search
tokenize: forward

Expand All @@ -147,6 +147,9 @@ params:
blog:
list:
displayTags: true
# date | lastmod | publishDate | title | weight
sortBy: date
sortOrder: desc # or "asc"

comments:
enable: false
Expand Down
7 changes: 4 additions & 3 deletions layouts/blog/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
<br class="hx-mt-1.5 hx-text-sm" />
<h1 class="hx-text-center hx-mt-2 hx-text-4xl hx-font-bold hx-tracking-tight hx-text-slate-900 dark:hx-text-slate-100">{{ .Title }}</h1>
<div class="content">{{ .Content }}</div>
{{ range .Pages.ByDate.Reverse }}
{{- $pages := partial "utils/sort-pages" (dict "page" . "by" site.Params.blog.list.sortBy "order" site.Params.blog.list.sortOrder) -}}
{{- range $pages }}
<div class="hx-mb-10">
<h3><a style="color: inherit; text-decoration: none;" class="hx-block hx-font-semibold hx-mt-8 hx-text-2xl " href="{{ .RelPermalink }}">{{ .Title }}</a></h3>
{{- if site.Params.blog.list.displayTags -}}
Expand All @@ -25,9 +26,9 @@ <h3><a style="color: inherit; text-decoration: none;" class="hx-block hx-font-se
</p>
<p class="hx-opacity-50 hx-text-sm hx-mt-4 hx-leading-7">{{ partial "utils/format-date" .Date }}</p>
</div>
{{ end }}
{{ end -}}
</main>
</article>
<div class="max-xl:hx-hidden hx-h-0 hx-w-64 hx-shrink-0"></div>
</div>
{{ end }}
{{- end -}}
32 changes: 32 additions & 0 deletions layouts/partials/utils/sort-pages.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{{- $page := .page -}}
{{- $by := .by | default "weight" -}}
{{- $order := .order | default "asc" -}}

{{- $pages := slice }}

{{- if eq $by "weight" }}
{{- $pages = $page.Pages.ByWeight }}
{{- else if eq $by "date" }}
{{- $pages = $page.Pages.ByDate }}
{{- else if eq $by "title" }}
{{- $pages = $page.Pages.ByTitle }}
{{- else if eq $by "expiryDate" }}
{{- $pages = $page.Pages.ByExpiryDate }}
{{- else if eq $by "publishDate" }}
{{- $pages = $page.Pages.ByPublishDate }}
{{- else if eq $by "lastmod" }}
{{- $pages = $page.Pages.ByLastmod }}
{{- else if eq $by "linkTitle" }}
{{- $pages = $page.Pages.ByLinkTitle }}
{{- else if eq $by "length" }}
{{- $pages = $page.Pages.ByLength }}
{{- else }}
{{- warnf "sort-pages: unknown sort field %q" $by -}}
{{- $pages = $page.Pages }}
{{ end -}}

{{- if eq $order "desc" }}
{{- $pages = $pages.Reverse }}
{{- end -}}

{{- return $pages -}}

0 comments on commit 6d4bbac

Please sign in to comment.