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" . }}
{{ 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 }}

View file

@ -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 }}

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 }}
<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>

View file

@ -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 }}

View file

@ -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>

View file

@ -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 }}

View file

@ -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 }}' />

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 }}
{{ 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 }}