feat(helper/image): new image helper (WIP)

This commit is contained in:
Jimmy Cai
2020-09-10 18:53:36 +02:00
parent 62b0da64df
commit 28c06ef8ae
7 changed files with 100 additions and 49 deletions
+11 -7
View File
@@ -12,13 +12,17 @@
</footer>
</div>
{{ if .Params.image }}
{{- $image := partial "helper/image" . -}}
{{- $thumbnail := $image.Fill "120x120" -}}
{{ $image := partial "helper/image" . }}
<div class="article-image">
<img src="{{ $thumbnail.RelPermalink }}" width="{{ $thumbnail.Width }}"
height="{{ $thumbnail.Height }}" loading="lazy">
</div>
{{ if $image.exists }}
<div class="article-image">
{{ if $image.local }}
{{- $thumbnail := $image.src.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 }}" />
{{ end }}
</div>
{{ end }}
</article>
+17 -12
View File
@@ -1,17 +1,22 @@
<article class="{{ if .Params.image }}has-image{{ end }}">
{{ if .Params.image }}
{{- $image := partial "helper/image" . -}}
{{- $thumbnail := $image.Fill "800x250" -}}
{{- $thumbnailRetina := $image.Fill "1600x500" -}}
{{ $image := partial "helper/image" . }}
<div class="article-image">
<a href="{{ .Permalink }}">
<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 }}" />
</a>
</div>
{{ if $image.exists }}
<div class="article-image">
<a href="{{ .Permalink }}">
{{ if $image.local }}
{{- $thumbnail := $image.src.Fill "800x250" -}}
{{- $thumbnailRetina := $image.src.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 }}" />
{{ end }}
</a>
</div>
{{ end }}
{{ partial "article/components/details" . }}
+15 -8
View File
@@ -1,12 +1,19 @@
<article class="{{ if .context.Params.image }}has-image{{ end }}">
{{ $image := partial "helper/image" .context }}
<article class="{{ if $image.exists }}has-image{{ end }}">
<a href="{{ .context.Permalink }}">
{{ if .context.Params.image }}
{{- $image := partial "helper/image" .context | resources.Fingerprint "md5" -}}
{{- $thumbnail := $image.Fill .size -}}
<div class="article-image">
<img src="{{ $thumbnail.RelPermalink }}" width="{{ $thumbnail.Width }}" height="{{ $thumbnail.Height }}"
loading="lazy" data-key="{{ .context.Slug }}" data-hash="{{ $image.Data.Integrity }}">
</div>
{{ if $image.exists }}
<div class="article-image">
{{ if $image.local }}
{{- $imageRaw := $image.src | 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 }}"/>
{{ end }}
</div>
{{ end }}
<div class="article-details">