|
| 1 | +<!-- |
| 2 | + This is template for hugo menus, accepts Page and Menu as context |
| 3 | + https://gohugo.io/variables/menus/ |
| 4 | +--> |
| 5 | +{{- if .Menu -}} |
| 6 | + {{- template "book-menu-hugo" . -}} |
| 7 | +{{- end -}} |
| 8 | + |
| 9 | +{{ define "book-menu-hugo" }} |
| 10 | +<ul> |
| 11 | +{{- range .Menu -}} |
| 12 | + {{- $class := slice -}} |
| 13 | + {{- with .Params.class -}} |
| 14 | + {{- $class = $class | append . -}} |
| 15 | + {{- end -}} |
| 16 | + {{- $isRemote := (urls.Parse .URL).IsAbs -}} |
| 17 | + {{- $menuURL := .URL -}} |
| 18 | + {{- if not $isRemote -}} |
| 19 | + {{- $menuURL = relURL $menuURL -}} |
| 20 | + {{- end -}} |
| 21 | + {{- $menuURL = strings.TrimSuffix "/" $menuURL -}} |
| 22 | + {{- $pageURL := strings.TrimSuffix "/" $.Page.RelPermalink -}} |
| 23 | + {{- $isCurrent := or |
| 24 | + ($.Page.IsMenuCurrent .Menu .) |
| 25 | + ($.Page.HasMenuCurrent .Menu .) |
| 26 | + (and .Page (eq $.Page.RelPermalink .Page.RelPermalink)) |
| 27 | + (eq $menuURL $pageURL) |
| 28 | + (and $.Page.IsHome (eq $menuURL "")) |
| 29 | + -}} |
| 30 | + {{- if $isCurrent -}} |
| 31 | + {{- $class = $class | append "active" -}} |
| 32 | + {{- end -}} |
| 33 | + <li> |
| 34 | + <a href="{{ .URL }}" |
| 35 | + {{- with $class }} class="{{ delimit . " " }}"{{- end -}} |
| 36 | + {{- with .Title }} title="{{ . }}"{{- end -}} |
| 37 | + {{- if $isRemote }} target="_blank" rel="noopener"{{- end -}}> |
| 38 | + {{- .Pre -}} |
| 39 | + {{- template "book-menu-title" . -}} |
| 40 | + {{- .Post -}} |
| 41 | + </a> |
| 42 | + {{- with .Children -}} |
| 43 | + {{- template "book-menu-hugo" (dict "Page" $.Page "Menu" .) -}} |
| 44 | + {{- end -}} |
| 45 | + {{- if and $isCurrent $.Page.Content -}} |
| 46 | + {{- $toc := $.Page.TableOfContents -}} |
| 47 | + {{- if and (ne .Page.Params.bookToC false) (ne $toc "<nav id=\"TableOfContents\"></nav>") -}} |
| 48 | + {{- $toc = replace $toc "id=\"TableOfContents\"" "class=\"book-menu-toc-list\"" -}} |
| 49 | + {{- $toc = replaceRE "(?s)</a>\\s*<ul>.*?</ul>" "</a>" $toc -}} |
| 50 | + <div class="book-menu-toc"> |
| 51 | + {{ $toc | safeHTML }} |
| 52 | + </div> |
| 53 | + {{- end -}} |
| 54 | + {{- end -}} |
| 55 | + </li> |
| 56 | +{{- end -}} |
| 57 | +</ul> |
| 58 | +{{ end }} |
| 59 | + |
| 60 | +{{ define "book-menu-title" }} |
| 61 | +{{- with .Params.BookIcon -}} |
| 62 | + <img src="{{ partial "docs/icon" . }}" class="book-icon" alt="{{ partial "docs/text/i18n" . }}" /> |
| 63 | +{{- end -}} |
| 64 | +{{- if .Name -}} |
| 65 | + {{- .Name -}} |
| 66 | +{{- else if .Page -}} |
| 67 | + {{- partial "docs/title" .Page -}} |
| 68 | +{{- end -}} |
| 69 | +{{ end }} |
0 commit comments