From 34a16bef390a4a05b20cfe614ee14db304519ebd Mon Sep 17 00:00:00 2001 From: George Cushen Date: Sun, 23 Aug 2020 19:54:28 +0100 Subject: [PATCH] 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 --- layouts/partials/navbar.html | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/layouts/partials/navbar.html b/layouts/partials/navbar.html index 56d44197..1d835247 100644 --- a/layouts/partials/navbar.html +++ b/layouts/partials/navbar.html @@ -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}}