Skip to content

Commit 581dd5e

Browse files
[Feat] version picker keeps same page when exists
1 parent 03d1b5d commit 581dd5e

1 file changed

Lines changed: 22 additions & 6 deletions

File tree

layouts/_partials/version-picker.html

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,20 @@
1616

1717
{{- $changeLanguage := (T "changeVersion") | default "Change version" -}}
1818

19+
{{- /* Rhize: work out the current page's path within its version tree, so
20+
switching versions can try to land on the same page in the target
21+
version instead of always going to that version's homepage. */ -}}
22+
{{- $pageURL := page.RelPermalink -}}
23+
{{- $pathSegments := strings.Split (strings.Trim $pageURL "/") "/" -}}
24+
{{- $relPath := "" -}}
25+
{{- if and (ge (len $pathSegments) 2) (eq (index $pathSegments 0) "versions") -}}
26+
{{- $relPath = delimit (after 2 $pathSegments) "/" -}}
27+
{{- else if and (ge (len $pathSegments) 1) (eq (index $pathSegments 0) "latest") -}}
28+
{{- $relPath = delimit (after 1 $pathSegments) "/" -}}
29+
{{- else -}}
30+
{{- $relPath = delimit $pathSegments "/" -}}
31+
{{- end -}}
32+
1933
<div class="hx:flex hx:justify-items-start {{ if $grow }}hx:grow{{ end }}">
2034
<button
2135
title="{{ $changeLanguage }}"
@@ -36,14 +50,16 @@
3650
style="position: fixed; inset: auto auto 0px 0px; margin: 0px; min-width: 100px;"
3751
>
3852
{{ range $versions }}
39-
{{ $link := "/" }}
53+
{{ $targetVersion := . }}
54+
{{ $isLatest := eq $targetVersion $lastVersion }}
55+
{{ $base := cond $isLatest "/latest" (printf "/versions/v%s" $targetVersion) }}
56+
{{ $href := $base }}
57+
{{ with site.GetPage (path.Join $base $relPath) }}
58+
{{ $href = .RelPermalink }}
59+
{{ end }}
4060
<li class="hx:flex hx:flex-col">
4161
<a
42-
{{ if eq . $lastVersion }}
43-
href="/"
44-
{{ else }}
45-
href="/versions/v{{ . }}"
46-
{{ end }}
62+
href="{{ $href }}"
4763
class="hx:text-gray-800 hx:dark:text-gray-100 hx:hover:bg-primary-50 hx:hover:text-primary-600 hx:hover:dark:bg-primary-500/10 hx:hover:dark:text-primary-600 hx:relative hx:cursor-pointer hx:whitespace-nowrap hx:py-1.5 hx:transition-colors hx:ltr:pl-3 hx:ltr:pr-9 hx:rtl:pr-3 hx:rtl:pl-9"
4864
>
4965
{{- . -}}

0 commit comments

Comments
 (0)