From a292f0f9fe26b8a9f034875d333f8f624e24fb53 Mon Sep 17 00:00:00 2001 From: Fredrik Jonsson Date: Wed, 23 Jun 2021 10:17:53 +0200 Subject: [PATCH] Add srcset support to figure and img shortcodes. --- layouts/shortcodes/figure.html | 12 +++++++++++- layouts/shortcodes/img.html | 12 +++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/layouts/shortcodes/figure.html b/layouts/shortcodes/figure.html index e3c16bc..683b697 100644 --- a/layouts/shortcodes/figure.html +++ b/layouts/shortcodes/figure.html @@ -7,6 +7,8 @@ {{ $link := .Get "link" -}} {{ $size := .Get "size" -}} {{ $src := .Get "src" -}} +{{ $srcset := .Get "srcset" -}} +{{ $srcsetw := (slice) }} {{ $title := .Get "title" -}} {{ $width := .Get "width" -}} {{ $maxwidth := .Site.Params.imageMaxWidth -}} @@ -24,6 +26,14 @@ {{ $realwidth = $original.Width -}} {{ $realheight = $original.Height -}} {{ end -}} + {{ if $srcset -}} + {{ range $set := split $srcset " " -}} + {{ $image := $original.Resize (printf "%sx" $set) -}} + {{ $srcsetw = $srcsetw | append (printf "%s %sw" $image.RelPermalink $set) }} + {{ end }} + {{ $width = "" -}} + {{ $height = "" -}} + {{ end -}} {{ else if and $src (fileExists (add "/static" $src)) -}} {{ with (imageConfig (add "/static" $src)) -}} {{ $realwidth = .Width -}} @@ -54,7 +64,7 @@ {{ end -}}
{{ with $link }}{{ end -}} -{{ . }} +{{ . }} {{- if $link }}{{ end }} {{ if or $title $caption $attr -}}
diff --git a/layouts/shortcodes/img.html b/layouts/shortcodes/img.html index e7caff2..6bf7bd2 100644 --- a/layouts/shortcodes/img.html +++ b/layouts/shortcodes/img.html @@ -4,6 +4,8 @@ {{ $link := .Get "link" -}} {{ $size := .Get "size" -}} {{ $src := .Get "src" -}} +{{ $srcset := .Get "srcset" -}} +{{ $srcsetw := (slice) }} {{ $width := .Get "width" -}} {{ $maxwidth := .Site.Params.imageMaxWidth -}} {{ $realwidth := 0 -}} @@ -20,6 +22,14 @@ {{ $realwidth = $original.Width -}} {{ $realheight = $original.Height -}} {{ end -}} + {{ if $srcset -}} + {{ range $set := split $srcset " " -}} + {{ $image := $original.Resize (printf "%sx" $set) -}} + {{ $srcsetw = $srcsetw | append (printf "%s %sw" $image.RelPermalink $set) }} + {{ end }} + {{ $width = "" -}} + {{ $height = "" -}} + {{ end -}} {{ else if and $src (fileExists (add "/static" $src)) -}} {{ with (imageConfig (add "/static" $src)) -}} {{ $realwidth = .Width -}} @@ -49,5 +59,5 @@ {{ $height = (math.Round $height) -}} {{ end -}} {{ with $link }}{{ end -}} -{{ . }} +{{ . }} {{- if $link }}{{ end }}