menu: frontmatter option to change sort predicate #98

This commit is contained in:
Sören Weber 2021-09-27 22:03:10 +02:00
parent f35e2336aa
commit 16b346c646
No known key found for this signature in database
GPG key ID: 07D17FF580AE7589
6 changed files with 29 additions and 16 deletions

View file

@ -55,7 +55,8 @@ Note that some of these parameters are explained in details in other sections of
mermaidInitialize = "{ \"theme\": \"default\" }"
# Hide Next and Previous page buttons normally displayed full height beside content
disableNextPrev = true
# Order sections in menu by "weight" or "title". Default to "weight"
# Order sections in menu by "weight" or "title". Default to "weight";
# this can be overridden in the pages frontmatter
ordersectionsby = "weight"
# Change default color scheme with a variant one. Can be "red", "blue", "green".
themeVariant = ""

View file

@ -103,6 +103,8 @@ disableToc = "false"
menuTitle = ""
# If set, this will explicitly override common rules for the expand state of a page's menu entry
alwaysopen = true
# If set, this will explicitly override common rules for the sorting order of a page's submenu entries
ordersectionsby = "title"
# The title of the page in menu will be prefixed by this HTML content
pre = ""
# The title of the page in menu will be postfixed by this HTML content

View file

@ -16,7 +16,7 @@ Use the children shortcode to list the child pages of a page and the further des
| showhidden | "false" | When true, child pages hidden from the menu will be displayed |
| description | "false" | Allows you to include a short text under each page in the list. When no description exists for the page, children shortcode takes the first 70 words of your content. [Read more info about summaries on gohugo.io](https://gohugo.io/content/summaries/) |
| depth | 1 | Enter a number to specify the depth of descendants to display. For example, if the value is 2, the shortcode will display 2 levels of child pages. **Tips:** set 999 to get all descendants |
| sort | [ordersectionsby]({{% relref "basics/configuration#global-site-parameters" %}}) | Sort children by **weight** - to sort on menu order, **title** - to sort alphabetically on menu label |
| sort | [ordersectionsby]({{% relref "basics/configuration#global-site-parameters" %}}) | Sort children by **weight**, to sort on menu order - **title**, to sort alphabetically on menu label. If not set it is sorted by the `ordersectionsby` setting of the site and the pages frontmatter |
## Demo

View file

@ -21,8 +21,10 @@
{{- end }}
<div class="highlightable">
<ul class="topics">
{{- $defaultOrdersectionsby := .Site.Params.ordersectionsby | default "weight" }}
{{- $currentOrdersectionsby := .Site.Home.Params.ordersectionsby | default $defaultOrdersectionsby }}
{{- $defaultAlwaysopen := .Site.Params.alwaysopen | default false }}
{{- if eq .Site.Params.ordersectionsby "title" }}
{{- if eq $currentOrdersectionsby "title" }}
{{- range .Site.Home.Sections.ByTitle }}
{{- template "section-tree-nav" dict "sect" . "currentnode" $currentNode "showvisitedlinks" $showvisitedlinks "alwaysopen" $defaultAlwaysopen }}
{{- end }}
@ -105,6 +107,8 @@
{{- if $hidden }}
{{- else if .IsSection }}
{{- safeHTML .Params.head }}
{{- $defaultOrdersectionsby := .Site.Params.ordersectionsby | default "weight" }}
{{- $currentOrdersectionsby := .Params.ordersectionsby | default $defaultOrdersectionsby }}
{{- $currentAlwaysopen := .Params.alwaysopen | default $alwaysopen }}
<li data-nav-id="{{.RelPermalink}}" title="{{.Title}}" class="dd-item{{if eq .File.UniqueID $currentFileUniqueID}} active{{end}}{{if .IsAncestor $currentNode }} parent{{end}}{{if $currentAlwaysopen}} alwaysopen{{end}}"><a href="{{.RelPermalink}}">
{{- partial "menu-pre.html" . }}{{ or .Params.menuTitle .LinkTitle .Title }}{{ partial "menu-post.html" . }}
@ -116,7 +120,7 @@
{{- $pages = (.Pages | union .Sections) }}
{{- end }}
{{- $defaultAlwaysopen := .Site.Params.alwaysopen | default true }}
{{- if eq .Site.Params.ordersectionsby "title" }}
{{- if eq $currentOrdersectionsby "title" }}
{{- range $pages.ByTitle }}
{{- template "section-tree-nav" dict "sect" . "currentnode" $currentNode "showvisitedlinks" $showvisitedlinks "alwaysopen" $defaultAlwaysopen }}
{{- end }}

View file

@ -42,7 +42,9 @@
{{- end }}
{{- $pages := ($currentNode.Scratch.Get "relearnSubPages") }}
{{- if eq .Site.Params.ordersectionsby "title"}}
{{- $defaultOrdersectionsby := .Site.Params.ordersectionsby | default "weight" }}
{{- $currentOrdersectionsby := .node.Params.ordersectionsby | default $defaultOrdersectionsby }}
{{- if eq $currentOrdersectionsby "title"}}
{{- range $pages.ByTitle }}
{{- template "relearn-structure" dict "node" . "currentnode" $currentNode "hiddenstem" $hidden_stem "hiddencurrent" $hidden_from_current }}
{{- end}}

View file

@ -3,7 +3,7 @@
{{- $style := .Get "style" | default "li" }}
{{- $depth := .Get "depth" | default 1 }}
{{- $withDescription := .Get "description" | default false }}
{{- $sortTerm := .Get "sort" | default (.Site.Params.ordersectionsby | default "weight") | lower }}
{{- $sortTerm := .Get "sort" | lower }}
{{- $containerstyle := .Get "containerstyle" | default "ul" }}
{{ (printf "<%s class=\"children children-%s children-sort-%s\">" $containerstyle $style $sortTerm)|safeHTML }}
@ -14,15 +14,17 @@
{{- $pages = (.Page.Pages | union .Page.Sections) }}
{{- end }}
{{- if eq $sortTerm "weight" }}
{{- $defaultOrdersectionsby := .Site.Params.ordersectionsby | default "weight" }}
{{- $currentOrdersectionsby := $sortTerm | default (.Page.Params.ordersectionsby | default $defaultOrdersectionsby) }}
{{- if eq $currentOrdersectionsby "weight" }}
{{- template "childs" dict "menu" $pages.ByWeight "containerstyle" $containerstyle "style" $style "showhidden" $showhidden "count" 1 "depth" $depth "pages" .Site.Pages "description" $withDescription "sortTerm" $sortTerm }}
{{- else if or (eq $sortTerm "name") (eq $sortTerm "title") }}
{{- else if or (eq $currentOrdersectionsby "name") (eq $currentOrdersectionsby "title") }}
{{- template "childs" dict "menu" $pages.ByTitle "containerstyle" $containerstyle "style" $style "showhidden" $showhidden "count" 1 "depth" $depth "pages" .Site.Pages "description" $withDescription "sortTerm" $sortTerm }}
{{- else if eq $sortTerm "publishdate" }}
{{- else if eq $currentOrdersectionsby "publishdate" }}
{{- template "childs" dict "menu" $pages.ByPublishDate "containerstyle" $containerstyle "style" $style "showhidden" $showhidden "count" 1 "depth" $depth "pages" .Site.Pages "description" $withDescription "sortTerm" $sortTerm }}
{{- else if eq $sortTerm "date" }}
{{- else if eq $currentOrdersectionsby "date" }}
{{- template "childs" dict "menu" $pages.ByDate "containerstyle" $containerstyle "style" $style "showhidden" $showhidden "count" 1 "depth" $depth "pages" .Site.Pages "description" $withDescription "sortTerm" $sortTerm }}
{{- else if eq $sortTerm "length" }}
{{- else if eq $currentOrdersectionsby "length" }}
{{- template "childs" dict "menu" $pages.ByLength "containerstyle" $containerstyle "style" $style "showhidden" $showhidden "count" 1 "depth" $depth "pages" .Site.Pages "description" $withDescription "sortTerm" $sortTerm }}
{{- else }}
{{- template "childs" dict "menu" $pages "containerstyle" $containerstyle "style" $style "showhidden" $showhidden "count" 1 "depth" $depth "pages" .Site.Pages "description" $withDescription "sortTerm" $sortTerm }}
@ -69,15 +71,17 @@
{{- $pages = (.Page.Pages | union .Page.Sections) }}
{{- end }}
{{- if eq $.sortTerm "weight" }}
{{- $defaultOrdersectionsby := .Site.Params.ordersectionsby | default "weight" }}
{{- $currentOrdersectionsby := $.sortTerm | default (.Page.Params.ordersectionsby | default $defaultOrdersectionsby) }}
{{- if eq $currentOrdersectionsby "weight" }}
{{- template "childs" dict "menu" $pages.ByWeight "containerstyle" $.containerstyle "style" $.style "showhidden" $.showhidden "count" (add $.count 1) "depth" $.depth "pages" $.pages "description" $.description "sortTerm" $.sortTerm }}
{{- else if or (eq $.sortTerm "name") (eq $.sortTerm "title") }}
{{- else if or (eq $.currentOrdersectionsby "name") (eq $.currentOrdersectionsby "title") }}
{{- template "childs" dict "menu" $pages.ByTitle "containerstyle" $.containerstyle "style" $.style "showhidden" $.showhidden "count" (add $.count 1) "depth" $.depth "pages" $.pages "description" $.description "sortTerm" $.sortTerm }}
{{- else if eq $.sortTerm "publishdate" }}
{{- else if eq $.currentOrdersectionsby "publishdate" }}
{{- template "childs" dict "menu" $pages.ByPublishDate "containerstyle" $.containerstyle "style" $.style "showhidden" $.showhidden "count" (add $.count 1) "depth" $.depth "pages" $.pages "description" $.description "sortTerm" $.sortTerm }}
{{- else if eq $.sortTerm "date" }}
{{- else if eq $.currentOrdersectionsby "date" }}
{{- template "childs" dict "menu" $pages.ByDate "containerstyle" $.containerstyle "style" $.style "showhidden" $.showhidden "count" (add $.count 1) "depth" $.depth "pages" $.pages "description" $.description "sortTerm" $.sortTerm }}
{{- else if eq $.sortTerm "length" }}
{{- else if eq $.currentOrdersectionsby "length" }}
{{- template "childs" dict "menu" $pages.ByLength "containerstyle" $.containerstyle "style" $.style "showhidden" $.showhidden "count" (add $.count 1) "depth" $.depth "pages" $.pages "description" $.description "sortTerm" $.sortTerm }}
{{- else }}
{{- template "childs" dict "menu" $pages "containerstyle" $.containerstyle "style" $.style "showhidden" $.showhidden "count" (add $.count 1) "depth" $.depth "pages" $.pages "description" $.description "sortTerm" $.sortTerm }}