From 3480fcdb9decabcd549ad4939842bbe2ce7654e0 Mon Sep 17 00:00:00 2001 From: Quentin Duchemin Date: Thu, 10 Aug 2023 14:14:57 +0200 Subject: [PATCH] Automatic responsive image resize, much lower footprint --- config.yaml | 2 + themes/chosto/layouts/shortcodes/figure.html | 83 ++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 themes/chosto/layouts/shortcodes/figure.html diff --git a/config.yaml b/config.yaml index 8a84462..e9501ab 100644 --- a/config.yaml +++ b/config.yaml @@ -9,6 +9,8 @@ languageName: Français watch: true paginate: 9 title: Le blog de Quentin +# Give more time to compress images +timeout: 120 permalinks: # All pages under posts section will have this link structure diff --git a/themes/chosto/layouts/shortcodes/figure.html b/themes/chosto/layouts/shortcodes/figure.html new file mode 100644 index 0000000..6132763 --- /dev/null +++ b/themes/chosto/layouts/shortcodes/figure.html @@ -0,0 +1,83 @@ +{{/* + Source : https://gist.github.com/cpbotha/deb310eed14308fe26f7b7d0fabeb34d + + figure with auto-resizing and srcset v2020-05-10 + + Drop-in replacement for Hugo's figure shortcode as of 2020-05-02 that uses img srcset + to enable browsers to download only the resolution that they need. + + The resizing and srcset magic only works for images that are part of the page + bundle. It will fall back to stock Hugo figure behaviour otherwise. + + Improvements that were initially out of reach of my Hugo template programming "skills" + but have now been taken care of: + - [x] gracefully handle images that are not in page bundle, i.e. no image processing available + - [x] use a single configurable sizes array, and derive everything from there + + See https://cpbotha.net/2020/05/02/drop-in-replacement-for-hugo-figure-shortcode-with-img-srcset-support/ + + - original srcset img shortcode from: https://laurakalbag.com/processing-responsive-images-with-hugo/ + - original hugo figure shortcode from: https://github.com/gohugoio/hugo/blob/master/tpl/tplimpl/embedded/templates/shortcodes/figure.html + - no unnecessary resizes and more nudges by Stéfan van der Walt https://mentat.za.net/ + - mashing together and srcset logic fixes by Charl P. Botha https://cpbotha.net/ + + Changes: + - 2020-05-10 fall back to stock Hugo behaviour when no page bundle found + - 2020-05-04 no unnecessary resizes, sizes in array + - 2020-05-02 initial release + + */}} + + {{/* hugo will resize to all of these sizes that are smaller than your original. configure if you like! */}} + {{ $sizes := (slice "480" "800" "1200" "1500") }} + + {{/* get file that matches the filename as specified as src="" in shortcode */}} + {{ $src := .Page.Resources.GetMatch (printf "*%s*" (.Get "src")) }} + + + {{- if .Get "link" -}} + + {{- end }} + {{ with .Get + {{- if .Get "link" }}{{ end -}} + {{- if or (or (.Get "title") (.Get "caption")) (.Get "attr") -}} +
+ {{ with (.Get "title") -}} +

{{ . }}

+ {{- end -}} + {{- if or (.Get "caption") (.Get "attr") -}}

+ {{- .Get "caption" | markdownify -}} + {{- with .Get "attrlink" }} + + {{- end -}} + {{- .Get "attr" | markdownify -}} + {{- if .Get "attrlink" }}{{ end }}

+ {{- end }} +
+ {{- end }} + \ No newline at end of file