Add blogPhoto shortcode

This shortcode is intended to be used in blogposts to include photos. It
downscales them and uses srcset to serve appropriate sizes to the
browsers.
This commit is contained in:
Vojtěch Káně 2020-04-16 23:14:46 +02:00
parent 5e602eda1c
commit 0a61815dcb

View File

@ -0,0 +1,29 @@
{{- /*
Usage:
{{< blogPhoto "filename_relative_to_page.ext" "Alt text" >}}
or
{{< blogPhoto src="filename_relative_to_page.ext" alt="Alt text" >}}
*/ -}}
{{- $imageName := false -}}
{{- $alt := false -}}
{{- if .IsNamedParams -}}
{{- $imageName = .Get "src" | default nil -}}
{{- $alt = .Get "alt" | default nil -}}
{{- else -}}
{{- $imageName = .Get 0 -}}
{{- $alt = .Get 1 -}}
{{- end -}}
{{- if (eq $imageName nil) }}{{ errorf "Missing image name in %s" .Position }}{{ end -}}
{{- if (eq $alt nil) }}{{ errorf "Missing alt for image %s in %s" $imageName .Position }}{{ end -}}
{{- $img := .Page.Resources.GetMatch (printf "%s" $imageName) | default nil -}}
{{- if (eq $img nil) }}{{ errorf "Image %s not found in %s" $imageName .Position }}{{ end -}}
{{- $resizedFullHD := $img.Fit "1920x1080 q50" -}}
{{- $resizedHD := $img.Fit "1280x720 q50" -}}
{{- $resizedSD := $img.Fit "640x360 q50" -}}
<img src="{{ $resizedFullHD.RelPermalink }}" alt="{{ $alt }}" srcset="{{ $resizedFullHD.RelPermalink }} 1080w, {{ $resizedHD.RelPermalink }} 720w, {{ $resizedSD.RelPermalink }} 360w" sizes="(orientation: portrait) 100vw, 50vw">