mirror of
https://github.com/gcushen/hugo-academic.git
synced 2025-07-26 19:15:16 +02:00
fix: two active links at the same time in navbar
Caused when `/` rather than an anchor or blank string used to represent homepage URL in a menu link. Note: further improvement possible such as by applying similar logic to non-root widget pages which have menu links to root widget page and its anchors. Fix #1812
This commit is contained in:
parent
1f6d31c93a
commit
34a16bef39
1 changed files with 19 additions and 9 deletions
|
@ -81,18 +81,28 @@
|
|||
{{ end }}
|
||||
|
||||
{{/* Get active page. */}}
|
||||
{{ $is_link_in_current_path := in $current_page.RelPermalink .URL }}
|
||||
|
||||
{{ $is_link_in_current_path := false }}
|
||||
{{ $is_widget_page := or $current_page.IsHome (eq $current_page.Type "widget_page") }}
|
||||
{{ $is_same_page := false }}
|
||||
{{ $hash := findRE "#(.+)" .URL }}
|
||||
{{ $is_same_page := $is_link_in_current_path }}
|
||||
{{ if gt (len $hash) 0 }}
|
||||
{{ $hash = index $hash 0 }}
|
||||
{{ $hash_removed := replace .URL $hash "" }}
|
||||
{{ if eq (len $hash_removed) 0 }}
|
||||
{{ $hash_removed = "/" }}{{/* Add robustness for `/#SECTION` or `#SECTION` in `menus.toml`. */}}
|
||||
|
||||
{{ if $current_page.IsHome | and (or (eq .URL "/") (eq .URL "")) }}
|
||||
{{ $is_link_in_current_path = true }}
|
||||
{{else}}
|
||||
{{ if gt (len .URL) 1 }}{{/* Ignore root URL */}}
|
||||
{{ $is_link_in_current_path = in $current_page.RelPermalink .URL }}
|
||||
{{ $is_same_page = $is_link_in_current_path }}
|
||||
{{end}}
|
||||
{{ if gt (len $hash) 0 }}
|
||||
{{ $hash = index $hash 0 }}
|
||||
{{ $hash_removed := replace .URL $hash "" }}
|
||||
{{ if eq (len $hash_removed) 0 }}
|
||||
{{ $hash_removed = "/" }}{{/* Add robustness for `/#SECTION` or `#SECTION` in `menus.toml`. */}}
|
||||
{{ end }}
|
||||
{{ $is_same_page = eq (path.Dir $current_page.RelPermalink) (path.Dir ($hash_removed|relLangURL)) }}
|
||||
{{ end }}
|
||||
{{ $is_same_page = eq (path.Dir $current_page.RelPermalink) (path.Dir ($hash_removed|relLangURL)) }}
|
||||
{{ end }}
|
||||
{{end}}
|
||||
|
||||
<li class="nav-item">
|
||||
<a class="nav-link {{if and $highlight_active_link $is_link_in_current_path }} active{{end}}" href="{{.URL | relLangURL}}"{{ if and $is_widget_page $is_same_page }} data-target="{{$hash}}"{{ end }}{{ ($.Scratch.Get "target") | safeHTMLAttr }}>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue