fix: respect geekdocFileTreeSortBy parameter while creating the toc tree (#771)

This commit is contained in:
Robert Kaussow 2024-01-12 11:56:06 +01:00 committed by GitHub
parent 1d789048f3
commit b750f4ab53
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 4 deletions

View file

@ -8,10 +8,18 @@ The `toc-tree` shortcode will generate a Table of Content from a section file tr
<!-- prettier-ignore-start --> <!-- prettier-ignore-start -->
```tpl ```tpl
{{</* toc-tree */>}} {{</* toc-tree [sortBy="title"] */>}}
``` ```
<!-- prettier-ignore-end --> <!-- prettier-ignore-end -->
### Attributes
<!-- prettier-ignore-start -->
<!-- spellchecker-disable -->
{{< propertylist name=shortcode-toc-tree sort=name order=asc >}}
<!-- spellchecker-enable -->
<!-- prettier-ignore-end -->
## Example ## Example
As said, the root will be the site on which the shortcode was used, you can see a demo including nesting in the [ToC Tree](/toc-tree/) section. As said, the root will be the site on which the shortcode was used, you can see a demo including nesting in the [ToC Tree](/toc-tree/) section.

View file

@ -0,0 +1,8 @@
---
properties:
- name: sortBy
type: string
description: |
Override the default sort parameter set by [`geekdocFileTreeSortBy`](/usage/configuration/#site-configuration).
required: false
defaultValue: .Site.Params.geekdocFileTreeSortBy

View file

@ -1,8 +1,10 @@
{{- $current := . }}
{{- $tocLevels := default (default 6 .Site.Params.geekdocToC) .Page.Params.geekdocToC }} {{- $tocLevels := default (default 6 .Site.Params.geekdocToC) .Page.Params.geekdocToC }}
{{- $sortBy := (default (default "title" .Site.Params.geekdocFileTreeSortBy) (.Get "sortBy") | lower) }}
{{- if $tocLevels }} {{- if $tocLevels }}
<div class="gdoc-toc gdoc-toc__level--{{ $tocLevels }}"> <div class="gdoc-toc gdoc-toc__level--{{ $tocLevels }}">
{{ template "toc-tree" dict "sect" .Page.Pages }} {{ template "toc-tree" dict "sect" .Page.Pages "current" $current "sortBy" $sortBy }}
</div> </div>
{{- end }} {{- end }}
@ -10,8 +12,36 @@
<!-- templates --> <!-- templates -->
{{- define "toc-tree" }} {{- define "toc-tree" }}
<ul> <ul>
{{- range .sect.GroupBy "Weight" }} {{ range .sect.GroupBy "Weight" }}
{{- range .ByTitle }} {{ $rangeBy := .ByTitle }}
{{ if eq $.sortBy "title" }}
{{ $rangeBy = .ByTitle }}
{{ else if eq $.sortBy "linktitle" }}
{{ $rangeBy = .ByLinkTitle }}
{{ else if eq $.sortBy "date" }}
{{ $rangeBy = .ByDate }}
{{ else if eq $.sortBy "publishdate" }}
{{ $rangeBy = .ByPublishDate }}
{{ else if eq $.sortBy "expirydate" }}
{{ $rangeBy = .ByExpiryDate }}
{{ else if eq $.sortBy "lastmod" }}
{{ $rangeBy = .ByLastmod }}
{{ else if eq $.sortBy "title_reverse" }}
{{ $rangeBy = .ByTitle.Reverse }}
{{ else if eq $.sortBy "linktitle_reverse" }}
{{ $rangeBy = .ByLinkTitle.Reverse }}
{{ else if eq $.sortBy "date_reverse" }}
{{ $rangeBy = .ByDate.Reverse }}
{{ else if eq $.sortBy "publishdate_reverse" }}
{{ $rangeBy = .ByPublishDate.Reverse }}
{{ else if eq $.sortBy "expirydate_reverse" }}
{{ $rangeBy = .ByExpiryDate.Reverse }}
{{ else if eq $.sortBy "lastmod_reverse" }}
{{ $rangeBy = .ByLastmod.Reverse }}
{{ end }}
{{ range $rangeBy }}
{{- if or (not .Params.geekdocHidden) (not (default true .Params.geekdocHiddenTocTree)) }} {{- if or (not .Params.geekdocHidden) (not (default true .Params.geekdocHiddenTocTree)) }}
<li> <li>
{{- if or .Content .Params.geekdocFlatSection }} {{- if or .Content .Params.geekdocFlatSection }}