diff options
author | Jimmy Cai <jimmehcai@gmail.com> | 2020-09-11 17:01:01 +0300 |
---|---|---|
committer | Jimmy Cai <jimmehcai@gmail.com> | 2020-09-11 17:01:01 +0300 |
commit | cfd4cdb731750786f787b7aaea6a9f411caa5af2 (patch) | |
tree | 430a267cd9b818b50bac1da27dd7f1cac55384cb | |
parent | 3e18e165e0dbc10ea1e81c745a04ea7ae82c6e47 (diff) |
feat(helper/image): new return format
-rw-r--r-- | layouts/_default/term.html | 6 | ||||
-rw-r--r-- | layouts/partials/article-list/compact.html | 6 | ||||
-rw-r--r-- | layouts/partials/article-list/default.html | 13 | ||||
-rw-r--r-- | layouts/partials/article-list/tile.html | 6 | ||||
-rw-r--r-- | layouts/partials/article/components/details.html | 4 | ||||
-rw-r--r-- | layouts/partials/article/components/header.html | 10 | ||||
-rw-r--r-- | layouts/partials/head/opengraph.html | 8 | ||||
-rw-r--r-- | layouts/partials/helper/image.html | 12 |
8 files changed, 30 insertions, 35 deletions
diff --git a/layouts/_default/term.html b/layouts/_default/term.html index d0f9471..4ca37da 100644 --- a/layouts/_default/term.html +++ b/layouts/_default/term.html @@ -18,12 +18,12 @@ {{ $image := partial "helper/image" . }} {{ if $image.exists }} <div class="taxonomy-image"> - {{ if $image.local }} - {{- $thumbnail := $image.src.Fill "120x120" -}} + {{ if $image.resource }} + {{- $thumbnail := $image.resource.Fill "120x120" -}} <img src="{{ $thumbnail.RelPermalink }}" width="{{ $thumbnail.Width }}" height="{{ $thumbnail.Height }}" loading="lazy"> {{ else }} - <img src="{{ $image.src }}" loading="lazy"> + <img src="{{ $image.permalink }}" loading="lazy"> {{ end }} </div> {{ end }} diff --git a/layouts/partials/article-list/compact.html b/layouts/partials/article-list/compact.html index dfcc276..bebb573 100644 --- a/layouts/partials/article-list/compact.html +++ b/layouts/partials/article-list/compact.html @@ -16,12 +16,12 @@ {{ if $image.exists }} <div class="article-image"> - {{ if $image.local }} - {{- $thumbnail := $image.src.Fill "120x120" -}} + {{ if $image.resource }} + {{- $thumbnail := $image.resource.Fill "120x120" -}} <img src="{{ $thumbnail.RelPermalink }}" width="{{ $thumbnail.Width }}" height="{{ $thumbnail.Height }}" loading="lazy"> {{ else }} - <img src="{{ $image.src }}" loading="lazy" alt="Featured image of post {{ .Title }}" /> + <img src="{{ $image.permalink }}" loading="lazy" alt="Featured image of post {{ .Title }}" /> {{ end }} </div> {{ end }} diff --git a/layouts/partials/article-list/default.html b/layouts/partials/article-list/default.html index 45f00d1..0beaeca 100644 --- a/layouts/partials/article-list/default.html +++ b/layouts/partials/article-list/default.html @@ -1,19 +1,18 @@ -<article class="{{ if .Params.image }}has-image{{ end }}"> - {{ $image := partial "helper/image" . }} - +{{ $image := partial "helper/image" . }} +<article class="{{ if $image.exists }}has-image{{ end }}"> {{ if $image.exists }} <div class="article-image"> <a href="{{ .Permalink }}"> - {{ if $image.local }} - {{- $thumbnail := $image.src.Fill "800x250" -}} - {{- $thumbnailRetina := $image.src.Fill "1600x500" -}} + {{ if $image.resource }} + {{- $thumbnail := $image.resource.Fill "800x250" -}} + {{- $thumbnailRetina := $image.resource.Fill "1600x500" -}} <img src="{{ $thumbnail.RelPermalink }}" srcset="{{ $thumbnail.RelPermalink }} 1x, {{ $thumbnailRetina.RelPermalink }} 2x" width="{{ $thumbnail.Width }}" height="{{ $thumbnail.Height }}" loading="lazy" alt="Featured image of post {{ .Title }}" /> {{ else }} - <img src="{{ $image.src }}" loading="lazy" alt="Featured image of post {{ .Title }}" /> + <img src="{{ $image.permalink }}" loading="lazy" alt="Featured image of post {{ .Title }}" /> {{ end }} </a> </div> diff --git a/layouts/partials/article-list/tile.html b/layouts/partials/article-list/tile.html index 6b4375a..a6a29a0 100644 --- a/layouts/partials/article-list/tile.html +++ b/layouts/partials/article-list/tile.html @@ -4,14 +4,14 @@ {{ if $image.exists }} <div class="article-image"> - {{ if $image.local }} - {{- $imageRaw := $image.src | resources.Fingerprint "md5" -}} + {{ if $image.resource }} + {{- $imageRaw := $image.resource | resources.Fingerprint "md5" -}} {{- $thumbnail := $imageRaw.Fill .size -}} <img src="{{ $thumbnail.RelPermalink }}" width="{{ $thumbnail.Width }}" height="{{ $thumbnail.Height }}" loading="lazy" data-key="{{ .context.Slug }}" data-hash="{{ $imageRaw.Data.Integrity }}"> {{ else }} - <img src="{{ $image.src }}" loading="lazy" data-key="{{ .context.Slug }}" data-hash="{{ $image.src }}"/> + <img src="{{ $image.permalink }}" loading="lazy" data-key="{{ .context.Slug }}" data-hash="{{ $image.permalink }}"/> {{ end }} </div> {{ end }} diff --git a/layouts/partials/article/components/details.html b/layouts/partials/article/components/details.html index 0e3ff50..9ba972c 100644 --- a/layouts/partials/article/components/details.html +++ b/layouts/partials/article/components/details.html @@ -4,8 +4,8 @@ {{ with $categories := .Params.categories }} <header class="article-category"> {{ range $categories }} - {{ if and $image.exists $image.local }} - {{- $imageRaw := $image.src | resources.Fingerprint "md5" -}} + {{ if and $image.exists $image.resource }} + {{- $imageRaw := $image.resource | resources.Fingerprint "md5" -}} {{- $20x := $imageRaw.Fill "20x20 smart" -}} <a href="/categories/{{ . | urlize }}" class="color-tag" data-image="{{ $20x.RelPermalink }}" data-key="{{ $context.Slug }}" data-hash="{{ $imageRaw.Data.Integrity }}">{{ . | humanize }}</a> diff --git a/layouts/partials/article/components/header.html b/layouts/partials/article/components/header.html index 14f6da2..fabeaea 100644 --- a/layouts/partials/article/components/header.html +++ b/layouts/partials/article/components/header.html @@ -3,15 +3,15 @@ {{ if $image.exists }} <div class="article-image"> - {{ if $image.local }} - {{- $tablet := $image.src.Resize "1024x" -}} - {{- $desktop := $image.src.Resize "2000x" -}} + {{ if $image.resource }} + {{- $tablet := $image.resource.Resize "1024x" -}} + {{- $desktop := $image.resource.Resize "2000x" -}} <img srcset="{{ $tablet.RelPermalink }} 1024w, {{ $desktop.RelPermalink }} 2000w" - src="{{ $desktop.RelPermalink }}" width="{{ $image.Width }}" height="{{ $image.Height }}" loading="lazy" + src="{{ $desktop.RelPermalink }}" width="{{ $image.resource.Width }}" height="{{ $image.resource.Height }}" loading="lazy" alt="Featured image of post {{ .Title }}" /> {{ else }} - <img src="{{ $image.src }}" loading="lazy" alt="Featured image of post {{ .Title }}" /> + <img src="{{ $image.permalink }}" loading="lazy" alt="Featured image of post {{ .Title }}" /> {{ end }} </div> {{ end }} diff --git a/layouts/partials/head/opengraph.html b/layouts/partials/head/opengraph.html index 654c55c..26c987a 100644 --- a/layouts/partials/head/opengraph.html +++ b/layouts/partials/head/opengraph.html @@ -42,13 +42,9 @@ {{ $image := partial "helper/image" . }} {{- if $image.exists -}} - {{ $imagePermalink := $image.src }} - {{ if $image.local }} - {{ $imagePermalink = absURL $image.src.RelPermalink }} - {{ end }} <meta name="twitter:card" content="summary_large_image"> - <meta property='og:image' content='{{ $imagePermalink }}' /> - <meta name="twitter:image" content='{{ $imagePermalink }}' /> + <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 }}' /> diff --git a/layouts/partials/helper/image.html b/layouts/partials/helper/image.html index c8371b4..3a78d6d 100644 --- a/layouts/partials/helper/image.html +++ b/layouts/partials/helper/image.html @@ -1,4 +1,4 @@ -{{ $result := dict "exists" false "local" false "isDefault" false }} +{{ $result := dict "exists" false "permalink" nil "resource" nil "isDefault" false }} {{ $imageField := .Params.image }} {{ if $imageField }} <!-- If page has `image` field set --> @@ -7,19 +7,19 @@ {{ $url := urls.Parse $imageField }} {{ if or (eq $url.Scheme "http") (eq $url.Scheme "https") }} <!-- Is a external image --> - {{ $result = merge $result (dict "src" $imageField) }} + {{ $result = merge $result (dict "permalink" $imageField) }} {{ else }} {{ $pageResourceImage := .Resources.GetMatch (printf "%s" ($imageField | safeURL)) }} {{ $siteResourceImage := resources.GetMatch (printf "%s" ($imageField | safeURL)) }} {{ if $pageResourceImage }} <!-- If image is found under page bundle --> - {{ $result = merge $result (dict "local" true) }} - {{ $result = merge $result (dict "src" $pageResourceImage) }} + {{ $result = merge $result (dict "permalink" $pageResourceImage.RelPermalink) }} + {{ $result = merge $result (dict "resource" $pageResourceImage) }} {{ else if $siteResourceImage }} <!-- Try search image under site's assets folder --> - {{ $result = merge $result (dict "local" true) }} - {{ $result = merge $result (dict "src" $siteResourceImage) }} + {{ $result = merge $result (dict "permalink" $siteResourceImage.RelPermalink) }} + {{ $result = merge $result (dict "resource" $siteResourceImage) }} {{ else }} <!-- Can not find the image --> {{ errorf "Failed loading image: %q" $imageField }} |