docsy icon indicating copy to clipboard operation
docsy copied to clipboard

Sidebar tree sorting by date if there is no `weight`

Open at055612 opened this issue 3 years ago • 2 comments

If all the pages/sections in a section have no weight set then the sidebar tree appears to show them in date modified order as this is the default page sorting for Hugo. The sidebar tree (and the list page) will show items in this order:

  1. Items with a weight >0, sorted by weight ASC
  2. Items with no weight, sorted by date DESC then LinkTitle ASC then filePath ASC

I think (for the docs part of the site at least) it would be preferable to have the sidebar tree (and list page) sort items by weight, then by LinkTitle, then if you want all items sorted alphabetically you can just leave the weights unset. Sorting by date makes no sense for a documentation site and having to re-adjust all the weights each time a page is added is not ideal.

The following makes it sort by weight then by LinkTitle, though this breaks things for the blog part of the site which needs to be sorted by weight then date. This is a bit hacky as I am still getting to grips with hugo and the go templating. One issue is that .Pages.ByWeight will put pages with no weight (i.e. 0) at the end, but .GroupBy "Weight" doesn't know about this special case so puts pages with no weight at the top, hence the repeated range blocks. I suspect there is a much neater way of fixing this though.

diff --git a/layouts/partials/sidebar-tree.html b/layouts/partials/sidebar-tree.html
index 258267b..b377fcf 100644
--- a/layouts/partials/sidebar-tree.html
+++ b/layouts/partials/sidebar-tree.html
@@ -45,8 +45,7 @@
 {{ $activePath := and (not $shouldDelayActive) ($p.IsDescendant $s) -}}
 {{ $show := cond (or (lt $ulNr $ulShow) $activePath (and (not $shouldDelayActive) (eq $s.Parent $p.Parent)) (and (not $shouldDelayActive) (eq $s.Parent $p)) (not $p.Site.Params.ui.sidebar_menu_compact) (and (not $shouldDelayActive) ($p.IsDescendant $s.Parent))) true false -}}
 {{ $mid := printf "m-%s" ($s.RelPermalink | anchorize) -}}
-{{ $pages_tmp := where (union $s.Pages $s.Sections).ByWeight ".Params.toc_hide" "!=" true -}}
-{{ $pages := $pages_tmp | first $sidebarMenuTruncate -}}
+{{ $pages := where $s.Pages ".Params.toc_hide" "!=" true -}}
 {{ $withChild := gt (len $pages) 0 -}}
 {{ $manualLink := cond (isset $s.Params "manuallink") $s.Params.manualLink ( cond (isset $s.Params "manuallinkrelref") (relref $s $s.Params.manualLinkRelref) $s.RelPermalink) -}}
 {{ $manualLinkTitle := cond (isset $s.Params "manuallinktitle") $s.Params.manualLinkTitle $s.Title -}}
@@ -59,13 +58,31 @@
   {{- end }}
   {{- if $withChild }}
   {{- $ulNr := add $ulNr 1 }}
+  {{ $page_counter := 0 }}
+  {{ $grouped_pages := $pages.GroupBy "Weight" }}
   <ul class="ul-{{ $ulNr }}{{ if (gt $ulNr 1)}} foldable{{end}}">
-    {{ range $pages -}}
+    {{ range (where $grouped_pages "Key" ">" 0) -}}
+    {{ range sort .Pages "LinkTitle" -}}
+    {{ $page_counter = add $page_counter 1 }}
+    {{ if le $page_counter $sidebarMenuTruncate -}}
     {{ if (not (and (eq $s $p.Site.Home) (eq .Params.toc_root true))) -}}
     {{ template "section-tree-nav-section" (dict "page" $p "section" . "shouldDelayActive" $shouldDelayActive "sidebarMenuTruncate" $sidebarMenuTruncate "ulNr" $ulNr "ulShow" $ulShow) }}
     {{- end }}
     {{- end }}
+    {{- end }}
+    {{- end }}
+
+    {{ range (where $grouped_pages ("Key" | default 0) "==" 0) -}}
+    {{ range sort .Pages "LinkTitle" -}}
+    {{ $page_counter = add $page_counter 1 }}
+    {{ if le $page_counter $sidebarMenuTruncate -}}
+    {{ if (not (and (eq $s $p.Site.Home) (eq .Params.toc_root true))) -}}
+    {{ template "section-tree-nav-section" (dict "page" $p "section" . "shouldDelayActive" $shouldDelayActive "sidebarMenuTruncate" $sidebarMenuTruncate "ulNr" $ulNr "ulShow" $ulShow) }}
+    {{- end }}
+    {{- end }}
+    {{- end }}
+    {{- end }}
   </ul>
   {{- end }}
 </li>
-{{- end }}
\ No newline at end of file
+{{- end }}

at055612 avatar Feb 14 '22 17:02 at055612

I agree with your thought about default sorting. Are there any other community opinions if we should change this?

raum51 avatar Mar 12 '22 08:03 raum51

+1. Thanks for sharing the diff @at055612. Worked like a charm.

emily-potyraj avatar Jul 01 '22 23:07 emily-potyraj

Hi, is this issue still open? I want to work on this issue; please assign me to this.

aarushisoni avatar Jan 23 '23 14:01 aarushisoni

Hi @aarushisoni we don't assign issues in this project. If you would like to contribute please check out our Contributing Guidelines.

emckean avatar Jan 23 '23 23:01 emckean