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") -}}
<nav class="breadcrumbs" role="navigation" aria-label="Breadcrumb">
<a href='{{ "" | absLangURL }}'>
{{- i18n "home" | default "Home" -}}
</a>
{{- $url := replace .Parent.Permalink (printf "%s" site.Home.Permalink) "" }}
{{- $lang_url := strings.TrimPrefix (printf "%s/" .Lang) $url -}}
{{- $scratch := newScratch }}
{{- range $index, $element := split $lang_url "/" }}
{{- $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">
{{- $ancestors := .Ancestors.Reverse -}}
{{- range $i, $bc := $ancestors }}
{{- if eq $i 0 }}
<a href="{{ $bc.RelPermalink }}">{{ i18n "home" | default "Home" }}</a>
{{- else }}
<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>
</svg>
<a href="{{ $bc_pg.Permalink }}">
{{- $bc_pg.Name -}}
</a>
<a href="{{ $bc.RelPermalink }}">{{ $bc.Title }}</a>
{{- end }}
{{- end -}}
{{- end }}
</nav>
{{- end -}}