{{- /* SEO & Social Media Meta Tags */ -}}

{{- $shortTitle := site.Params.shortTitle | default site.Title -}}
{{- $baseTitle := .Title -}}
{{- if hasPrefix .Title "http" -}}{{- $baseTitle = cond .Parent .Parent.Title site.Title -}}{{- end -}}
{{- $pagerSuffix := "" -}}
{{- if and (not .IsPage) .Paginator (gt .Paginator.PageNumber 1) -}}
  {{- $pagerSuffix = cond (eq .Language.Lang "zh") (printf " - 第 %d 页" .Paginator.PageNumber) (printf " - Page %d" .Paginator.PageNumber) -}}
{{- end -}}
{{- $titleCore := cond (gt (len $baseTitle) 50) (printf "%s…" (substr $baseTitle 0 50)) $baseTitle -}}
{{- $title := cond .IsHome site.Title (printf "%s%s | %s" $titleCore $pagerSuffix $shortTitle) -}}
{{- $taxonomyDesc := "" -}}
{{- if eq .Kind "taxonomy" -}}
  {{- $taxonomyDesc = cond (eq .Language.Lang "zh") "探索 cubxxw 博客的所有话题。" (printf "Browse all topics on cubxxw blog.") -}}
{{- else if eq .Kind "term" -}}
  {{- $taxonomyDesc = cond (eq .Language.Lang "zh") (printf "浏览 %d 篇关于 %s 的文章。" (len .Pages) .Title) (printf "Browse %d articles tagged with %s." (len .Pages) .Title) -}}
{{- end -}}
{{- $description := cond (ne $taxonomyDesc "") $taxonomyDesc (cond .IsHome site.Params.description (cond .Description .Description .Summary | plainify | truncate 160)) -}}
{{- $keywords := cond .IsHome site.Params.keywords (cond .Params.keywords .Params.keywords (slice)) -}}
{{- $author := cond .IsHome site.Params.author (cond .Params.author .Params.author site.Params.author) -}}

{{- /* Canonical URL */ -}}
<link rel="canonical" href="{{ if .Params.canonicalURL -}} {{ trim .Params.canonicalURL " " }} {{- else -}} {{ .Permalink }} {{- end }}" />

{{- /* Basic SEO */ -}}
<meta name="description" content="{{ $description }}" />
<meta name="keywords" content="{{ delimit $keywords ", " }}" />
<meta name="author" content="{{ $author }}" />
{{- if .Params.date -}}
<meta name="date" content="{{ .Params.date.Format "2006-01-02" }}" />
{{- end -}}

{{- /* Open Graph / Facebook */ -}}
{{- $ogType := cond (or .IsHome (eq .Kind "taxonomy") (eq .Kind "term") (eq .Kind "section")) "website" "article" -}}
<meta property="og:type" content="{{ $ogType }}" />
<meta property="og:site_name" content="{{ site.Title }}" />
{{- $ogLocale := cond (eq .Language.Lang "zh") "zh_CN" "en_US" -}}
<meta property="og:locale" content="{{ $ogLocale }}" />
{{- range .AllTranslations -}}
{{- if ne .Language.Lang $.Language.Lang -}}
<meta property="og:locale:alternate" content="{{ cond (eq .Language.Lang "zh") "zh_CN" "en_US" }}" />
{{- end -}}
{{- end -}}
<meta property="og:url" content="{{ .Permalink }}" />
<meta property="og:title" content="{{ $title | plainify }}" />
<meta property="og:description" content="{{ $description | plainify }}" />
{{- $sectionDefault := dict "ai-technology" "/assets/og-ai-technology.png" "growth" "/assets/og-growth.png" "projects" "/assets/og-projects.png" -}}
{{- $defaultOgImage := index (site.Params.images | default (slice "/assets/og-image.png")) 0 -}}
{{- $sectionOgImage := index $sectionDefault .Section | default $defaultOgImage -}}
{{- $ogImage := $sectionOgImage -}}
{{- with .Params.cover.image -}}{{- $ogImage = . -}}{{- end -}}
<meta property="og:image" content="{{ $ogImage | absURL }}" />
<meta property="og:image:alt" content="{{ with .Params.cover.alt }}{{ . }}{{ else }}{{ .Title }}{{ end }}" />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
{{- if .Params.date -}}
<meta property="article:published_time" content="{{ .Params.date.Format "2006-01-02T15:04:05Z07:00" }}" />
{{- end -}}
{{- if .Lastmod -}}
<meta property="article:modified_time" content="{{ .Lastmod.Format "2006-01-02T15:04:05Z07:00" }}" />
{{- end -}}
{{- with .Params.tags -}}
{{- range first 5 . -}}
<meta property="article:tag" content="{{ . | plainify }}" />
{{- end -}}
{{- end -}}
{{- $cats := .Params.categories -}}
{{- if $cats -}}
{{- if not (reflect.IsSlice $cats) -}}{{- $cats = slice $cats -}}{{- end -}}
{{- range first 3 $cats -}}
<meta property="article:section" content="{{ . | plainify }}" />
{{- end -}}
{{- end -}}

{{- /* Twitter Card */ -}}
<meta name="twitter:card" content="summary_large_image" />
<meta name="twitter:url" content="{{ .Permalink }}" />
<meta name="twitter:title" content="{{ $title | plainify }}" />
<meta name="twitter:description" content="{{ $description | plainify }}" />
<meta name="twitter:image" content="{{ $ogImage | absURL }}" />
<meta name="twitter:image:alt" content="{{ with .Params.cover.alt }}{{ . }}{{ else }}{{ .Title }}{{ end }}" />
{{- with site.Params.social.twitter -}}
<meta name="twitter:site" content="@{{ . }}" />
<meta name="twitter:creator" content="@{{ . }}" />
{{- end -}}

{{- /* Favicons are emitted once by head.html (with defaults) — do not
       duplicate them here. */ -}}

{{- /* RSS Feed */ -}}
{{- range .AlternativeOutputFormats -}}
{{- if eq .Rel "alternate" -}}
<link rel="alternate" type="{{ .MediaType.Type }}" href="{{ .Permalink }}" title="{{ site.Title }}" />
{{- end -}}
{{- end -}}

{{- /* Structured Data / JSON-LD */ -}}
{{- partial "seo/structured-data.html" . -}}

{{- /* Preconnect/DNS hints are consolidated in extend_head.html */ -}}
