Handle cover image URL in partials when image is a resource

This commit is contained in:
Nick Douma 2023-08-09 12:02:51 +02:00
parent 69cec7a0ba
commit c66978f721
3 changed files with 16 additions and 0 deletions

View File

@ -3,11 +3,16 @@
<meta property="og:type" content="{{ if .IsPage }}article{{ else }}website{{ end }}" /> <meta property="og:type" content="{{ if .IsPage }}article{{ else }}website{{ end }}" />
<meta property="og:url" content="{{ .Permalink }}" /> <meta property="og:url" content="{{ .Permalink }}" />
{{- if .Params.cover.image -}} {{- if .Params.cover.image -}}
{{- $cover := (.Resources.ByType "image").GetMatch (printf "*%s*" (.Params.cover.image)) }}
{{- if $cover -}}{{/* i.e it is present in page bundle */}}
<meta property="og:image" content="{{ $cover.Permalink }}" />
{{- else }}{{/* For absolute urls and external links */}}
{{- if (ne .Params.cover.relative true) }} {{- if (ne .Params.cover.relative true) }}
<meta property="og:image" content="{{ .Params.cover.image | absURL }}" /> <meta property="og:image" content="{{ .Params.cover.image | absURL }}" />
{{- else}} {{- else}}
<meta property="og:image" content="{{ (path.Join .RelPermalink .Params.cover.image ) | absURL }}" /> <meta property="og:image" content="{{ (path.Join .RelPermalink .Params.cover.image ) | absURL }}" />
{{- end}} {{- end}}
{{- end}}
{{- else }} {{- else }}
{{- with $.Params.images -}} {{- with $.Params.images -}}

View File

@ -74,6 +74,10 @@
"wordCount" : "{{ .WordCount }}", "wordCount" : "{{ .WordCount }}",
"inLanguage": {{ .Language.Lang | default "en-us" }}, "inLanguage": {{ .Language.Lang | default "en-us" }},
{{ if .Params.cover.image -}} {{ if .Params.cover.image -}}
{{- $cover := (.Resources.ByType "image").GetMatch (printf "*%s*" (.Params.cover.image)) }}
{{- if $cover -}}{{/* i.e it is present in page bundle */}}
"image": "{{ $cover.Permalink }}",
{{- else }}{{/* For absolute urls and external links */}}
"image": "image":
{{- if (ne .Params.cover.relative true) -}} {{- if (ne .Params.cover.relative true) -}}
{{ .Params.cover.image | absURL }}, {{ .Params.cover.image | absURL }},
@ -81,6 +85,7 @@
{{ (path.Join .RelPermalink .Params.cover.image ) | absURL }}, {{ (path.Join .RelPermalink .Params.cover.image ) | absURL }},
{{- end}} {{- end}}
{{- end -}} {{- end -}}
{{- end -}}
"datePublished": {{ .PublishDate }}, "datePublished": {{ .PublishDate }},
"dateModified": {{ .Lastmod }}, "dateModified": {{ .Lastmod }},
{{- with (.Params.author | default site.Params.author) }} {{- with (.Params.author | default site.Params.author) }}

View File

@ -1,10 +1,16 @@
{{- if .Params.cover.image -}} {{- if .Params.cover.image -}}
{{- $cover := (.Resources.ByType "image").GetMatch (printf "*%s*" (.Params.cover.image)) }}
{{- if $cover -}}{{/* i.e it is present in page bundle */}}
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="{{ $cover.Permalink }}"/>
{{- else }}{{/* For absolute urls and external links */}}
<meta name="twitter:card" content="summary_large_image" /> <meta name="twitter:card" content="summary_large_image" />
{{- if (ne $.Params.cover.relative true) }} {{- if (ne $.Params.cover.relative true) }}
<meta name="twitter:image" content="{{ .Params.cover.image | absURL }}" /> <meta name="twitter:image" content="{{ .Params.cover.image | absURL }}" />
{{- else }} {{- else }}
<meta name="twitter:image" content="{{ (path.Join .RelPermalink .Params.cover.image ) | absURL }}" /> <meta name="twitter:image" content="{{ (path.Join .RelPermalink .Params.cover.image ) | absURL }}" />
{{- end}} {{- end}}
{{- end -}}
{{- else }} {{- else }}
{{- with $.Params.images -}} {{- with $.Params.images -}}
<meta name="twitter:card" content="summary_large_image"/> <meta name="twitter:card" content="summary_large_image"/>