{{- /* Structured Data for SEO */ -}}

{{- $isPost := and (eq .Type "posts") (eq .Kind "page") -}}
{{- $authorName := "Xinwei Xiong" -}}
{{- with site.Params.author -}}
  {{- if reflect.IsMap . -}}
    {{- $authorName = .name | default "Xinwei Xiong" -}}
  {{- else if reflect.IsSlice . -}}
    {{- $authorName = index . 0 | default "Xinwei Xiong" -}}
  {{- else -}}
    {{- $authorName = . -}}
  {{- end -}}
{{- end -}}
{{- $siteName := site.Title -}}
{{- /* Publisher logo: schema.org wants a raster ≥112×112 — use the
       180×180 apple-touch-icon PNG, not the .ico favicon. */ -}}
{{- $logo := site.Params.assets.apple_touch_icon | default "/apple-touch-icon.png" | absURL -}}

{{- /* Article Schema */ -}}
{{- if $isPost -}}
<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "BlogPosting",
  "headline": {{ .Title }},
  "name": {{ .Title }},
  "description": {{ cond (ne .Description "") .Description .Summary | plainify | truncate 200 }},
  "image": {{ with .Params.cover.image }}{{ . | absURL }}{{ else }}{{ index site.Params.images 0 | default "/assets/og-image.png" | absURL }}{{ end }},
  "url": {{ .Permalink }},
  "datePublished": {{ .PublishDate.Format "2006-01-02T15:04:05Z07:00" }},
  "dateModified": {{ .Lastmod.Format "2006-01-02T15:04:05Z07:00" }},
  {{- $postAuthor := "Xinwei Xiong" -}}
  {{- with .Params.author -}}
    {{- if reflect.IsSlice . -}}{{- $postAuthor = index . 0 | string -}}
    {{- else if reflect.IsMap . -}}{{- $postAuthor = .name | default "Xinwei Xiong" -}}
    {{- else -}}{{- $postAuthor = . | string -}}
    {{- end -}}
  {{- end -}}
  "author": {
    "@type": "Person",
    {{- /* No jsonify: Hugo's JS-context auto-escaping already JSON-encodes
           values inside ld+json scripts; jsonify here double-encoded the
           name into "\"Xinwei Xiong\"". */}}
    "name": {{ $postAuthor | plainify }},
    "url": {{ site.BaseURL }}
  },
  "inLanguage": "{{ if eq .Language.Lang "zh" }}zh-CN{{ else }}en-US{{ end }}",
  "publisher": {
    "@type": "Organization",
    "name": {{ $siteName }},
    "logo": {
      "@type": "ImageObject",
      "url": {{ $logo }}
    }
  },
  "mainEntityOfPage": {
    "@type": "WebPage",
    "@id": {{ .Permalink }}
  },
  {{- with .Params.tags -}}
  "keywords": [{{ range $i, $kw := . }}{{ if $i }}, {{ end }}{{ $kw | plainify }}{{ end }}],
  {{- end -}}
  "articleSection": {{ cond (gt (len .Params.categories) 0) (index .Params.categories 0) "Blog" }},
  "wordCount": {{ .WordCount }}
}
</script>
{{- end -}}

{{- /* WebSite Schema */ -}}
<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "WebSite",
  "name": {{ $siteName }},
  "url": {{ site.BaseURL }},
  "description": {{ site.Params.description }},
  "inLanguage": "{{ if eq .Language.Lang "zh" }}zh-CN{{ else }}en-US{{ end }}",
  "potentialAction": {
    "@type": "SearchAction",
    "target": {
      "@type": "EntryPoint",
      "urlTemplate": "{{ site.BaseURL }}search/?s={search_term_string}"
    },
    "query-input": "required name=search_term_string"
  }
}
</script>

{{- /* Person/Author Schema */ -}}
<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "Person",
  "name": {{ $authorName | default "Xinwei Xiong" }},
  "url": {{ site.BaseURL }},
  "sameAs": [
    "https://github.com/cubxxw",
    "https://www.zhihu.com/people/3293172751",
    "https://space.bilibili.com/1233089591",
    "https://www.youtube.com/channel/UCd3qbRbMwYlh5uKneo_2m_w"
  ],
  "jobTitle": "AI Entrepreneur",
  "worksFor": {
    "@type": "Organization",
    "name": "OpenClaw"
  }
}
</script>

{{- /* BreadcrumbList Schema */ -}}
{{- if .Parent -}}
{{- $ancestors := slice -}}
{{- $p := .Parent -}}
{{- range seq 10 -}}
  {{- if $p -}}
    {{- $ancestors = $ancestors | append $p -}}
    {{- $p = $p.Parent -}}
  {{- end -}}
{{- end -}}
{{- $ancestors = collections.Reverse $ancestors -}}
<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "BreadcrumbList",
  "itemListElement": [
    {{- range $index, $ancestor := $ancestors -}}
    {
      "@type": "ListItem",
      "position": {{ add $index 1 }},
      "name": {{ $ancestor.Title }},
      "item": {{ $ancestor.Permalink }}
    },
    {{- end -}}
    {
      "@type": "ListItem",
      "position": {{ add (len $ancestors) 1 }},
      "name": {{ .Title }},
      "item": {{ .Permalink }}
    }
  ]
}
</script>
{{- end -}}
