Skip to content

Instantly share code, notes, and snippets.

@apoorvmote
Last active February 28, 2021 08:56
Show Gist options
  • Save apoorvmote/39a99a665bf1ae0b9a57e4fddc201535 to your computer and use it in GitHub Desktop.
Save apoorvmote/39a99a665bf1ae0b9a57e4fddc201535 to your computer and use it in GitHub Desktop.
{{ $url := replace .Permalink (printf "%s" .Site.BaseURL) ""}}
{{ .Scratch.Add "path" .Site.BaseURL }}
{{ .Scratch.Add "breadcrumb" (slice (dict "url" .Site.BaseURL "name" "Home"))}}
{{ .Scratch.Add "permalink" .Permalink }}
{{ .Scratch.Add "title" .Title }}
{{ $pScratch := .Scratch }}
{{ range $index, $element := split $url "/" }}
{{ $pScratch.Add "path" $element }}
{{ $pScratch.Add "path" "/" }}
{{ if ne $element "" }}
{{ if eq ($pScratch.Get "path") ($pScratch.Get "permalink") }}
{{ $pScratch.Add "breadcrumb" (slice (dict "url" ($pScratch.Get "path") "name" ($pScratch.Get "title")))}}
{{ else }}
{{ $pScratch.Add "breadcrumb" (slice (dict "url" ($pScratch.Get "path") "name" (humanize .)))}}
{{ end }}
{{ end }}
{{ end }}
<script type="application/ld+json">
[{
"@context": "https://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [
{{ range $index, $breadcrumb := .Scratch.Get "breadcrumb" }}
{{ if ne $index 0 }},{{ end }}
{
"@type": "ListItem",
"position": {{ add $index 1 }},
"name": "{{ $breadcrumb.name }}",
{{ if ne $breadcrumb.url ($pScratch.Get "permalink") }}
"item": {{ printf "%s" $breadcrumb.url }},
{{ end }}
}
{{ end }}
]
}
{{ range $index, $category := .Params.categories }}
,
{
"@context": "https://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [
{
"@type": "ListItem",
"position": 1,
"name": "Home",
"item": "{{$.Site.BaseURL}}",
},
{
"@type": "ListItem",
"position": 2,
"name": "Categories",
"item": "{{$.Site.BaseURL}}categories/",
},
{
"@type": "ListItem",
"position": 3,
"name": "{{ humanize . }}",
"item": "{{$.Site.BaseURL}}categories/{{.}}",
},
{
"@type": "ListItem",
"position": 4,
"name": "{{ $pScratch.Get "title" }}",
},
]
}
{{ end }}
]
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment