Improve hugo menu handling, add active class to current menu entity, name now takes over page title

This commit is contained in:
Alex Shpak
2025-09-21 22:22:19 +02:00
parent d08e17457f
commit 79001ff6f3
7 changed files with 49 additions and 28 deletions
-1
View File
@@ -1,7 +1,6 @@
--- ---
menu: menu:
after: after:
name: blog
weight: 5 weight: 5
title: Blog title: Blog
--- ---
+10
View File
@@ -0,0 +1,10 @@
---
title: Showcases
layout: landing
---
<div class="book-hero text-center">
# At the moment this page is empty
<div>
+4 -5
View File
@@ -42,24 +42,23 @@ enableGitInfo = true
[menu] [menu]
[[menu.home]] [[menu.home]]
name = "Documentation" name = "Documentation"
url = "/docs/example/" pageRef = "/docs/example/"
weight = 1 weight = 1
[[menu.home]] [[menu.home]]
name = "Introduction" pageRef = "/docs/example/"
url = "/docs/example/"
weight = 10 weight = 10
parent = "Documentation" parent = "Documentation"
[[menu.home]] [[menu.home]]
name = "Shortcodes" name = "Shortcodes"
url = "/docs/shortcodes/badges/" pageRef = "/docs/shortcodes/badges/"
weight = 20 weight = 20
parent = "Documentation" parent = "Documentation"
[[menu.home]] [[menu.home]]
name = "Showcases" name = "Showcases"
url = "/showcases/" pageRef = "/showcases/"
weight = 2 weight = 2
[[menu.home]] [[menu.home]]
+4 -5
View File
@@ -40,18 +40,17 @@ languages:
menu: menu:
home: home:
- name: "Documentation" - name: "Documentation"
url: "/docs/example/" pageRef: "/docs/example/"
weight: 1 weight: 1
- name: "Introduction" - pageRef: "/docs/example/"
url: "/docs/example/"
weight: 10 weight: 10
parent: "Documentation" parent: "Documentation"
- name: "Shortcodes" - name: "Shortcodes"
url: "/docs/shortcodes/badges/" pageRef: "/docs/shortcodes/badges/"
weight: 20 weight: 20
parent: "Documentation" parent: "Documentation"
- name: "Showcases" - name: "Showcases"
url: "/showcases/" pageRef: "/showcases/"
weight: 2 weight: 2
- name: "Github" - name: "Github"
url: "https://github.com/alex-shpak/hugo-book/" url: "https://github.com/alex-shpak/hugo-book/"
+28 -14
View File
@@ -1,29 +1,43 @@
<!-- <!--
This is template for hugo menus, accepts MenuEntity as context This is template for hugo menus, accepts Page and Menu as context
https://gohugo.io/variables/menus/ https://gohugo.io/variables/menus/
--> -->
{{ if . }} {{- if .Menu -}}
{{ template "book-menu-hugo" . }} {{- template "book-menu-hugo" . -}}
{{ end }} {{- end -}}
{{ define "book-menu-hugo" }} {{ define "book-menu-hugo" }}
<ul> <ul>
{{ range . }} {{- range .Menu -}}
{{- $class := slice -}}
{{- with .Params.class -}}
{{- $class = $class | append . -}}
{{- end -}}
{{- if $.Page.IsMenuCurrent .Menu . -}}
{{- $class = $class | append "active" -}}
{{- end -}}
<li> <li>
{{ $isRemote := not (or (.Page) (strings.HasPrefix .URL "/")) }} {{- $isRemote := not (or (.Page) (strings.HasPrefix .URL "/")) -}}
<a href="{{ .URL }}" {{ with .Params.class }}class="{{ . }}"{{ end }} {{ if $isRemote }}target="_blank" rel="noopener"{{ end }}> <a href="{{ .URL }}"
{{- with $class }}class="{{ delimit . " " }}"{{ end -}}
{{- with .Title }}title="{{ . }}"{{ end -}}
{{- if $isRemote }}target="_blank" rel="noopener"{{ end -}}>
{{- .Pre -}} {{- .Pre -}}
{{ with .Page }} {{ template "book-menu-title" . }}
{{ partial "docs/title" .Page }}
{{ else }}
{{ .Name }}
{{ end }}
{{- .Post -}} {{- .Post -}}
</a> </a>
{{- with .Children }} {{- with .Children }}
{{ template "book-menu-hugo" . }} {{ template "book-menu-hugo" (dict "Page" $.Page "Menu" .) }}
{{- end }} {{- end }}
</li> </li>
{{ end }} {{- end -}}
</ul> </ul>
{{ end }} {{ end }}
{{ define "book-menu-title" }}
{{ if .Name }}
{{ .Name }}
{{ else if .Page }}
{{ partial "docs/title" .Page }}
{{ end }}
{{ end }}
+2 -2
View File
@@ -6,11 +6,11 @@
{{ end }} {{ end }}
{{ partial "docs/inject/menu-before" . }} {{ partial "docs/inject/menu-before" . }}
{{ partial "docs/menu-hugo" .Site.Menus.before }} {{ partial "docs/menu-hugo" (dict "Page" .Page "Menu" .Site.Menus.before) }}
{{ partial "docs/menu-filetree" . }} {{ partial "docs/menu-filetree" . }}
{{ partial "docs/menu-hugo" .Site.Menus.after }} {{ partial "docs/menu-hugo" (dict "Page" .Page "Menu" .Site.Menus.after) }}
{{ partial "docs/inject/menu-after" . }} {{ partial "docs/inject/menu-after" . }}
</nav> </nav>
+1 -1
View File
@@ -10,6 +10,6 @@
{{ define "header" }} {{ define "header" }}
<nav> <nav>
{{ partial "docs/menu-hugo" .Site.Menus.home }} {{ partial "docs/menu-hugo" (dict "Page" .Page "Menu" .Site.Menus.home) }}
</nav> </nav>
{{ end }} {{ end }}