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] [params.widgets.tagCloud]
limit = 10 limit = 10
[params.opengraph] [params.opengraph]
defaultImage = ""
[params.opengraph.twitter] [params.opengraph.twitter]
site = "" site = ""
[params.defaultImage]
[params.defaultImage.opengraph]
enabled = false
local = false
src = ""
[menu] [menu]
[[menu.main]] [[menu.main]]

View File

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

View File

@ -12,7 +12,7 @@
</footer> </footer>
</div> </div>
{{ $image := partial "helper/image" . }} {{ $image := partial "helper/image" (dict "Context" .) }}
{{ if $image.exists }} {{ if $image.exists }}
<div class="article-image"> <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 }}"> <article class="{{ if $image.exists }}has-image{{ end }}">
{{ if $image.exists }} {{ if $image.exists }}
<div class="article-image"> <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 }}"> <article class="{{ if $image.exists }}has-image{{ end }}">
<a href="{{ .context.Permalink }}"> <a href="{{ .context.Permalink }}">

View File

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

View File

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

View File

@ -40,13 +40,9 @@
{{- end -}} {{- end -}}
{{- end -}} {{- end -}}
{{ $image := partial "helper/image" . }} {{ $image := partial "helper/image" (dict "Context" . "Type" "opengraph") }}
{{- if $image.exists -}} {{- if $image.exists -}}
<meta name="twitter:card" content="summary_large_image"> <meta name="twitter:card" content="summary_large_image">
<meta property='og:image' content='{{ $image.permalink }}' /> <meta property='og:image' content='{{ absURL $image.permalink }}' />
<meta name="twitter:image" content='{{ $image.permalink }}' /> <meta name="twitter:image" content='{{ absURL $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 }}' />
{{- end -}} {{- end -}}

View File

@ -1,5 +1,6 @@
{{ $result := dict "exists" false "permalink" nil "resource" nil "isDefault" false }} {{ $result := dict "exists" false "permalink" nil "resource" nil "isDefault" false }}
{{ $imageField := .Params.image }} {{ $imageField := .Context.Params.image }}
{{ if $imageField }} {{ if $imageField }}
<!-- If page has `image` field set --> <!-- If page has `image` field set -->
{{ $result = merge $result (dict "exists" true) }} {{ $result = merge $result (dict "exists" true) }}
@ -9,7 +10,7 @@
<!-- Is a external image --> <!-- Is a external image -->
{{ $result = merge $result (dict "permalink" $imageField) }} {{ $result = merge $result (dict "permalink" $imageField) }}
{{ else }} {{ else }}
{{ $pageResourceImage := .Resources.GetMatch (printf "%s" ($imageField | safeURL)) }} {{ $pageResourceImage := .Context.Resources.GetMatch (printf "%s" ($imageField | safeURL)) }}
{{ $siteResourceImage := resources.GetMatch (printf "%s" ($imageField | safeURL)) }} {{ $siteResourceImage := resources.GetMatch (printf "%s" ($imageField | safeURL)) }}
{{ if $pageResourceImage }} {{ if $pageResourceImage }}
@ -27,6 +28,34 @@
{{ end }} {{ end }}
{{ 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 }} {{ end }}
{{ return $result }} {{ return $result }}