From 1317e5697fa22527c8bc3200b5b4bcf86c1abab3 Mon Sep 17 00:00:00 2001 From: Xin <5097752+imfing@users.noreply.github.com> Date: Fri, 6 Mar 2026 22:58:13 +0000 Subject: [PATCH] fix(sidebar): include URL menu.main items in mobile sidebar --- layouts/_partials/sidebar.html | 155 +++++++++++++++++++++++++++------ tests/accessibility.spec.ts | 12 +++ 2 files changed, 140 insertions(+), 27 deletions(-) diff --git a/layouts/_partials/sidebar.html b/layouts/_partials/sidebar.html index ac03cd7..034e4be 100644 --- a/layouts/_partials/sidebar.html +++ b/layouts/_partials/sidebar.html @@ -78,45 +78,146 @@ {{- $level := .level -}} {{- $toc := .toc | default false -}} {{- $useMainMenu := and (eq $level 0) $toc -}} - {{- $menuNamesByPath := dict -}} + {{- $mainMenuEntries := slice -}} - {{- $items := where (union .context.RegularPages .context.Sections) "Params.sidebar.exclude" "!=" true -}} + {{- $items := slice -}} {{- if $useMainMenu -}} - {{- $items = slice -}} {{- range $menuItem := site.Menus.main -}} + {{- $menuType := $menuItem.Params.type | default "" -}} + {{- $isIconOnly := and $menuItem.Params.icon (ne $menuType "link") -}} + {{- /* Keep only navigation links in the mobile sidebar. */ -}} + {{- if or (eq $menuType "search") (eq $menuType "theme-toggle") (eq $menuType "language-switch") $isIconOnly -}} + {{- continue -}} + {{- end -}} + + {{- $menuTitle := or (T $menuItem.Identifier) $menuItem.Name -}} + {{- /* Dropdown parents mirror navbar behavior: render a labeled group of child links. */ -}} + {{- if $menuItem.HasChildren -}} + {{- $childEntries := slice -}} + {{- range $childItem := $menuItem.Children -}} + {{- $childType := $childItem.Params.type | default "" -}} + {{- $childIsIconOnly := and $childItem.Params.icon (ne $childType "link") -}} + {{- if or (eq $childType "search") (eq $childType "theme-toggle") (eq $childType "language-switch") $childIsIconOnly -}} + {{- continue -}} + {{- end -}} + + {{- $childTitle := or (T $childItem.Identifier) $childItem.Name -}} + {{- with $childItem.Page -}} + {{- if ne .Params.sidebar.exclude true -}} + {{- $childEntries = $childEntries | append (dict "title" $childTitle "link" .RelPermalink) -}} + {{- end -}} + {{- continue -}} + {{- end -}} + + {{- $childLink := $childItem.URL -}} + {{- with $childItem.PageRef -}} + {{- if hasPrefix . "/" -}} + {{- $childLink = relLangURL (strings.TrimPrefix "/" .) -}} + {{- end -}} + {{- end -}} + {{- if $childLink -}} + {{- $childEntries = $childEntries | append (dict "title" $childTitle "link" $childLink) -}} + {{- end -}} + {{- end -}} + + {{- if gt (len $childEntries) 0 -}} + {{- $mainMenuEntries = $mainMenuEntries | append (dict "type" "group" "title" $menuTitle "children" $childEntries) -}} + {{- end -}} + {{- continue -}} + {{- end -}} + + {{- /* Normalize page-backed entries so we keep nested tree behavior. */ -}} {{- with $menuItem.Page -}} {{- if ne .Params.sidebar.exclude true -}} - {{- $items = $items | append . -}} - {{- $menuNamesByPath = merge $menuNamesByPath (dict .Path (or (T $menuItem.Identifier) $menuItem.Name)) -}} + {{- $mainMenuEntries = $mainMenuEntries | append (dict "type" "page" "item" . "title" $menuTitle) -}} + {{- end -}} + {{- continue -}} + {{- end -}} + + {{- $link := $menuItem.URL -}} + {{- with $menuItem.PageRef -}} + {{- if hasPrefix . "/" -}} + {{- $link = relLangURL (strings.TrimPrefix "/" .) -}} {{- end -}} {{- end -}} + {{- if $link -}} + {{- $mainMenuEntries = $mainMenuEntries | append (dict "type" "url" "link" $link "title" $menuTitle) -}} + {{- end -}} {{- end -}} + {{- else -}} + {{- $items = where (union .context.RegularPages .context.Sections) "Params.sidebar.exclude" "!=" true -}} {{- end -}} - {{- if gt (len $items) 0 -}} + {{- $hasItems := gt (len $items) 0 -}} + {{- if $useMainMenu -}} + {{- $hasItems = gt (len $mainMenuEntries) 0 -}} + {{- end -}} + + {{- if $hasItems -}} {{- if eq $level 0 -}} - {{- $topLevelItems := cond $useMainMenu $items $items.ByWeight -}} - {{- range $topLevelItems }} - {{- if .Params.sidebar.separator -}} -