feat(helper/image): new return format

This commit is contained in:
Jimmy Cai 2020-09-11 16:01:01 +02:00
parent 3e18e165e0
commit cfd4cdb731
No known key found for this signature in database
GPG Key ID: 3EA408E527F37B18
8 changed files with 30 additions and 35 deletions

View File

@ -18,12 +18,12 @@
{{ $image := partial "helper/image" . }} {{ $image := partial "helper/image" . }}
{{ if $image.exists }} {{ if $image.exists }}
<div class="taxonomy-image"> <div class="taxonomy-image">
{{ if $image.local }} {{ if $image.resource }}
{{- $thumbnail := $image.src.Fill "120x120" -}} {{- $thumbnail := $image.resource.Fill "120x120" -}}
<img src="{{ $thumbnail.RelPermalink }}" width="{{ $thumbnail.Width }}" <img src="{{ $thumbnail.RelPermalink }}" width="{{ $thumbnail.Width }}"
height="{{ $thumbnail.Height }}" loading="lazy"> height="{{ $thumbnail.Height }}" loading="lazy">
{{ else }} {{ else }}
<img src="{{ $image.src }}" loading="lazy"> <img src="{{ $image.permalink }}" loading="lazy">
{{ end }} {{ end }}
</div> </div>
{{ end }} {{ end }}

View File

@ -16,12 +16,12 @@
{{ if $image.exists }} {{ if $image.exists }}
<div class="article-image"> <div class="article-image">
{{ if $image.local }} {{ if $image.resource }}
{{- $thumbnail := $image.src.Fill "120x120" -}} {{- $thumbnail := $image.resource.Fill "120x120" -}}
<img src="{{ $thumbnail.RelPermalink }}" width="{{ $thumbnail.Width }}" <img src="{{ $thumbnail.RelPermalink }}" width="{{ $thumbnail.Width }}"
height="{{ $thumbnail.Height }}" loading="lazy"> height="{{ $thumbnail.Height }}" loading="lazy">
{{ else }} {{ 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 }} {{ end }}
</div> </div>
{{ end }} {{ end }}

View File

@ -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 }} {{ if $image.exists }}
<div class="article-image"> <div class="article-image">
<a href="{{ .Permalink }}"> <a href="{{ .Permalink }}">
{{ if $image.local }} {{ if $image.resource }}
{{- $thumbnail := $image.src.Fill "800x250" -}} {{- $thumbnail := $image.resource.Fill "800x250" -}}
{{- $thumbnailRetina := $image.src.Fill "1600x500" -}} {{- $thumbnailRetina := $image.resource.Fill "1600x500" -}}
<img src="{{ $thumbnail.RelPermalink }}" <img src="{{ $thumbnail.RelPermalink }}"
srcset="{{ $thumbnail.RelPermalink }} 1x, {{ $thumbnailRetina.RelPermalink }} 2x" srcset="{{ $thumbnail.RelPermalink }} 1x, {{ $thumbnailRetina.RelPermalink }} 2x"
width="{{ $thumbnail.Width }}" height="{{ $thumbnail.Height }}" loading="lazy" width="{{ $thumbnail.Width }}" height="{{ $thumbnail.Height }}" loading="lazy"
alt="Featured image of post {{ .Title }}" /> alt="Featured image of post {{ .Title }}" />
{{ else }} {{ 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 }} {{ end }}
</a> </a>
</div> </div>

View File

@ -4,14 +4,14 @@
{{ if $image.exists }} {{ if $image.exists }}
<div class="article-image"> <div class="article-image">
{{ if $image.local }} {{ if $image.resource }}
{{- $imageRaw := $image.src | resources.Fingerprint "md5" -}} {{- $imageRaw := $image.resource | resources.Fingerprint "md5" -}}
{{- $thumbnail := $imageRaw.Fill .size -}} {{- $thumbnail := $imageRaw.Fill .size -}}
<img src="{{ $thumbnail.RelPermalink }}" width="{{ $thumbnail.Width }}" height="{{ $thumbnail.Height }}" <img src="{{ $thumbnail.RelPermalink }}" width="{{ $thumbnail.Width }}" height="{{ $thumbnail.Height }}"
loading="lazy" data-key="{{ .context.Slug }}" data-hash="{{ $imageRaw.Data.Integrity }}"> loading="lazy" data-key="{{ .context.Slug }}" data-hash="{{ $imageRaw.Data.Integrity }}">
{{ else }} {{ 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 }} {{ end }}
</div> </div>
{{ end }} {{ end }}

View File

@ -4,8 +4,8 @@
{{ with $categories := .Params.categories }} {{ with $categories := .Params.categories }}
<header class="article-category"> <header class="article-category">
{{ range $categories }} {{ range $categories }}
{{ if and $image.exists $image.local }} {{ if and $image.exists $image.resource }}
{{- $imageRaw := $image.src | resources.Fingerprint "md5" -}} {{- $imageRaw := $image.resource | resources.Fingerprint "md5" -}}
{{- $20x := $imageRaw.Fill "20x20 smart" -}} {{- $20x := $imageRaw.Fill "20x20 smart" -}}
<a href="/categories/{{ . | urlize }}" class="color-tag" <a href="/categories/{{ . | urlize }}" class="color-tag"
data-image="{{ $20x.RelPermalink }}" data-key="{{ $context.Slug }}" data-hash="{{ $imageRaw.Data.Integrity }}">{{ . | humanize }}</a> data-image="{{ $20x.RelPermalink }}" data-key="{{ $context.Slug }}" data-hash="{{ $imageRaw.Data.Integrity }}">{{ . | humanize }}</a>

View File

@ -3,15 +3,15 @@
{{ if $image.exists }} {{ if $image.exists }}
<div class="article-image"> <div class="article-image">
{{ if $image.local }} {{ if $image.resource }}
{{- $tablet := $image.src.Resize "1024x" -}} {{- $tablet := $image.resource.Resize "1024x" -}}
{{- $desktop := $image.src.Resize "2000x" -}} {{- $desktop := $image.resource.Resize "2000x" -}}
<img srcset="{{ $tablet.RelPermalink }} 1024w, {{ $desktop.RelPermalink }} 2000w" <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 }}" /> alt="Featured image of post {{ .Title }}" />
{{ else }} {{ 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 }} {{ end }}
</div> </div>
{{ end }} {{ end }}

View File

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

View File

@ -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 }} {{ $imageField := .Params.image }}
{{ if $imageField }} {{ if $imageField }}
<!-- If page has `image` field set --> <!-- If page has `image` field set -->
@ -7,19 +7,19 @@
{{ $url := urls.Parse $imageField }} {{ $url := urls.Parse $imageField }}
{{ if or (eq $url.Scheme "http") (eq $url.Scheme "https") }} {{ if or (eq $url.Scheme "http") (eq $url.Scheme "https") }}
<!-- Is a external image --> <!-- Is a external image -->
{{ $result = merge $result (dict "src" $imageField) }} {{ $result = merge $result (dict "permalink" $imageField) }}
{{ else }} {{ else }}
{{ $pageResourceImage := .Resources.GetMatch (printf "%s" ($imageField | safeURL)) }} {{ $pageResourceImage := .Resources.GetMatch (printf "%s" ($imageField | safeURL)) }}
{{ $siteResourceImage := resources.GetMatch (printf "%s" ($imageField | safeURL)) }} {{ $siteResourceImage := resources.GetMatch (printf "%s" ($imageField | safeURL)) }}
{{ if $pageResourceImage }} {{ if $pageResourceImage }}
<!-- If image is found under page bundle --> <!-- If image is found under page bundle -->
{{ $result = merge $result (dict "local" true) }} {{ $result = merge $result (dict "permalink" $pageResourceImage.RelPermalink) }}
{{ $result = merge $result (dict "src" $pageResourceImage) }} {{ $result = merge $result (dict "resource" $pageResourceImage) }}
{{ else if $siteResourceImage }} {{ else if $siteResourceImage }}
<!-- Try search image under site's assets folder --> <!-- Try search image under site's assets folder -->
{{ $result = merge $result (dict "local" true) }} {{ $result = merge $result (dict "permalink" $siteResourceImage.RelPermalink) }}
{{ $result = merge $result (dict "src" $siteResourceImage) }} {{ $result = merge $result (dict "resource" $siteResourceImage) }}
{{ else }} {{ else }}
<!-- Can not find the image --> <!-- Can not find the image -->
{{ errorf "Failed loading image: %q" $imageField }} {{ errorf "Failed loading image: %q" $imageField }}