{{ $baseURL := "/" | absURL -}}

{{ $dot := . -}}
{{ $dot.Scratch.Set "path" "" -}}
{{ $dot.Scratch.Set "breadcrumb" slice -}}

{{ $url := replace .Permalink ( printf "%s" .Site.BaseURL) "" -}}
{{ $.Scratch.Add "path" .Site.BaseURL -}}

{{ $.Scratch.Add "breadcrumb" (slice (dict "url" .Site.BaseURL "name" "home" "position" 1 )) -}}
  {{ range $index, $element := split $url "/" -}}
    {{ $dot.Scratch.Add "path" $element -}}
    {{ $.Scratch.Add "path" "/" -}}
    {{ if ne $element "" -}}
    {{ $.Scratch.Add "breadcrumb" (slice (dict "url" ($.Scratch.Get "path") "name" . "position" (add $index 2))) -}}
  {{ end -}}
{{ end -}}

{{ $images := $.Resources.ByType "image" -}}
{{ $feature := $images.GetMatch "*feature*" -}}
{{ $feature_param := $.Params.feature_image  }}
{{ $feature_frontmatter := $images.GetMatch $feature_param }}
{{ if $feature_frontmatter -}}
  {{ $.Scratch.Set "primaryImage" $feature_frontmatter.Permalink }}
  {{ with $.Params.feature_image_alt }}
    {{ $.Scratch.Set "primaryImageAlt" . }}
  {{ end }}
{{ else if $feature -}}
  {{ $.Scratch.Set "primaryImage" $feature.Permalink }}
  {{ with $.Params.feature_image_alt }}
    {{ $.Scratch.Set "primaryImageAlt" . }}
  {{ end }}
{{ else if $.Params.images }}
  {{ $.Scratch.Set "primaryImage" ( index $.Params.images 0 | absURL ) }}
{{ else if $.Site.Params.images }}
  {{ $.Scratch.Set "primaryImage" ( index $.Site.Params.images 0 | absURL ) }}
{{ end }}

<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@graph": [
    {
      "@type": "Person",
      "@id": {{ print $baseURL "#/schema/person/1" }},
      "name": {{ .Site.Params.schemaName }},
      "url": {{ print $baseURL }},
      "image": {
        "@type": "ImageObject",
        "@id": {{ print $baseURL "#/schema/image/1"}},
        "url": {{ print $baseURL .Site.Params.schemaImage }},
        "width": {{- .Site.Params.schemaImageWidth -}},
        "height": {{- .Site.Params.schemaImageHeight -}},
        "caption": {{ .Site.Params.schemaName }}
      }
    },
    {
      "@type": "WebSite",
      "@id": {{ print $baseURL "#/schema/website/1" }},
      "url": {{ print $baseURL }},
      "name": {{ .Site.Title }},
      "description": {{ .Site.Params.description }},
      "publisher": {
        "@id": {{ print $baseURL "#/schema/person/1" }}
      }
    },
    {
      {{ if and (ne .Kind "taxonomy") (ne .Kind "term") -}}
        "@type": "WebPage",
      {{ else -}}
        "@type": "CollectionPage",
      {{ end -}}
      "@id": {{ .Permalink }},
      "url": {{ .Permalink }},
      "name": {{ with .Title }}{{ . }}{{ else }}{{ .Site.Title }}{{ end }},
      "description": {{ with .Description }}{{ . }}{{ else }}{{ .Site.Params.description }}{{ end }},
      "isPartOf": {
        "@id": {{ print $baseURL "#/schema/website/1" }}
      },
      "about": {
        "@id": {{ print $baseURL "#/schema/person/1" }}
      },
      "datePublished": {{ with .PublishDate}}{{ .Format "2006-01-02T15:04:05-07:00" }}{{ else }}{{ .Date.Format "2006-01-02T15:04:05-07:00" }}{{ end }},
      "dateModified": {{ .Lastmod.Format "2006-01-02T15:04:05-07:00" }},
      "breadcrumb": {
        "@id": {{ print .Permalink "#/schema/breadcrumb/1" }}
      },
      "primaryImageOfPage": {
        "@id": {{ print .Permalink "#/schema/image/2" }}
      },
      "inLanguage": {{ .Site.Params.schemaLocale }},
      "potentialAction": [{
        "@type": "ReadAction", "target": [{{ .Permalink }}]
      }]
    },
    {
      "@type": "BreadcrumbList",
      "@id": {{ print .Permalink "#/schema/breadcrumb/1" }},
      "name": "Breadcrumbs",
      "itemListElement": [{{ $list := $.Scratch.Get "breadcrumb" }}{{ $len := (len $list) }}{{ range $index, $element := $list }}{{ if ne .position 1 }},{{ end }}{
        "@type": "ListItem",
        "position": {{ .position }},
        "item": {
          {{ if ne (add $index 1) $len -}}
          "@type": "WebPage",
          "@id": {{ .url }},
          "url": {{ .url }},
          "name": {{ .name | humanize | title }}
          {{ else -}}
          "@id": {{ .url }}
          {{ end -}}
        }
        }{{ end }}]
    },
    {{ if and (eq .Kind "page") (eq .Section "posts") -}}
    {
      "@context": "https://schema.org",
      "@graph": [
        {
          "@type": "Article",
          "@id": {{ print $baseURL "#/schema/article/1" }},
          "headline": {{ .Title }},
          "description": {{ .Description }},
          "isPartOf": {
            "@id": {{ .Permalink }}
          },
          "mainEntityOfPage": {
            "@id": {{ .Permalink }}
          },
          "datePublished": {{ .PublishDate.Format "2006-01-02T15:04:05-07:00" }},
          "dateModified": {{ .Lastmod.Format "2006-01-02T15:04:05-07:00" }},
          "author": {
            "@id": {{ print $baseURL "#/schema/person/1" }}
          },          
          "publisher": {
            "@id": {{ print $baseURL "#/schema/person/1" }}
          },
          "image": {
            "@id": {{ print .Permalink "#/schema/image/2" }}
          }
        }
      ]
    },
    {{- end -}}
    {
      "@context": "https://schema.org",
      "@graph": [
        {
          "@type": "ImageObject",
          "@id": {{ print .Permalink "#/schema/image/2" }},
          "url": {{ $.Scratch.Get "primaryImage" }},
          "contentUrl": {{ $.Scratch.Get "primaryImage" }},
          "caption": {{ with $.Scratch.Get "primaryImageAlt" }}{{ . }}{{ else }}{{ .Title }}{{ end }}
        }
      ]
    }
  ]
}
</script>