From a2662603dfda86d2be2ba0de06809a7e7e7b4f65 Mon Sep 17 00:00:00 2001 From: Jimmy Cai Date: Fri, 11 Sep 2020 16:27:23 +0200 Subject: feat(helper/image): new way to set default image --- layouts/partials/article-list/compact.html | 2 +- layouts/partials/article-list/default.html | 2 +- layouts/partials/article-list/tile.html | 2 +- layouts/partials/article/components/details.html | 2 +- layouts/partials/article/components/header.html | 2 +- layouts/partials/head/opengraph.html | 10 +++---- layouts/partials/helper/image.html | 33 ++++++++++++++++++++++-- 7 files changed, 39 insertions(+), 14 deletions(-) (limited to 'layouts/partials') diff --git a/layouts/partials/article-list/compact.html b/layouts/partials/article-list/compact.html index bebb573..61c1f29 100644 --- a/layouts/partials/article-list/compact.html +++ b/layouts/partials/article-list/compact.html @@ -12,7 +12,7 @@ - {{ $image := partial "helper/image" . }} + {{ $image := partial "helper/image" (dict "Context" .) }} {{ if $image.exists }}
diff --git a/layouts/partials/article-list/default.html b/layouts/partials/article-list/default.html index 0beaeca..879d848 100644 --- a/layouts/partials/article-list/default.html +++ b/layouts/partials/article-list/default.html @@ -1,4 +1,4 @@ -{{ $image := partial "helper/image" . }} +{{ $image := partial "helper/image" (dict "Context" .) }}
{{ if $image.exists }}
diff --git a/layouts/partials/article-list/tile.html b/layouts/partials/article-list/tile.html index a6a29a0..4af3028 100644 --- a/layouts/partials/article-list/tile.html +++ b/layouts/partials/article-list/tile.html @@ -1,4 +1,4 @@ -{{ $image := partial "helper/image" .context }} +{{ $image := partial "helper/image" (dict "Context" .context) }}
diff --git a/layouts/partials/article/components/details.html b/layouts/partials/article/components/details.html index 9ba972c..0a8f777 100644 --- a/layouts/partials/article/components/details.html +++ b/layouts/partials/article/components/details.html @@ -1,4 +1,4 @@ -{{ $image := partial "helper/image" . }} +{{ $image := partial "helper/image" (dict "Context" .) }} {{ $context := . }}
{{ with $categories := .Params.categories }} diff --git a/layouts/partials/article/components/header.html b/layouts/partials/article/components/header.html index fabeaea..702e923 100644 --- a/layouts/partials/article/components/header.html +++ b/layouts/partials/article/components/header.html @@ -1,5 +1,5 @@
- {{ $image := partial "helper/image" . }} + {{ $image := partial "helper/image" (dict "Context" .) }} {{ if $image.exists }}
diff --git a/layouts/partials/head/opengraph.html b/layouts/partials/head/opengraph.html index 26c987a..9410693 100644 --- a/layouts/partials/head/opengraph.html +++ b/layouts/partials/head/opengraph.html @@ -40,13 +40,9 @@ {{- end -}} {{- end -}} -{{ $image := partial "helper/image" . }} +{{ $image := partial "helper/image" (dict "Context" . "Type" "opengraph") }} {{- if $image.exists -}} - - -{{- else if .Site.Params.opengraph.defaultImage -}} - {{ $image := resources.Get .Site.Params.opengraph.defaultImage }} - - + + {{- end -}} \ No newline at end of file diff --git a/layouts/partials/helper/image.html b/layouts/partials/helper/image.html index 3a78d6d..fc9925f 100644 --- a/layouts/partials/helper/image.html +++ b/layouts/partials/helper/image.html @@ -1,5 +1,6 @@ {{ $result := dict "exists" false "permalink" nil "resource" nil "isDefault" false }} -{{ $imageField := .Params.image }} +{{ $imageField := .Context.Params.image }} + {{ if $imageField }} {{ $result = merge $result (dict "exists" true) }} @@ -9,7 +10,7 @@ {{ $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) }} + + + {{ $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 }} + + {{ $result = merge $result (dict "permalink" $siteResourceImage.RelPermalink) }} + {{ $result = merge $result (dict "resource" $siteResourceImage) }} + {{ else }} + + {{ 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 }} \ No newline at end of file -- cgit v1.2.3