{{- $page := .page -}}

{{- if not $page.IsHome -}}
  {{- $items := slice -}}
  {{- $position := 1 -}}

  {{- with site.Home -}}
    {{- $home_title := .LinkTitle | default site.Title | default "Home" -}}
    {{- $item := dict "@type" "ListItem" "position" $position "name" ($home_title | plainify) "item" (dict "@id" .Permalink "name" ($home_title | plainify)) -}}
    {{- $items = $items | append $item -}}
    {{- $position = add $position 1 -}}
  {{- end -}}

  {{- with $page.Ancestors -}}
    {{- range .Reverse -}}
      {{- if not .IsHome -}}
        {{- $item := dict "@type" "ListItem" "position" $position "name" (.LinkTitle | plainify) "item" (dict "@id" .Permalink "name" (.LinkTitle | plainify)) -}}
        {{- $items = $items | append $item -}}
        {{- $position = add $position 1 -}}
      {{- end -}}
    {{- end -}}
  {{- end -}}

  {{- $item := dict "@type" "ListItem" "position" $position "name" ($page.LinkTitle | default $page.Title | plainify) "item" (dict "@id" $page.Permalink "name" ($page.LinkTitle | default $page.Title | plainify)) -}}
  {{- $items = $items | append $item -}}

  {{- if gt (len $items) 1 -}}
    {{- $breadcrumb := dict "@context" "https://schema.org" "@type" "BreadcrumbList" "itemListElement" $items -}}
    <script type="application/ld+json">
    {{ $breadcrumb | jsonify (dict "indent" "  ") | safeJS }}
    </script>
  {{- end -}}
{{- end -}}
