Refactor breadcrumb

This commit is contained in:
mark
2023-12-25 14:45:30 +01:00
parent 47ca9d303a
commit 134e544718
8 changed files with 64 additions and 12 deletions

View File

@@ -0,0 +1,16 @@
comment: Displays a breadcrumb for a specific page.
arguments:
page:
type:
- '*hugolib.pageState'
- '*hugolib.pageForShortcode'
optional: false
comment: Page to display the breadcrumb for.
group: partial
path:
type: string
optional: true
comment: Path of the page to display the breadcrumb for.
group: shortcode

View File

@@ -11,7 +11,7 @@
<div class="container-xxl p-4 px-xxl-0"> <div class="container-xxl p-4 px-xxl-0">
{{- if ne $layout "featured" -}} {{- if ne $layout "featured" -}}
{{ if and (not $page.IsHome) site.Params.navigation.breadcrumb }} {{ if and (not $page.IsHome) site.Params.navigation.breadcrumb }}
<div>{{ partial "assets/breadcrumb.html" $page }}</div> <div>{{ partial "assets/breadcrumb.html" (dict "page" $page) }}</div>
{{ end -}} {{ end -}}
{{- end -}} {{- end -}}

View File

@@ -1,6 +1,6 @@
{{- define "partials/single-main.html" -}} {{- define "partials/single-main.html" -}}
{{- $breakpoint := $.Scratch.Get "breakpoint" -}} {{- $breakpoint := $.Scratch.Get "breakpoint" -}}
{{ if .Site.Params.navigation.breadcrumb }}{{ partial "assets/breadcrumb.html" . }}{{ end -}} {{ if .Site.Params.navigation.breadcrumb }}{{ partial "assets/breadcrumb.html" (dict "page" .) }}{{ end -}}
{{ .Render "single/header" }} {{ .Render "single/header" }}

View File

@@ -1,6 +1,24 @@
<!--
Copyright © 2023 The Hinode Team / Mark Dumay. All rights reserved.
Use of this source code is governed by The MIT License (MIT) that can be found in the LICENSE file.
Visit gethinode.com/license for more details.
-->
{{ $error := false }}
<!-- Validate arguments -->
{{ if partial "utilities/IsInvalidArgs.html" (dict "structure" "breadcrumb" "args" . "group" "partial") }}
{{ errorf "Invalid arguments: %s" .Position -}}
{{ $error = true }}
{{ end }}
<!-- Initialize arguments -->
{{ $page := .page }}
<!-- Main code -->
<nav aria-label="breadcrumb"> <nav aria-label="breadcrumb">
<ol class="breadcrumb"> <ol class="breadcrumb">
{{- range $index, $item := .Page.Ancestors.Reverse -}} {{- range $index, $item := $page.Ancestors.Reverse -}}
{{- $title := .LinkTitle -}} {{- $title := .LinkTitle -}}
{{- if .IsHome }}{{ $title = T "home" }}{{ end -}} {{- if .IsHome }}{{ $title = T "home" }}{{ end -}}
{{- $address := or .RelPermalink (.Params.Redirect | relLangURL) -}} {{- $address := or .RelPermalink (.Params.Redirect | relLangURL) -}}
@@ -10,6 +28,6 @@
<li class="breadcrumb-item">{{ $title }}</li> <li class="breadcrumb-item">{{ $title }}</li>
{{ end }} {{ end }}
{{- end -}} {{- end -}}
<li class="breadcrumb-item active" aria-current="page">{{ .Page.LinkTitle }}</li> <li class="breadcrumb-item active" aria-current="page">{{ $page.LinkTitle }}</li>
</ol> </ol>
</nav> </nav>

View File

@@ -64,7 +64,7 @@
<div class="container-fluid p-0 {{ with site.Params.home.feature.color }} bg-{{ . }} bg-opacity-{{ site.Params.style.themeOpacity | default "25" | safeHTML }}{{ end }}"> <div class="container-fluid p-0 {{ with site.Params.home.feature.color }} bg-{{ . }} bg-opacity-{{ site.Params.style.themeOpacity | default "25" | safeHTML }}{{ end }}">
<div class="container-xxl p-4 px-xxl-0 {{ if site.Params.home.fullCover }} fullcover{{ end }} d-flex flex-column"> <div class="container-xxl p-4 px-xxl-0 {{ if site.Params.home.fullCover }} fullcover{{ end }} d-flex flex-column">
{{ if $breadcrumb }} {{ if $breadcrumb }}
<div>{{ partial "assets/breadcrumb.html" $page }}</div> <div>{{ partial "assets/breadcrumb.html" (dict "page" $page) }}</div>
{{ end -}} {{ end -}}
{{ if eq $orientation "stacked" }} {{ if eq $orientation "stacked" }}

View File

@@ -35,6 +35,8 @@
{{ end }} {{ end }}
{{ end }} {{ end }}
{{ $group := .group }}
{{ $namedArgs := true }} {{ $namedArgs := true }}
{{ if not $error }} {{ if not $error }}
@@ -111,7 +113,13 @@
{{ $max := len $args }} {{ $max := len $args }}
{{ $expected := 0 }} {{ $expected := 0 }}
{{ range $key, $val := $definitions }} {{ range $key, $val := $definitions }}
{{ if not $val.optional }} {{ $skip := false }}
{{ $groups := slice | append $val.group }}
{{ if and $group $val.group }}
{{ $skip = not (in $groups $group )}}
{{ end }}
{{ if and (not $skip) (not $val.optional) }}
{{ if $namedArgs }} {{ if $namedArgs }}
{{ if not (isset $args $key) }} {{ if not (isset $args $key) }}
{{ warnf "[%s] argument '%s': expected value" $structure $key }} {{ warnf "[%s] argument '%s': expected value" $structure $key }}

View File

@@ -1,11 +1,20 @@
<!-- <!--
Displays a breadcrum for a specific page. The shortcode supports the following arguments: Copyright © 2023 The Hinode Team / Mark Dumay. All rights reserved.
"path" Optional path of the page, defaults to current page. Use of this source code is governed by The MIT License (MIT) that can be found in the LICENSE file.
Visit gethinode.com/license for more details.
--> -->
{{- $error := false -}} {{ $error := false }}
<!-- Validate arguments -->
{{ if partial "utilities/IsInvalidArgs.html" (dict "structure" "breadcrumb" "args" .Params "group" "shortcode") }}
{{ errorf "Invalid arguments: %s" .Position -}}
{{ $error = true }}
{{ end }}
<!-- Initialize arguments -->
{{- $page := . -}} {{- $page := . -}}
{{- $path := .Get "path" | default "" }} {{- $path := .Get "path" }}
{{- if $path -}} {{- if $path -}}
{{- $page = .Site.GetPage $path -}} {{- $page = .Site.GetPage $path -}}
{{- if not $page -}} {{- if not $page -}}
@@ -14,6 +23,7 @@
{{- end -}} {{- end -}}
{{- end -}} {{- end -}}
<!-- Main code -->
{{- if not $error -}} {{- if not $error -}}
{{- partial "assets/breadcrumb.html" $page -}} {{- partial "assets/breadcrumb.html" (dict "page" $page) -}}
{{- end -}} {{- end -}}

View File

@@ -10,7 +10,7 @@
<div class="col col-{{ $breakpoint.prev }}-12 col-{{ $breakpoint.current }}-8"> <div class="col col-{{ $breakpoint.prev }}-12 col-{{ $breakpoint.current }}-8">
{{- if ne $layout "featured" -}} {{- if ne $layout "featured" -}}
{{ if and (not $page.IsHome) site.Params.navigation.breadcrumb }} {{ if and (not $page.IsHome) site.Params.navigation.breadcrumb }}
<div>{{ partial "assets/breadcrumb.html" $page }}</div> <div>{{ partial "assets/breadcrumb.html" (dict "page" $page) }}</div>
{{ end -}} {{ end -}}
{{- end -}} {{- end -}}