diff options
author | Jimmy Cai <jimmehcai@gmail.com> | 2020-09-11 17:27:23 +0300 |
---|---|---|
committer | Jimmy Cai <jimmehcai@gmail.com> | 2020-09-11 17:27:23 +0300 |
commit | a2662603dfda86d2be2ba0de06809a7e7e7b4f65 (patch) | |
tree | 49fe83d2f4881f4e85583a9f2ba2a93b801beedd /layouts/partials | |
parent | cfd4cdb731750786f787b7aaea6a9f411caa5af2 (diff) |
feat(helper/image): new way to set default image
Diffstat (limited to 'layouts/partials')
-rw-r--r-- | layouts/partials/article-list/compact.html | 2 | ||||
-rw-r--r-- | layouts/partials/article-list/default.html | 2 | ||||
-rw-r--r-- | layouts/partials/article-list/tile.html | 2 | ||||
-rw-r--r-- | layouts/partials/article/components/details.html | 2 | ||||
-rw-r--r-- | layouts/partials/article/components/header.html | 2 | ||||
-rw-r--r-- | layouts/partials/head/opengraph.html | 10 | ||||
-rw-r--r-- | layouts/partials/helper/image.html | 33 |
7 files changed, 39 insertions, 14 deletions
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 @@ </footer> </div> - {{ $image := partial "helper/image" . }} + {{ $image := partial "helper/image" (dict "Context" .) }} {{ if $image.exists }} <div class="article-image"> 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" .) }} <article class="{{ if $image.exists }}has-image{{ end }}"> {{ if $image.exists }} <div class="article-image"> 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) }} <article class="{{ if $image.exists }}has-image{{ end }}"> <a href="{{ .context.Permalink }}"> 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 := . }} <div class="article-details"> {{ 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 @@ <header class="article-header"> - {{ $image := partial "helper/image" . }} + {{ $image := partial "helper/image" (dict "Context" .) }} {{ if $image.exists }} <div class="article-image"> 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 -}} <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 -}}
\ 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 }} <!-- 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 }}
\ No newline at end of file |