Skip to content

Commit f37d79c

Browse files
[Fix] Scope sidebar links to version correctly
1 parent 684dd2c commit f37d79c

1 file changed

Lines changed: 334 additions & 0 deletions

File tree

layouts/_partials/sidebar.html

Lines changed: 334 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,334 @@
1+
{{- $context := .context -}}
2+
3+
{{- $disableSidebar := .disableSidebar | default false -}}
4+
{{- $displayPlaceholder := .displayPlaceholder | default false -}}
5+
6+
{{- $navRoot := cond (eq site.Home.Type "docs") site.Home $context.FirstSection -}}
7+
{{- $pageURL := $context.RelPermalink -}}
8+
9+
{{- /* Rhize override: pages under /versions/{VERSION}/ get a sidebar scoped
10+
to that version's own content tree, not the top-level (latest) tree. */ -}}
11+
{{- $pathSegments := strings.Split (strings.Trim $pageURL "/") "/" -}}
12+
{{- if and (ge (len $pathSegments) 2) (eq (index $pathSegments 0) "versions") -}}
13+
{{- with site.GetPage (printf "/versions/%s" (index $pathSegments 1)) -}}
14+
{{- $navRoot = . -}}
15+
{{- end -}}
16+
{{- end -}}
17+
18+
{{- if .context.Params.sidebar.hide -}}
19+
{{- $disableSidebar = true -}}
20+
{{- $displayPlaceholder = false -}}
21+
{{- end -}}
22+
23+
{{- $sidebarClass := "hx:md:sticky" -}}
24+
{{- if $disableSidebar -}}
25+
{{- if $displayPlaceholder -}}
26+
{{- $sidebarClass = "hx:md:hidden hx:xl:block" -}}
27+
{{- else -}}
28+
{{- $sidebarClass = "hx:md:hidden" -}}
29+
{{- end -}}
30+
{{- end -}}
31+
32+
<aside class="hextra-sidebar-container hx:flex hx:flex-col hx:print:hidden hx:md:top-16 hx:md:shrink-0 hx:md:w-64 hx:md:self-start hx:max-md:[transform:translate3d(0,-100%,0)] {{ $sidebarClass }}">
33+
{{- if (site.Params.search.enable | default true) -}}
34+
<!-- Search bar on small screen -->
35+
<div class="hx:px-4 hx:pt-4 hx:md:hidden">
36+
{{ partial "search.html" (dict "location" "sidebar") }}
37+
</div>
38+
{{- end -}}
39+
<div class="hextra-scrollbar hx:overflow-y-auto hx:overflow-x-hidden hx:p-4 hx:grow hx:md:h-[calc(100vh-var(--navbar-height)-var(--menu-height))]">
40+
<ul class="hx:flex hx:flex-col hx:gap-1 hx:md:hidden">
41+
<!-- Nav -->
42+
{{ template "sidebar-main" (dict "context" site.Home "pageURL" $pageURL "page" $context "toc" true) -}}
43+
{{ template "sidebar-footer" }}
44+
</ul>
45+
46+
<!-- Sidebar on large screen -->
47+
{{- if $disableSidebar -}}
48+
{{- if $displayPlaceholder }}<div class="hx:max-xl:hidden hx:h-0 hx:w-64 hx:shrink-0"></div>{{ end -}}
49+
{{ .context.Store.Set "enableFooterSwitches" true }}
50+
{{- else -}}
51+
<ul class="hx:flex hx:flex-col hx:gap-1 hx:max-md:hidden">
52+
{{ template "sidebar-main" (dict "context" $navRoot "page" $context "pageURL" $pageURL) }}
53+
{{ template "sidebar-footer" }}
54+
</ul>
55+
{{ end -}}
56+
</div>
57+
{{/* Hide theme switch when sidebar is disabled */}}
58+
{{ $switchesClass := cond $disableSidebar "hx:md:hidden" "" -}}
59+
{{ $displayThemeToggle := (site.Params.theme.displayToggle | default true) -}}
60+
61+
{{ if or hugo.IsMultilingual $displayThemeToggle }}
62+
<div class="{{ $switchesClass }} {{ with hugo.IsMultilingual }}hx:justify-end{{ end }} hx:sticky hx:bottom-0 hx:max-h-(--menu-height) hx:bg-white hx:dark:bg-dark hx:mx-4 hx:py-4 hx:shadow-[0_-12px_16px_#fff] hx:flex hx:items-center hx:gap-2 hx:border-gray-200 hx:dark:border-neutral-800 hx:dark:shadow-[0_-12px_16px_#111] hx:contrast-more:border-neutral-400 hx:contrast-more:shadow-none hx:contrast-more:dark:shadow-none hx:border-t" data-toggle-animation="show">
63+
{{- with hugo.IsMultilingual -}}
64+
{{- partial "language-switch" (dict "context" $context "grow" true) -}}
65+
{{- with $displayThemeToggle }}{{ partial "theme-toggle" (dict "hideLabel" true "location" "bottom-right") }}{{ end -}}
66+
{{- else -}}
67+
{{- with $displayThemeToggle -}}
68+
<div class="hx:flex hx:grow hx:flex-col">{{ partial "theme-toggle" }}</div>
69+
{{- end -}}
70+
{{- end -}}
71+
</div>
72+
{{- end -}}
73+
</aside>
74+
75+
{{- define "sidebar-main" -}}
76+
{{ template "sidebar-tree" (dict "context" .context "level" 0 "page" .page "pageURL" .pageURL "toc" (.toc | default false)) }}
77+
{{- end -}}
78+
79+
{{- define "sidebar-tree" -}}
80+
{{- if ge .level 4 -}}
81+
{{- return -}}
82+
{{- end -}}
83+
84+
{{- $context := .context -}}
85+
{{- $page := .page }}
86+
{{- $pageURL := .page.RelPermalink -}}
87+
{{- $level := .level -}}
88+
{{- $toc := .toc | default false -}}
89+
{{- $useMainMenu := and (eq $level 0) $toc -}}
90+
{{- $mainMenuEntries := slice -}}
91+
92+
{{- $items := where (union .context.RegularPages .context.Sections) "Params.sidebar.exclude" "!=" true -}}
93+
{{- if $useMainMenu -}}
94+
{{- range $menuItem := site.Menus.main -}}
95+
{{- $menuType := $menuItem.Params.type | default "" -}}
96+
{{- $isIconOnly := and $menuItem.Params.icon (ne $menuType "link") -}}
97+
{{- /* Keep only navigation links in the mobile sidebar. */ -}}
98+
{{- if or (eq $menuType "search") (eq $menuType "theme-toggle") (eq $menuType "language-switch") $isIconOnly -}}
99+
{{- continue -}}
100+
{{- end -}}
101+
102+
{{- $menuTitle := or (T $menuItem.Identifier) $menuItem.Name -}}
103+
{{- /* Dropdown parents mirror navbar behavior: render a labeled group of child links. */ -}}
104+
{{- if $menuItem.HasChildren -}}
105+
{{- $childEntries := slice -}}
106+
{{- range $childItem := $menuItem.Children -}}
107+
{{- $childType := $childItem.Params.type | default "" -}}
108+
{{- $childIsIconOnly := and $childItem.Params.icon (ne $childType "link") -}}
109+
{{- if or (eq $childType "search") (eq $childType "theme-toggle") (eq $childType "language-switch") $childIsIconOnly -}}
110+
{{- continue -}}
111+
{{- end -}}
112+
113+
{{- $childTitle := or (T $childItem.Identifier) $childItem.Name -}}
114+
{{- $childPage := $childItem.Page -}}
115+
{{- with $childItem.PageRef -}}
116+
{{- with $page.Site.GetPage . -}}
117+
{{- $childPage = . -}}
118+
{{- end -}}
119+
{{- end -}}
120+
{{- with $childPage -}}
121+
{{- if ne .Params.sidebar.exclude true -}}
122+
{{- $childEntries = $childEntries | append (dict "title" $childTitle "link" .RelPermalink) -}}
123+
{{- end -}}
124+
{{- continue -}}
125+
{{- end -}}
126+
127+
{{- $childLink := $childItem.URL -}}
128+
{{- with $childItem.PageRef -}}
129+
{{- if hasPrefix . "/" -}}
130+
{{- $childLink = relLangURL (strings.TrimPrefix "/" .) -}}
131+
{{- end -}}
132+
{{- end -}}
133+
{{- if $childLink -}}
134+
{{- $childEntries = $childEntries | append (dict "title" $childTitle "link" $childLink) -}}
135+
{{- end -}}
136+
{{- end -}}
137+
138+
{{- if gt (len $childEntries) 0 -}}
139+
{{- $mainMenuEntries = $mainMenuEntries | append (dict "type" "group" "title" $menuTitle "children" $childEntries) -}}
140+
{{- end -}}
141+
{{- continue -}}
142+
{{- end -}}
143+
144+
{{- /* Normalize page-backed entries so we keep nested tree behavior. */ -}}
145+
{{- $menuPage := $menuItem.Page -}}
146+
{{- with $menuItem.PageRef -}}
147+
{{- with $page.Site.GetPage . -}}
148+
{{- $menuPage = . -}}
149+
{{- end -}}
150+
{{- end -}}
151+
{{- with $menuPage -}}
152+
{{- if ne .Params.sidebar.exclude true -}}
153+
{{- $mainMenuEntries = $mainMenuEntries | append (dict "type" "page" "item" . "title" $menuTitle) -}}
154+
{{- end -}}
155+
{{- continue -}}
156+
{{- end -}}
157+
158+
{{- $link := $menuItem.URL -}}
159+
{{- with $menuItem.PageRef -}}
160+
{{- if hasPrefix . "/" -}}
161+
{{- $link = relLangURL (strings.TrimPrefix "/" .) -}}
162+
{{- end -}}
163+
{{- end -}}
164+
{{- if $link -}}
165+
{{- $mainMenuEntries = $mainMenuEntries | append (dict "type" "url" "link" $link "title" $menuTitle) -}}
166+
{{- end -}}
167+
{{- end -}}
168+
{{- end -}}
169+
170+
{{- $useMainMenuEntries := and $useMainMenu (gt (len $mainMenuEntries) 0) -}}
171+
{{- $hasItems := or (gt (len $items) 0) $useMainMenuEntries -}}
172+
173+
{{- if $hasItems -}}
174+
{{- if eq $level 0 -}}
175+
{{- if $useMainMenuEntries -}}
176+
{{- /* Mixed list: page entries render trees; url entries render leaf links. */ -}}
177+
{{- range $entry := $mainMenuEntries -}}
178+
{{- if eq (index $entry "type") "page" -}}
179+
{{- $item := index $entry "item" -}}
180+
{{- if $item.Params.sidebar.separator -}}
181+
<li class="[word-break:break-word] hx:mt-5 hx:mb-2 hx:px-2 hx:py-1.5 hx:text-sm hx:font-semibold hx:text-gray-900 hx:first:mt-0 hx:dark:text-gray-100">
182+
<span class="hx:cursor-default">{{ index $entry "title" }}</span>
183+
</li>
184+
{{- else -}}
185+
{{- $active := eq (strings.TrimSuffix "/" $pageURL) (strings.TrimSuffix "/" $item.RelPermalink) -}}
186+
{{- $shouldOpen := or ($item.Params.sidebar.open) ($item.IsAncestor $page) $active | default true }}
187+
<li class="{{ if $shouldOpen }}open{{ end }}">
188+
{{- template "sidebar-item-link" dict "context" $item "active" $active "open" $shouldOpen "title" (index $entry "title") "link" $item.RelPermalink -}}
189+
{{- if and $toc $active (ne $item.Params.toc false) -}}
190+
{{- template "sidebar-toc" dict "page" $item -}}
191+
{{- end -}}
192+
{{- template "sidebar-tree" dict "context" $item "page" $page "pageURL" $pageURL "level" (add $level 1) "toc" $toc -}}
193+
</li>
194+
{{- end -}}
195+
{{- else if eq (index $entry "type") "group" -}}
196+
<li class="open">
197+
<div class="hextra-sidebar-item hx:group hx:relative hx:flex hx:items-center">
198+
<span class="hx:flex hx:grow hx:cursor-default hx:px-2 hx:py-1.5 hx:text-sm hx:font-semibold hx:text-gray-900 hx:dark:text-gray-100">
199+
{{- index $entry "title" -}}
200+
</span>
201+
</div>
202+
<div class="hextra-sidebar-children hx:ltr:pr-0 hx:rtl:pl-0 hx:overflow-hidden">
203+
<ul class='hx:relative hx:flex hx:flex-col hx:gap-1 hx:before:absolute hx:before:inset-y-1 hx:before:w-px hx:before:bg-gray-200 hx:before:content-[""] hx:ltr:ml-3 hx:ltr:pl-3 hx:ltr:before:left-0 hx:rtl:mr-3 hx:rtl:pr-3 hx:rtl:before:right-0 hx:dark:before:bg-neutral-800'>
204+
{{- range $child := index $entry "children" -}}
205+
{{- $link := index $child "link" -}}
206+
{{- $active := eq (strings.TrimSuffix "/" $pageURL) (strings.TrimSuffix "/" $link) -}}
207+
<li class="hx:flex hx:flex-col">
208+
{{- template "sidebar-item-link" dict "active" $active "open" false "title" (index $child "title") "link" $link -}}
209+
</li>
210+
{{- end -}}
211+
</ul>
212+
</div>
213+
</li>
214+
{{- else -}}
215+
{{- $link := index $entry "link" -}}
216+
{{- $active := eq (strings.TrimSuffix "/" $pageURL) (strings.TrimSuffix "/" $link) -}}
217+
<li>{{ template "sidebar-item-link" dict "active" $active "open" false "title" (index $entry "title") "link" $link }}</li>
218+
{{- end -}}
219+
{{- end -}}
220+
{{- else -}}
221+
{{- range $items.ByWeight }}
222+
{{- if .Params.sidebar.separator -}}
223+
<li class="[word-break:break-word] hx:mt-5 hx:mb-2 hx:px-2 hx:py-1.5 hx:text-sm hx:font-semibold hx:text-gray-900 hx:first:mt-0 hx:dark:text-gray-100">
224+
<span class="hx:cursor-default">{{ partial "utils/title" . }}</span>
225+
</li>
226+
{{- else -}}
227+
{{- $active := eq $pageURL .RelPermalink -}}
228+
{{- $shouldOpen := or (.Params.sidebar.open) (.IsAncestor $page) $active | default true }}
229+
<li class="{{ if $shouldOpen }}open{{ end }}">
230+
{{- $linkTitle := partial "utils/title" . -}}
231+
{{- template "sidebar-item-link" dict "context" . "active" $active "open" $shouldOpen "title" $linkTitle "link" .RelPermalink -}}
232+
{{- if and $toc $active (ne .Params.toc false) -}}
233+
{{- template "sidebar-toc" dict "page" . -}}
234+
{{- end -}}
235+
{{- template "sidebar-tree" dict "context" . "page" $page "pageURL" $pageURL "level" (add $level 1) "toc" $toc -}}
236+
</li>
237+
{{- end -}}
238+
{{- end -}}
239+
{{- end -}}
240+
{{- else -}}
241+
<div class="hextra-sidebar-children hx:ltr:pr-0 hx:rtl:pl-0 hx:overflow-hidden">
242+
<ul class='hx:relative hx:flex hx:flex-col hx:gap-1 hx:before:absolute hx:before:inset-y-1 hx:before:w-px hx:before:bg-gray-200 hx:before:content-[""] hx:ltr:ml-3 hx:ltr:pl-3 hx:ltr:before:left-0 hx:rtl:mr-3 hx:rtl:pr-3 hx:rtl:before:right-0 hx:dark:before:bg-neutral-800'>
243+
{{- range $items.ByWeight }}
244+
{{- $active := eq $pageURL .RelPermalink -}}
245+
{{- $shouldOpen := or (.Params.sidebar.open) (.IsAncestor $page) $active | default true }}
246+
{{- $linkTitle := partial "utils/title" . -}}
247+
<li class="hx:flex hx:flex-col {{ if $shouldOpen }}open{{ end }}">
248+
{{- template "sidebar-item-link" dict "context" . "active" $active "open" $shouldOpen "title" $linkTitle "link" .RelPermalink -}}
249+
{{- if and $toc $active (ne .Params.toc false) -}}
250+
{{ template "sidebar-toc" dict "page" . }}
251+
{{- end }}
252+
{{ template "sidebar-tree" dict "context" . "page" $page "pageURL" $pageURL "level" (add $level 1) "toc" $toc }}
253+
</li>
254+
{{- end -}}
255+
</ul>
256+
</div>
257+
{{- end -}}
258+
{{- end -}}
259+
{{- end -}}
260+
261+
{{- define "sidebar-toc" -}}
262+
{{ $page := .page }}
263+
{{ with $page.Fragments.Headings }}
264+
<ul class='hx:flex hx:flex-col hx:gap-1 hx:relative hx:before:absolute hx:before:inset-y-1 hx:before:w-px hx:before:bg-gray-200 hx:before:content-[""] hx:dark:before:bg-neutral-800 hx:ltr:pl-3 hx:ltr:before:left-0 hx:rtl:pr-3 hx:rtl:before:right-0 hx:ltr:ml-3 hx:rtl:mr-3'>
265+
{{- range . }}
266+
{{- with .Headings }}
267+
{{- range . -}}
268+
<li>
269+
<a
270+
href="#{{ anchorize .ID }}"
271+
class="hx:flex hx:rounded-sm hx:px-2 hx:py-1.5 hx:text-sm hx:transition-colors [word-break:break-word] hx:cursor-pointer [-webkit-tap-highlight-color:transparent] [-webkit-touch-callout:none] hx:contrast-more:border hx:gap-2 hx:before:opacity-25 hx:before:content-['#'] hx:text-gray-500 hx:hover:bg-gray-100 hx:hover:text-gray-900 hx:dark:text-neutral-400 hx:dark:hover:bg-primary-100/5 hx:dark:hover:text-gray-50 hx:contrast-more:text-gray-900 hx:contrast-more:dark:text-gray-50 hx:contrast-more:border-transparent hx:contrast-more:hover:border-gray-900 hx:contrast-more:dark:hover:border-gray-50"
272+
>
273+
{{- .Title | safeHTML | plainify | htmlUnescape -}}
274+
</a>
275+
</li>
276+
{{ end -}}
277+
{{ end -}}
278+
{{ end -}}
279+
</ul>
280+
{{ end }}
281+
{{- end -}}
282+
283+
{{- define "sidebar-footer" -}}
284+
{{- range site.Menus.sidebar -}}
285+
{{- $name := or (T .Identifier) .Name -}}
286+
{{ if eq .Params.type "separator" }}
287+
<li class="[word-break:break-word] hx:mt-5 hx:mb-2 hx:px-2 hx:py-1.5 hx:text-sm hx:font-semibold hx:text-gray-900 hx:first:mt-0 hx:dark:text-gray-100">
288+
<span class="hx:cursor-default">{{ $name }}</span>
289+
</li>
290+
{{ else }}
291+
{{- $link := .URL -}}
292+
{{- with .PageRef -}}
293+
{{- if hasPrefix . "/" -}}
294+
{{- $link = relLangURL (strings.TrimPrefix "/" .) -}}
295+
{{- end -}}
296+
{{- end -}}
297+
<li>{{ template "sidebar-item-link" dict "active" false "open" false "title" $name "link" $link }}</li>
298+
{{ end }}
299+
{{- end -}}
300+
{{- end -}}
301+
302+
{{- define "sidebar-item-link" -}}
303+
{{- $external := strings.HasPrefix .link "http" -}}
304+
{{- $open := .open | default true -}}
305+
{{- $hasChildren := false -}}
306+
{{- $linkClass := "hx:flex hx:items-center hx:justify-between hx:gap-2 hx:grow hx:cursor-pointer hx:rounded-sm hx:px-2 hx:py-1.5 hx:text-sm hx:transition-colors [-webkit-tap-highlight-color:transparent] [-webkit-touch-callout:none] hx:hextra-focus-visible-inset" -}}
307+
{{- with .context }}{{ if or .RegularPages .Sections }}{{ $hasChildren = true }}{{ end }}{{ end -}}
308+
{{- if $hasChildren -}}
309+
{{- $linkClass = printf "%s hx:ltr:pr-8 hx:rtl:pl-8" $linkClass -}}
310+
{{- end -}}
311+
{{- if .active -}}
312+
{{- $linkClass = printf "%s hextra-sidebar-active-item hx:bg-primary-100 hx:font-semibold hx:text-primary-800 hx:contrast-more:border hx:contrast-more:border-primary-500 hx:dark:bg-primary-400/10 hx:dark:text-primary-600 hx:contrast-more:dark:border-primary-500" $linkClass -}}
313+
{{- else -}}
314+
{{- $linkClass = printf "%s hx:text-gray-500 hx:hover:bg-gray-100 hx:hover:text-gray-900 hx:contrast-more:border hx:contrast-more:border-transparent hx:contrast-more:text-gray-900 hx:contrast-more:hover:border-gray-900 hx:dark:text-neutral-400 hx:dark:hover:bg-primary-100/5 hx:dark:hover:text-gray-50 hx:contrast-more:dark:text-gray-50 hx:contrast-more:dark:hover:border-gray-50" $linkClass -}}
315+
{{- end -}}
316+
<div class="hextra-sidebar-item hx:group hx:relative hx:flex hx:items-center" data-active="{{ if .active }}true{{ else }}false{{ end }}">
317+
<a
318+
class="{{ $linkClass }}"
319+
href="{{ .link }}"
320+
{{ if $external }}target="_blank" rel="noreferrer"{{ end }}
321+
>
322+
<span class="hx:min-w-0 [word-break:break-word]">{{- .title -}}</span>
323+
</a>
324+
{{- if $hasChildren }}
325+
<button type="button" class="hextra-sidebar-collapsible-button hx:absolute hx:top-1/2 hx:-translate-y-1/2 hx:ltr:right-2 hx:rtl:left-2 hx:shrink-0 hx:cursor-pointer hx:p-0 hx:text-gray-500 hx:dark:text-neutral-400 hx:group-hover:text-gray-900 hx:dark:group-hover:text-gray-50 hx:group-data-[active=true]:text-primary-800 hx:group-data-[active=true]:dark:text-primary-600 hx:hextra-focus-visible-inset" aria-label="{{ (T "toggleSection") | default "Toggle section" }}" aria-expanded="{{ if $open }}true{{ else }}false{{ end }}">
326+
{{- template "sidebar-collapsible-button" -}}
327+
</button>
328+
{{- end }}
329+
</div>
330+
{{- end -}}
331+
332+
{{- define "sidebar-collapsible-button" -}}
333+
<svg fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true" focusable="false" class="hx:h-[18px] hx:min-w-[18px] hx:rounded-xs hx:p-0.5 hx:hover:bg-gray-800/5 hx:dark:hover:bg-gray-100/5"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7" class="hx:origin-center hx:transition-transform hx:rtl:-rotate-180"></path></svg>
334+
{{- end -}}

0 commit comments

Comments
 (0)