refactor(breadcrumbs): simplify breadcrumb generation using ancestors method

This commit is contained in:
Aditya Telange
2026-05-03 11:46:39 +05:30
parent 87de3fb024
commit 1027c7fc62
+9 -16
View File
@@ -1,23 +1,16 @@
{{- if (.Param "ShowBreadCrumbs") -}} {{- if (.Param "ShowBreadCrumbs") -}}
<nav class="breadcrumbs" role="navigation" aria-label="Breadcrumb"> <nav class="breadcrumbs" role="navigation" aria-label="Breadcrumb">
<a href='{{ "" | absLangURL }}'> {{- $ancestors := .Ancestors.Reverse -}}
{{- i18n "home" | default "Home" -}} {{- range $i, $bc := $ancestors }}
</a> {{- if eq $i 0 }}
{{- $url := replace .Parent.Permalink (printf "%s" site.Home.Permalink) "" }} <a href="{{ $bc.RelPermalink }}">{{ i18n "home" | default "Home" }}</a>
{{- $lang_url := strings.TrimPrefix (printf "%s/" .Lang) $url -}} {{- else }}
{{- $scratch := newScratch }} <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"
{{- range $index, $element := split $lang_url "/" }} stroke-linecap="round" stroke-linejoin="round" class="feather feather-chevron-right">
{{- $scratch.Add "path" (printf "%s/" $element )}}
{{- $bc_pg := site.GetPage ($scratch.Get "path") -}}
{{- if (and ($bc_pg) (gt (len . ) 0))}}
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-chevron-right">
<polyline points="9 18 15 12 9 6"></polyline> <polyline points="9 18 15 12 9 6"></polyline>
</svg> </svg>
<a href="{{ $bc_pg.Permalink }}"> <a href="{{ $bc.RelPermalink }}">{{ $bc.Title }}</a>
{{- $bc_pg.Name -}} {{- end }}
</a>
{{- end }} {{- end }}
{{- end -}}
</nav> </nav>
{{- end -}} {{- end -}}