opengraph: adjust to Hugo's build-in code #824

This commit is contained in:
Sören Weber 2024-04-07 15:12:46 +02:00
parent 3c6d603254
commit 714b64ab14
No known key found for this signature in database
GPG key ID: BEC6D55545451B6D

View file

@ -1,11 +1,10 @@
{{- /* based on Hugo 0.119.0 _internal/opengraph.html but with modified title */}} {{- /* based on Hugo 0.125.0 _internal/opengraph.html but with modified title */}}
{{- $format := partial "get-format.hugo" . }} {{- $format := partial "get-format.hugo" . }}
{{- $outputFormat := partial "output-format.hugo" (dict "page" . "format" $format) }} {{- $outputFormat := partial "output-format.hugo" (dict "page" . "format" $format) }}
{{- $basename := "index" }} {{- $basename := "index" }}
{{- if eq $outputFormat "searchpage" }} {{- if eq $outputFormat "searchpage" }}
{{- $basename = partial "BaseName.hugo" $format.RelPermalink }} {{- $basename = partial "BaseName.hugo" $format.RelPermalink }}
{{- end }} {{- end }}
{{- $page := . }}
{{- $title := .Title }} {{- $title := .Title }}
{{- if eq $outputFormat "searchpage" }} {{- if eq $outputFormat "searchpage" }}
{{- $title = T "Search" }} {{- $title = T "Search" }}
@ -19,34 +18,24 @@
{{- if and $title .Site.Title (not (eq $title .Site.Title)) }} {{- if and $title .Site.Title (not (eq $title .Site.Title)) }}
{{- $title = printf "%s %s %s" $title (default "::" .Site.Params.titleSeparator) .Site.Title }} {{- $title = printf "%s %s %s" $title (default "::" .Site.Params.titleSeparator) .Site.Title }}
{{- end }} {{- end }}
<meta property="og:title" content="{{ $title }}">
<meta property="og:description" content="{{ with .Description }}{{ . }}{{ else }}{{if .IsPage}}{{ .Summary }}{{ else }}{{ with .Site.Params.description }}{{ . }}{{ end }}{{ end }}{{ end }}">
<meta property="og:type" content="{{ if .IsPage }}article{{ else }}website{{ end }}">
<meta property="og:url" content="{{ partial "relLangPrettyUglyURL.hugo" (dict "to" . "abs" true "basename" $basename) }}"> <meta property="og:url" content="{{ partial "relLangPrettyUglyURL.hugo" (dict "to" . "abs" true "basename" $basename) }}">
{{- with $.Params.images }}
{{- range first 6 . }} {{- with or site.Title site.Params.title | plainify }}
<meta property="og:image" content="{{ . | absURL }}"> <meta property="og:site_name" content="{{ . }}">
{{- end }}
{{- else }}
{{- $images := $.Resources.ByType "image" }}
{{- $featured := $images.GetMatch "*feature*" }}
{{- if not $featured }}
{{- $featured = $images.GetMatch "{*cover*,*thumbnail*}" }}
{{- end }}
{{- with $featured }}
<meta property="og:image" content="{{ $featured.Permalink }}">
{{- else }}
{{- with $.Site.Params.images }}
<meta property="og:image" content="{{ index . 0 | absURL }}">
{{- end }} {{- end }}
<meta property="og:title" content="{{ $title | plainify }}">
{{- with or .Description .Summary site.Params.description | plainify }}
<meta property="og:description" content="{{ . }}">
{{- end }} {{- end }}
{{- with or .Params.locale site.Language.LanguageCode site.Language.Lang }}
<meta property="og:locale" content="{{ . }}">
{{- end }} {{- end }}
{{- if .IsPage }} {{- if .IsPage }}
{{- $iso8601 := "2006-01-02T15:04:05-07:00" }} <meta property="og:type" content="article">
{{- with .Parent }} {{- with .FirstSection }}
{{- $page := . }}
{{- $link := "<link href=\"%s\" rel=\"%s\" type=\"%s\" title=\"%s\">" }}
{{- $title := .Title }} {{- $title := .Title }}
{{- if eq $outputFormat "searchpage" }} {{- if eq $outputFormat "searchpage" }}
{{- $title = T "Search" }} {{- $title = T "Search" }}
@ -60,48 +49,57 @@
{{- if and $title .Site.Title (not (eq $title .Site.Title)) }} {{- if and $title .Site.Title (not (eq $title .Site.Title)) }}
{{- $title = printf "%s %s %s" $title (default "::" .Site.Params.titleSeparator) .Site.Title }} {{- $title = printf "%s %s %s" $title (default "::" .Site.Params.titleSeparator) .Site.Title }}
{{- end }} {{- end }}
<meta property="article:section" content="{{ $title }}"> <meta property="article:section" content="{{ $title | plainify }}">
{{- end }} {{- end }}
{{- $iso8601 := "2006-01-02T15:04:05-07:00" }}
{{- with .PublishDate }} {{- with .PublishDate }}
<meta property="article:published_time" {{ .Format $iso8601 | printf "content=%q" | safeHTMLAttr }}> <meta property="article:published_time" {{ .Format $iso8601 | printf "content=%q" | safeHTMLAttr }}>
{{- end }} {{- end }}
{{- with .Lastmod }} {{- with .Lastmod }}
<meta property="article:modified_time" {{ .Format $iso8601 | printf "content=%q" | safeHTMLAttr }}> <meta property="article:modified_time" {{ .Format $iso8601 | printf "content=%q" | safeHTMLAttr }}>
{{- end }} {{- end }}
{{- range .GetTerms "tags" | first 6 }}
<meta property="article:tag" content="{{ .Page.Title | plainify }}">
{{- end }}
{{- else }}
<meta property="og:type" content="website">
{{- end }}
{{- with partial "_funcs/get-page-images" . }}
{{- range . | first 6 }}
<meta property="og:image" content="{{ .Permalink }}">
{{- end }}
{{- end }} {{- end }}
{{- with .Params.audio }} {{- with .Params.audio }}
<meta property="og:audio" content="{{ . }}"> {{- range . | first 6 }}
<meta property="og:audio" content="{{ . | absURL }}">
{{- end }} {{- end }}
{{- with .Params.locale }}
<meta property="og:locale" content="{{ . }}">
{{- end }}
{{- with .Site.Title }}
<meta property="og:site_name" content="{{ . }}">
{{- end }} {{- end }}
{{- with .Params.videos }} {{- with .Params.videos }}
{{- range . }} {{- range . | first 6 }}
<meta property="og:video" content="{{ . | absURL }}"> <meta property="og:video" content="{{ . | absURL }}">
{{- end }} {{- end }}
{{- end }} {{- end }}
{{- /* If it is part of a series, link to related articles */}} {{- range .GetTerms "series" }}
{{- $permalink := .Permalink }} {{- range .Pages | first 7 }}
{{- $siteSeries := .Site.Taxonomies.series }} {{- if ne $ . }}
{{- if $siteSeries }} <meta property="og:see_also" content="{{ .Permalink }}">
{{- with .Params.series }} {{- end }}
{{- range $name := . }} {{- end }}
{{- $series := index $siteSeries ($name | urlize) }} {{- end }}
{{- range $page := first 6 $series.Pages }}
{{- if ne $page.Permalink $permalink }} {{- $facebookApp := "" }}
<meta property="og:see_also" content="{{ $page.Permalink }}"> {{- with site.Params.social }}
{{ end }} {{- if reflect.IsMap . }}
{{- end }} {{- with .facebook_app_id }}
{{- $facebookApp = . }}
{{- end }} {{- end }}
{{- end }} {{- end }}
{{- end }} {{- end }}
{{- /* Deprecate site.Social.facebook_admin in favor of site.Params.social.facebook_admin */}}
{{- $facebookAdmin := "" }} {{- $facebookAdmin := "" }}
{{- with site.Params.social.facebook_admin }} {{- with site.Params.social.facebook_admin }}
{{- $facebookAdmin = . }} {{- $facebookAdmin = . }}
@ -115,7 +113,10 @@
{{- end }} {{- end }}
{{- end }} {{- end }}
{{- /* Facebook Page Admin ID for Domain Insights */}} {{- with $facebookApp }}
<meta property="fb:app_id" content="{{ . }}">
{{- else }}
{{- with $facebookAdmin }} {{- with $facebookAdmin }}
<meta property="fb:admins" content="{{ . }}"> <meta property="fb:admins" content="{{ . }}">
{{- end }} {{- end }}
{{- end }}