Skip to content
This repository has been archived by the owner on Jul 8, 2021. It is now read-only.

If a menu entry has a page, make it a link even for nested menus. #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

lambdafu
Copy link

@lambdafu lambdafu commented Sep 27, 2016

Currently, a nested menu has grey-and-nonclickable main menu entries in the nav section. This patch allows a "landing page" for such top level menu entries even with nested sections. This landing page could be the front matter of a book, for example (if each top menu entry is a book, and subsections are chapters in that book).

Hugo doesn't allow reverse lookup of urls easily, so it's a bit lame (iterating over $.Site.Pages and accessing the undocumented URLPath). Unfortunately, MenuEntry objects can not be extended either, so there is no simpler solution that I can see.

Example:

[[menu.main]]
        name   = "User's Manual"
        url    = "user"
        weight = 10

[[menu.main]]
        name   = "System Manual"
        url    = "system"
        weight = 20

And then files:

content/user/00-index.md
content/user/10-chapter1.md
content/user/20-chapter2.md
content/system/00-index.md
content/system/10-chapter1.md
content/system/20-chapter2.md

This would be the preambel of an index file (content/user/00-index.md):


---
title: User's Manual
url: user
weight: 0

---

The URL "user" here is matched against the URL in the menu entry. If they are equal, the link is added instead of just the text.

And this the preambel of a chapter:


---
title: Introduction
url: user/introduction
weight: 10
menu:
  main:
    parent: User's Manual
    identifier: Introduction
    weight: 10

---

@lambdafu
Copy link
Author

Fixes #31

@lambdafu
Copy link
Author

Here is an example illustrating this: https://github.com/lambdafu/hugo-material-book

@tiagoboldt
Copy link

I've managed to to make this change with a considerable more simplistic approach. Sharing the diff bellow.

--- themes/hugo-material-docs/layouts/partials/nav.html 2017-01-31 02:23:04.000000000 +0000
+++ layouts/partials/nav.html   2017-01-31 02:24:08.000000000 +0000
@@ -4,16 +4,14 @@

 {{ $.Scratch.Set "currentMenuEntry" . }}
 <li>
+  {{ partial "nav_link" $currentNode }}
   {{ if .HasChildren }}
-    <span class="section">{{ .Name | title }}</span>
     <ul>
       {{ range .Children }}
         {{ $.Scratch.Set "currentMenuEntry" . }}
         {{ partial "nav_link" $currentNode }}
       {{ end }}
     </ul>
-  {{ else }}
-    {{ partial "nav_link" $currentNode }}
   {{ end }}
 </li>
 {{ end }}

Basically I always write the link for the page and then add the ul if it has children.

@stp-ip
Copy link

stp-ip commented May 3, 2017

@digitalcraftsman any chance to support links working for nested items even with the lazy blogger menu approach? Currently it only links to sections. Nested items just link to the anchor.

@tblom
Copy link

tblom commented Jul 22, 2017

This is also being discussed in issue #70

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants