From d4df517137820a821bc42f03adf9bf99850ff5c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Wro=CC=81bel?= Date: Mon, 27 May 2024 18:13:45 +0200 Subject: [PATCH] Copy image partial from gruvbox theme --- layouts/partials/image.html | 95 +++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 layouts/partials/image.html diff --git a/layouts/partials/image.html b/layouts/partials/image.html new file mode 100644 index 00000000..d6a747f5 --- /dev/null +++ b/layouts/partials/image.html @@ -0,0 +1,95 @@ + +{{ if .caption }} +
+{{ end }} + +{{ if (strings.HasPrefix .src "http") }} + + {{ .alt }} +{{ else if (strings.HasPrefix .src "/") }} + + {{ $imageConfig := imageConfig (path.Join "static" .src) }} + {{ .alt }} +{{ else }} + {{ $src := .src }} + {{ if strings.HasPrefix $src "./" }} + + {{ $src = substr $src 2 }} + {{ end }} + + {{ $images := .page.Resources.ByType "image" }} + {{ $image := $images.GetMatch $src }} + {{ if eq $image.MediaType.SubType "svg" }} + + {{ .alt }} + {{ else }} + + {{ $minWidth := .page.Site.Params.imageResize.min | default 300 }} + {{ $maxWidth := .page.Site.Params.imageResize.max | default 700 }} + {{ $increment := .page.Site.Params.imageResize.increment | default 200 }} + {{ $widths := seq $minWidth $increment $maxWidth }} + {{ if lt $image.Width $maxWidth }} + {{ $widths = $widths | append $image.Width }} + {{ end }} + {{ $widthsCount := len $widths }} + + {{ $sizes := slice }} + {{ $srcSet := slice }} + {{ $webpSrcSet := slice }} + {{ range $i, $width := $widths }} + {{ if ge $image.Width $width }} + {{ if eq $i (sub $widthsCount 1) }} + {{ $sizes = $sizes | append (printf "%dpx" $width) }} + {{ else }} + {{ $maxWidth := (add $width (sub $increment 1)) }} + {{ $sizes = $sizes | append (printf "(max-width: %dpx) %dpx" $maxWidth $width) }} + {{ end }} + {{ $resized := $image.Resize (printf "%dx" $width) }} + {{ $srcSet = $srcSet | append (printf "%s %dw" $resized.RelPermalink $resized.Width) }} + {{ if not (eq "webp" $image.MediaType.SubType) }} + + {{ $webp := $image.Resize (printf "%dx webp" $width) }} + {{ $webpSrcSet = $webpSrcSet | append (printf "%s %dw" $webp.RelPermalink $webp.Width) }} + {{ end }} + {{ end }} + {{ end }} + + + + + {{ .alt }} + + {{ end }} +{{ end }} + + + +{{ if .caption }} +
{{ .page.RenderString .caption }}
+
+{{ end }} \ No newline at end of file