HugoBlox / hugo-blox-builder

🚨 GROW YOUR AUDIENCE WITH HUGOBLOX! 🚀 HugoBlox is an easy, fast no-code website builder for researchers, entrepreneurs, data scientists, and developers. Build stunning sites in minutes. 适合研究人员、企业家、数据科学家和开发者的简单快速无代码网站构建器。用拖放功能、可定制模板和内置SEO工具快速创建精美网站!
https://hugoblox.com/templates/
MIT License
8.2k stars 2.9k forks source link

Two active links at the same time in navbar #1812

Closed rchaput closed 4 years ago

rchaput commented 4 years ago

Hello,

Describe the bug

When using sections in your menu (i.e. navbar), and when the current page is different that the Home page (/), two links are active in the navbar, / and the current page (see the attached screenshot).

To Reproduce

Steps to reproduce the behavior:

  1. Use sections (not widgets) in your menu configuration (config/menus.toml), such as:
    [[main]]
    name = "Home"
    url = "/"
    [[main]]
    name = "About Me"
    url = "/aboutme/"
  2. Click on a section in the menu (other than Home), for example About Me.
  3. See that both the Home and About Me links are active and shown as such (blue link and active class on the HTML element)

image

Expected behavior

Only the current section should be marked as active, not the Home one as well.

Technical details:

  • Academic Version: 4.8.0 (commit 05bfd7c48d4b2300c53aa8610b67afacb709e280)
  • Hugo Version: Hugo Static Site Generator v0.73.0-428907CC/extended linux/amd64 BuildDate: 2020-06-23T16:40:09Z
  • Browser/OS: Firefox on Linux

I believe I have tracked down the bug to the navbar.html partial (themes/academic/layouts/partials/navbar.html), on line 84:

{{ $is_link_in_current_path := in $current_page.RelPermalink .URL }}

If I am correct, this means that we check if / (the Home URL) is in /aboutme/ (the current page relative permalink). This is true, so the active class is added. Replacing in by eq indeed solves this problem, but I imagine it brings a lot of other problems (such as not recognizing the hash with widget pages), hence I did not create a PR. I suppose we would need to first remove the (eventual) hash, and then compare the equality. The is_same_page part following this code would need to be modified as well, and I do not feel comfortable enough with the way Academic (or Hugo) works at the time present.

gcushen commented 4 years ago

It's intended to use the hash ID of your first homepage widget for the home link as per the Demo - https://github.com/gcushen/hugo-academic/blob/master/exampleSite/config/_default/menus.toml . For example, use #about if about.md is your first widget, rather than /.

The menu logic was developed in much earlier versions of Hugo so there is perhaps some more recent Hugo functionality that could be used to improve it.