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:
after:
name: blog
weight: 5
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.home]]
name = "Documentation"
url = "/docs/example/"
pageRef = "/docs/example/"
weight = 1
[[menu.home]]
name = "Introduction"
url = "/docs/example/"
pageRef = "/docs/example/"
weight = 10
parent = "Documentation"
[[menu.home]]
name = "Shortcodes"
url = "/docs/shortcodes/badges/"
pageRef = "/docs/shortcodes/badges/"
weight = 20
parent = "Documentation"
[[menu.home]]
name = "Showcases"
url = "/showcases/"
pageRef = "/showcases/"
weight = 2
[[menu.home]]
+4 -5
View File
@@ -40,18 +40,17 @@ languages:
menu:
home:
- name: "Documentation"
url: "/docs/example/"
pageRef: "/docs/example/"
weight: 1
- name: "Introduction"
url: "/docs/example/"
- pageRef: "/docs/example/"
weight: 10
parent: "Documentation"
- name: "Shortcodes"
url: "/docs/shortcodes/badges/"
pageRef: "/docs/shortcodes/badges/"
weight: 20
parent: "Documentation"
- name: "Showcases"
url: "/showcases/"
pageRef: "/showcases/"
weight: 2
- name: "Github"
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/
-->
{{ if . }}
{{ template "book-menu-hugo" . }}
{{ end }}
{{- if .Menu -}}
{{- template "book-menu-hugo" . -}}
{{- end -}}
{{ define "book-menu-hugo" }}
<ul>
{{ range . }}
{{- range .Menu -}}
{{- $class := slice -}}
{{- with .Params.class -}}
{{- $class = $class | append . -}}
{{- end -}}
{{- if $.Page.IsMenuCurrent .Menu . -}}
{{- $class = $class | append "active" -}}
{{- end -}}
<li>
{{ $isRemote := not (or (.Page) (strings.HasPrefix .URL "/")) }}
<a href="{{ .URL }}" {{ with .Params.class }}class="{{ . }}"{{ end }} {{ if $isRemote }}target="_blank" rel="noopener"{{ end }}>
{{- $isRemote := not (or (.Page) (strings.HasPrefix .URL "/")) -}}
<a href="{{ .URL }}"
{{- with $class }}class="{{ delimit . " " }}"{{ end -}}
{{- with .Title }}title="{{ . }}"{{ end -}}
{{- if $isRemote }}target="_blank" rel="noopener"{{ end -}}>
{{- .Pre -}}
{{ with .Page }}
{{ partial "docs/title" .Page }}
{{ else }}
{{ .Name }}
{{ end }}
{{ template "book-menu-title" . }}
{{- .Post -}}
</a>
{{- with .Children }}
{{ template "book-menu-hugo" . }}
{{ template "book-menu-hugo" (dict "Page" $.Page "Menu" .) }}
{{- end }}
</li>
{{ end }}
{{- end -}}
</ul>
{{ 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 }}
{{ 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-hugo" .Site.Menus.after }}
{{ partial "docs/menu-hugo" (dict "Page" .Page "Menu" .Site.Menus.after) }}
{{ partial "docs/inject/menu-after" . }}
</nav>
+1 -1
View File
@@ -10,6 +10,6 @@
{{ define "header" }}
<nav>
{{ partial "docs/menu-hugo" .Site.Menus.home }}
{{ partial "docs/menu-hugo" (dict "Page" .Page "Menu" .Site.Menus.home) }}
</nav>
{{ end }}