Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion exampleSite/content/en/shortcodes/toc-tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@ The `toc-tree` shortcode will generate a Table of Content from a section file tr

<!-- prettier-ignore-start -->
```tpl
{{</* toc-tree */>}}
{{</* toc-tree [sortBy="title"] */>}}
```
<!-- prettier-ignore-end -->

### Attributes

<!-- prettier-ignore-start -->
<!-- spellchecker-disable -->
{{< propertylist name=shortcode-toc-tree sort=name order=asc >}}
<!-- spellchecker-enable -->
<!-- prettier-ignore-end -->

## 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.
8 changes: 8 additions & 0 deletions exampleSite/data/properties/shortcode-toc-tree.yaml
Original file line number Diff line number Diff line change
@@ -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
36 changes: 33 additions & 3 deletions layouts/shortcodes/toc-tree.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,47 @@
{{- $current := . }}
{{- $tocLevels := default (default 6 .Site.Params.geekdocToC) .Page.Params.geekdocToC }}
{{- $sortBy := (default (default "title" .Site.Params.geekdocFileTreeSortBy) (.Get "sortBy") | lower) }}

{{- if $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>
{{- end }}


<!-- templates -->
{{- define "toc-tree" }}
<ul>
{{- range .sect.GroupBy "Weight" }}
{{- range .ByTitle }}
{{ range .sect.GroupBy "Weight" }}
{{ $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)) }}
<li>
{{- if or .Content .Params.geekdocFlatSection }}
Expand Down