hugo-material-docs icon indicating copy to clipboard operation
hugo-material-docs copied to clipboard

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

Open lambdafu opened this issue 7 years ago • 5 comments

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 avatar Sep 27 '16 13:09 lambdafu

Fixes #31

lambdafu avatar Sep 27 '16 13:09 lambdafu

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

lambdafu avatar Sep 27 '16 15:09 lambdafu

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.

tiagoboldt avatar Jan 31 '17 02:01 tiagoboldt

@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.

stp-ip avatar May 03 '17 08:05 stp-ip

This is also being discussed in issue #70

tblom avatar Jul 22 '17 04:07 tblom