Improve hugo menu handling, add active class to current menu entity, name now takes over page title
This commit is contained in:
@@ -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 }}
|
||||
@@ -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>
|
||||
|
||||
|
||||
@@ -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 }}
|
||||
|
||||
Reference in New Issue
Block a user