feat(helper/image): new way to set default image

This commit is contained in:
Jimmy Cai 2020-09-11 16:27:23 +02:00
parent cfd4cdb731
commit a2662603df
No known key found for this signature in database
GPG key ID: 3EA408E527F37B18
9 changed files with 45 additions and 16 deletions

View file

@ -37,9 +37,13 @@ DefaultContentLanguage = "en" # Theme i18n support
[params.widgets.tagCloud]
limit = 10
[params.opengraph]
defaultImage = ""
[params.opengraph.twitter]
site = ""
[params.defaultImage]
[params.defaultImage.opengraph]
enabled = false
local = false
src = ""
[menu]
[[menu.main]]

View file

@ -15,7 +15,7 @@
{{ end }}
</div>
{{ $image := partial "helper/image" . }}
{{ $image := partial "helper/image" (dict "Context" .) }}
{{ if $image.exists }}
<div class="taxonomy-image">
{{ if $image.resource }}

View file

@ -12,7 +12,7 @@
</footer>
</div>
{{ $image := partial "helper/image" . }}
{{ $image := partial "helper/image" (dict "Context" .) }}
{{ if $image.exists }}
<div class="article-image">

View file

@ -1,4 +1,4 @@
{{ $image := partial "helper/image" . }}
{{ $image := partial "helper/image" (dict "Context" .) }}
<article class="{{ if $image.exists }}has-image{{ end }}">
{{ if $image.exists }}
<div class="article-image">

View file

@ -1,4 +1,4 @@
{{ $image := partial "helper/image" .context }}
{{ $image := partial "helper/image" (dict "Context" .context) }}
<article class="{{ if $image.exists }}has-image{{ end }}">
<a href="{{ .context.Permalink }}">

View file

@ -1,4 +1,4 @@
{{ $image := partial "helper/image" . }}
{{ $image := partial "helper/image" (dict "Context" .) }}
{{ $context := . }}
<div class="article-details">
{{ with $categories := .Params.categories }}

View file

@ -1,5 +1,5 @@
<header class="article-header">
{{ $image := partial "helper/image" . }}
{{ $image := partial "helper/image" (dict "Context" .) }}
{{ if $image.exists }}
<div class="article-image">

View file

@ -40,13 +40,9 @@
{{- end -}}
{{- end -}}
{{ $image := partial "helper/image" . }}
{{ $image := partial "helper/image" (dict "Context" . "Type" "opengraph") }}
{{- if $image.exists -}}
<meta name="twitter:card" content="summary_large_image">
<meta property='og:image' content='{{ $image.permalink }}' />
<meta name="twitter:image" content='{{ $image.permalink }}' />
{{- else if .Site.Params.opengraph.defaultImage -}}
{{ $image := resources.Get .Site.Params.opengraph.defaultImage }}
<meta property='og:image' content='{{ absURL $image.RelPermalink }}' />
<meta name="twitter:image" content='{{ absURL $image.RelPermalink }}' />
<meta property='og:image' content='{{ absURL $image.permalink }}' />
<meta name="twitter:image" content='{{ absURL $image.permalink }}' />
{{- end -}}

View file

@ -1,5 +1,6 @@
{{ $result := dict "exists" false "permalink" nil "resource" nil "isDefault" false }}
{{ $imageField := .Params.image }}
{{ $imageField := .Context.Params.image }}
{{ if $imageField }}
<!-- If page has `image` field set -->
{{ $result = merge $result (dict "exists" true) }}
@ -9,7 +10,7 @@
<!-- Is a external image -->
{{ $result = merge $result (dict "permalink" $imageField) }}
{{ else }}
{{ $pageResourceImage := .Resources.GetMatch (printf "%s" ($imageField | safeURL)) }}
{{ $pageResourceImage := .Context.Resources.GetMatch (printf "%s" ($imageField | safeURL)) }}
{{ $siteResourceImage := resources.GetMatch (printf "%s" ($imageField | safeURL)) }}
{{ if $pageResourceImage }}
@ -27,6 +28,34 @@
{{ end }}
{{ end }}
{{ else if and (ne .Type nil) (index .Context.Site.Params.defaultImage .Type) }}
<!-- Type arg is set, check for defaultImage setting -->
{{ $defaultImageSetting := index .Context.Site.Params.defaultImage .Type }}
{{ if $defaultImageSetting.enabled }}
{{ $result = merge $result (dict "isDefault" true) }}
{{ $result = merge $result (dict "exists" true) }}
{{ if $defaultImageSetting.local }}
{{ $siteResourceImage := resources.GetMatch (printf "%s" ($defaultImageSetting.src | safeURL)) }}
{{ if $siteResourceImage }}
<!-- Try search image under site's assets folder -->
{{ $result = merge $result (dict "permalink" $siteResourceImage.RelPermalink) }}
{{ $result = merge $result (dict "resource" $siteResourceImage) }}
{{ else }}
<!-- Can not find the image -->
{{ errorf "Failed loading image: %q" $defaultImageSetting.src }}
{{ $result = merge $result (dict "exists" false) }}
{{ end }}
{{ else }}
{{ $result = merge $result (dict "permalink" $defaultImageSetting.src) }}
{{ end }}
{{ end }}
{{ end }}
{{ return $result }}