fix(head): social image meta duplication

This commit is contained in:
ZhenShuo Leo
2025-10-01 13:44:02 +08:00
parent b3bc04cfc3
commit 374a0f51f7
+14 -10
View File
@@ -54,24 +54,28 @@
{{/* Social */}} {{/* Social */}}
{{ template "_internal/opengraph.html" . }} {{ template "_internal/opengraph.html" . }}
{{ template "_internal/twitter_cards.html" . }} {{ template "_internal/twitter_cards.html" . }}
{{/* Use defaultSocialImage if feature image does not exist */}}
{{/* Main page always uses this; fallback elsewhere if no feature image */}} {{ $featureImage := "" }}
{{/* See https://gohugo.io/templates/embedded/#open-graph */}} {{ $pageImages := .Resources.ByType "image" }}
{{ $images := .Resources.ByType "image" }} {{ range slice "*featured*" "*cover*" "*thumbnail*" }}
{{ $socialImage := $images.GetMatch "*feature*" }} {{ if not $featureImage }}
{{ if not $socialImage }} {{ $featureImage = $pageImages.GetMatch . }}
{{ end }}
{{ end }}
{{ if not $featureImage }}
{{ with .Site.Params.defaultSocialImage }} {{ with .Site.Params.defaultSocialImage }}
{{ $socialImage := "" }}
{{ if or (strings.HasPrefix . "http:") (strings.HasPrefix . "https:") }} {{ if or (strings.HasPrefix . "http:") (strings.HasPrefix . "https:") }}
{{ $socialImage = resources.GetRemote . }} {{ $socialImage = resources.GetRemote . }}
{{ else }} {{ else }}
{{ $socialImage = resources.Get . }} {{ $socialImage = resources.Get . }}
{{ end }} {{ end }}
{{ with $socialImage }}
<meta name="twitter:image" content="{{ .RelPermalink | absURL }}">
<meta property="og:image" content="{{ .RelPermalink | absURL }}">
{{ end }}
{{ end }} {{ end }}
{{ end }} {{ end }}
{{ with $socialImage }}
<meta name="twitter:image" content="{{ .RelPermalink | absURL }}">
<meta property="og:image" content="{{ .RelPermalink | absURL }}">
{{ end }}
{{/* Site Verification */}} {{/* Site Verification */}}
{{ with .Site.Params.verification.google }} {{ with .Site.Params.verification.google }}