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
No known key found for this signature in database
GPG key ID: 3EA408E527F37B18
7 changed files with 100 additions and 49 deletions

View file

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

View file

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

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 }}"> <a href="{{ .context.Permalink }}">
{{ if .context.Params.image }}
{{- $image := partial "helper/image" .context | resources.Fingerprint "md5" -}} {{ if $image.exists }}
{{- $thumbnail := $image.Fill .size -}} <div class="article-image">
<div class="article-image"> {{ if $image.local }}
<img src="{{ $thumbnail.RelPermalink }}" width="{{ $thumbnail.Width }}" height="{{ $thumbnail.Height }}" {{- $imageRaw := $image.src | resources.Fingerprint "md5" -}}
loading="lazy" data-key="{{ .context.Slug }}" data-hash="{{ $image.Data.Integrity }}"> {{- $thumbnail := $imageRaw.Fill .size -}}
</div>
<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 }} {{ end }}
<div class="article-details"> <div class="article-details">

View file

@ -1,15 +1,14 @@
{{ $i := .Params.image }} {{ $image := partial "helper/image" . }}
{{ $context := . }} {{ $context := . }}
<div class="article-details"> <div class="article-details">
{{ with $categories := .Params.categories }} {{ with $categories := .Params.categories }}
<header class="article-category"> <header class="article-category">
{{ range $categories }} {{ range $categories }}
{{ if $i }} {{ if and $image.exists $image.local }}
{{- $image := partial "helper/image" $context | resources.Fingerprint "md5" -}} {{- $imageRaw := $image.src | resources.Fingerprint "md5" -}}
{{- $20x := $image.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="{{ $image.Data.Integrity }}">{{ . | humanize }}</a> data-image="{{ $20x.RelPermalink }}" data-key="{{ $context.Slug }}" data-hash="{{ $imageRaw.Data.Integrity }}">{{ . | humanize }}</a>
{{ else }} {{ else }}
<a href="/categories/{{ . | urlize }}">{{ . | humanize }}</a> <a href="/categories/{{ . | urlize }}">{{ . | humanize }}</a>
{{ end }} {{ end }}

View file

@ -1,16 +1,19 @@
<header class="article-header"> <header class="article-header">
{{ if .Params.image }} {{ $image := partial "helper/image" . }}
{{- $image := partial "helper/image" . -}}
{{- $tablet := $image.Resize "1024x" -}} {{ if $image.exists }}
{{- $desktop := $image.Resize "2000x" -}} <div class="article-image">
{{ if $image.local }}
{{- $tablet := $image.src.Resize "1024x" -}}
{{- $desktop := $image.src.Resize "2000x" -}}
<div class="article-image"> <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.Width }}" height="{{ $image.Height }}" alt="Featured image of post {{ .Title }}" />
loading="lazy" {{ else }}
alt="Featured image of post {{ .Title }}" /> <img src="{{ $image.src }}" loading="lazy" alt="Featured image of post {{ .Title }}" />
</div> {{ end }}
</div>
{{ end }} {{ end }}
{{ partial "article/components/details" . }} {{ partial "article/components/details" . }}

View file

@ -40,11 +40,15 @@
{{- end -}} {{- end -}}
{{- end -}} {{- end -}}
{{- if .Params.image -}} {{ $image := partial "helper/image" . }}
{{ $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 name="twitter:card" content="summary_large_image">
<meta property='og:image' content='{{ absURL $image.RelPermalink }}' /> <meta property='og:image' content='{{ $imagePermalink }}' />
<meta name="twitter:image" content='{{ absURL $image.RelPermalink }}' /> <meta name="twitter:image" content='{{ $imagePermalink }}' />
{{- 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,2 +1,31 @@
{{- $image := .Resources.GetMatch (printf "%s" (.Params.image | safeURL)) -}} {{ $result := dict "exists" false "local" false "isDefault" false }}
{{ return $image }} {{ $imageField := .Params.image }}
{{ if $imageField }}
<!-- If page has `image` field set -->
{{ $result = merge $result (dict "exists" true) }}
{{ if strings.HasPrefix $imageField "http" }}
<!-- Is a external image -->
{{ $result = merge $result (dict "src" $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) }}
{{ else if $siteResourceImage }}
<!-- Try search image under site's assets folder -->
{{ $result = merge $result (dict "local" true) }}
{{ $result = merge $result (dict "src" $siteResourceImage) }}
{{ else }}
<!-- Can not find the image -->
{{ errorf "Failed loading image: %q" $imageField }}
{{ $result = merge $result (dict "exists" false) }}
{{ end }}
{{ end }}
{{ end }}
{{ return $result }}