From f578268214abe2e2438ff7ba1ec85f77ea825465 Mon Sep 17 00:00:00 2001 From: Dominik Oswald <6849456+d-oit@users.noreply.github.com> Date: Fri, 20 Dec 2024 18:31:50 +0100 Subject: [PATCH 1/2] image srcset enhancement --- data/dimensions.yml | 23 +++++++++++ data/structures/gallery.yml | 21 ++++++++++ data/structures/image-adapter.yml | 2 +- data/structures/image.yml | 6 +++ layouts/partials/assets/adapters/hugo.html | 11 ++++-- .../assets/helpers/image-definition.html | 38 +++++++++++++------ .../assets/helpers/image-dimension.html | 19 ++++++---- .../partials/assets/helpers/image-set.html | 5 ++- layouts/partials/assets/video.html | 1 + layouts/shortcodes/image.html | 3 ++ 10 files changed, 105 insertions(+), 24 deletions(-) create mode 100644 data/structures/gallery.yml diff --git a/data/dimensions.yml b/data/dimensions.yml index e7b090578..25cc0889a 100644 --- a/data/dimensions.yml +++ b/data/dimensions.yml @@ -1,5 +1,8 @@ - ratio: 4x3 dimensions: + - 150x112 + - 300x225 + - 450x337 - 576x432 - 768x576 - 992x744 @@ -8,6 +11,9 @@ - 2800x2100 - ratio: 3x2 dimensions: + - 150x100 + - 300x200 + - 450x300 - 576x384 - 768x512 - 992x661 @@ -16,6 +22,9 @@ - 2800x1867 - ratio: 1x1 dimensions: + - 150x150 + - 300x300 + - 450x450 - 576x576 - 768x768 - 992x992 @@ -24,6 +33,9 @@ - 2800x2800 - ratio: 16x9 dimensions: + - 150x84 + - 300x169 + - 450x253 - 576x324 - 768x432 - 992x558 @@ -32,6 +44,9 @@ - 2800x1575 - ratio: 21x9 dimensions: + - 150x64 + - 300x129 + - 450x193 - 576x247 - 768x329 - 992x425 @@ -40,9 +55,17 @@ - 2800x1200 - ratio: auto dimensions: + - 50 # Small thumbnails + - 150 # Standard thumbnails + - 300 + - 450 - 576 - 768 - 992 - 1200 - 1400 + - 1920 # Full HD + - 2560 # QHD - 2800 + - 3840 # 4K + - 5120 # 5K diff --git a/data/structures/gallery.yml b/data/structures/gallery.yml new file mode 100644 index 000000000..4716de326 --- /dev/null +++ b/data/structures/gallery.yml @@ -0,0 +1,21 @@ +comment: >- + Displays the publication date and title as gallery. +arguments: + page: + type: + - '*hugolib.pageState' + - '*hugolib.pageForShortcode' + optional: false + comment: Required context of the current page. + group: partial + list: + type: + - 'page.Pages' + - 'resource.Resources' + optional: false + comment: Required array of pages. + group: partial + class: + type: string + optional: true + comment: Class attribute of the list element, e.g. “w-50”. \ No newline at end of file diff --git a/data/structures/image-adapter.yml b/data/structures/image-adapter.yml index e937fe820..0fdb731cb 100644 --- a/data/structures/image-adapter.yml +++ b/data/structures/image-adapter.yml @@ -52,6 +52,7 @@ arguments: values: - fill - fit + - resize height: type: int optional: false @@ -76,4 +77,3 @@ arguments: - Bottom - BottomRight - Smart - diff --git a/data/structures/image.yml b/data/structures/image.yml index 370e51b35..c8c954d1b 100644 --- a/data/structures/image.yml +++ b/data/structures/image.yml @@ -58,6 +58,7 @@ arguments: to vector graphics. options: values: + - auto - 1x1 - 3x2 - 4x3 @@ -161,3 +162,8 @@ arguments: - BottomRight - Smart release: v0.24.22 + dataAttributes: + type: string + optional: true + comment: image data attributes `data-*`. + release: v0.27.14 \ No newline at end of file diff --git a/layouts/partials/assets/adapters/hugo.html b/layouts/partials/assets/adapters/hugo.html index bd280532b..75f0fb7ec 100644 --- a/layouts/partials/assets/adapters/hugo.html +++ b/layouts/partials/assets/adapters/hugo.html @@ -1,4 +1,4 @@ - {{ if not $error }} {{ $scaled := "" }} - {{ if eq $transform "fill" }} + + {{ if and (eq $transform "fill") $height }} {{- $scaled = $img.Fill (printf "%dx%d %s %s" $width $height $anchor $format) -}} + {{ else if (not $height) }} + {{- $scaled = $img.Resize (printf "%dx %s %s" $width $anchor $format) -}} + {{- $height = $scaled.Height -}} {{ else }} {{- $scaled = $img.Fit (printf "%dx%d %s" $width $height $format) -}} {{ end }} + {{- $clean := path.Ext $img.RelPermalink -}} {{ $destination := "" }} @@ -68,4 +73,4 @@ {{- end -}} {{ end }} -{{ return $element }} +{{ return $element }} \ No newline at end of file diff --git a/layouts/partials/assets/helpers/image-definition.html b/layouts/partials/assets/helpers/image-definition.html index d47155270..0b2ba1fb9 100644 --- a/layouts/partials/assets/helpers/image-definition.html +++ b/layouts/partials/assets/helpers/image-definition.html @@ -17,6 +17,8 @@ {{- $plain := .plain | default false }} {{- $anchor := .anchor }} {{- $alt := or $title $caption (T "image") }} +{{- $dataAttributes := .dataAttributes }} + {{- $fileAnchor := "" -}} @@ -27,7 +29,6 @@ {{- $url = index $segments 0 -}} {{- $fileAnchor = index $segments 1 -}} {{- end -}} - {{ $target := partial "assets/helpers/image-dimension.html" (dict "page" $page @@ -43,21 +44,18 @@ {{ $height := index $target "height" }} {{ $width := index $target "width" }} {{ $data := index $target "data" }} - {{- range $none := $modes -}} {{- if ne $none $mode -}} {{- $wrapper = printf "%s d-none-%s" (or $wrapper "") $none -}} {{- end -}} {{- end -}} - {{- if $caption -}}
{{ else }} {{ with $wrapper }}
{{ end }} {{ end }} - {{ if $data }} {{- if site.Params.debugging.includeSVGOrigin }} {{ printf "" $.url | safeHTML }} @@ -65,14 +63,30 @@ {{- $data = replace $data " + +{{ if in $class "lightbox" }} +
+
+{{ end }} + +{{ if in $class "lightbox" }} + +
+
+{{ end }} {{- else }} diff --git a/layouts/partials/assets/helpers/image-dimension.html b/layouts/partials/assets/helpers/image-dimension.html index 121eb62c5..9cb708f9d 100644 --- a/layouts/partials/assets/helpers/image-dimension.html +++ b/layouts/partials/assets/helpers/image-dimension.html @@ -52,11 +52,10 @@ {{- if hasSuffix $url "svg" -}} {{- $res = partial "utilities/GetResource.html" (dict "url" $url "page" $page) -}} {{ if not $res }} - {{- $targetURL := partial "utilities/GetStaticURL" (dict "url" (strings.TrimPrefix "/static" $url)) -}} - {{- if not (fileExists (path.Join "/static" $targetURL)) -}} + {{- if not (fileExists (path.Join "/static" $url)) -}} {{ warnf "Cannot find vector image resource: %q" $url -}} {{ else }} - {{ $width := string (partial "utilities/GetWidth.html" (dict "path" $targetURL "height" 500)) }} + {{ $width := string (partial "utilities/GetWidth.html" (dict "path" $url "height" 500)) }} {{ if $width }} {{ $dims = $dims | append (printf "%sx500" $width) }} {{ else }} @@ -84,8 +83,12 @@ {{ $widths := partial "assets/helpers/GetDimension.html" (dict "ratio" "auto") }} {{ range $w := $widths -}} - {{ $height = int (math.Round (mul (div (float $w) $img.Width) $img.Height)) }} - {{- $dims = $dims | append (printf "%dx%d" (int $w) $height ) -}} + {{ if $height }} + {{ $height = int (math.Round (mul (div (float $w) $img.Width) $img.Height)) }} + {{- $dims = $dims | append (printf "%dx%d" (int $w) $height ) -}} + {{ else}} + {{- $dims = $dims | append (printf "%dx" (int $w) ) -}} + {{ end }} {{- end -}} {{ end }} {{ end }} @@ -111,7 +114,9 @@ {{ with $dims }} {{ range $dim := (. | last 1) }} {{ $width = (int (index (split $dim "x") 0)) }} - {{ $height = (int (index (split $dim "x") 1)) }} + {{ if $height }} + {{ $height = (int (index (split $dim "x") 1)) }} + {{ end}} {{ end }} {{ end }} {{ end }} @@ -119,7 +124,7 @@ {{ if or (hasSuffix $url "svg") $plain }} - {{- $targetURL = partial "utilities/GetStaticURL" (dict "url" (strings.TrimPrefix "/static" $url)) -}} + {{- $targetURL = partial "utilities/GetStaticURL" (dict "url" $url) -}} {{ else }} {{- $targetURL = partial "assets/helpers/image-set.html" (dict "url" $url diff --git a/layouts/partials/assets/helpers/image-set.html b/layouts/partials/assets/helpers/image-set.html index ce381fc2b..4e61ec796 100644 --- a/layouts/partials/assets/helpers/image-set.html +++ b/layouts/partials/assets/helpers/image-set.html @@ -24,7 +24,10 @@ {{ $imgset := slice }} {{- range $index, $dim := $dims -}} {{ $width := (int (index (split $dim "x") 0)) }} - {{ $height := (int (index (split $dim "x") 1)) }} + {{ $height := "" }} + {{ if $height }} + {{ $height = (int (index (split $dim "x") 1)) }} + {{ end }} {{- $element := partial $adapter (dict "host" $host diff --git a/layouts/partials/assets/video.html b/layouts/partials/assets/video.html index fc86121aa..918cd25fd 100644 --- a/layouts/partials/assets/video.html +++ b/layouts/partials/assets/video.html @@ -128,6 +128,7 @@ "url" (printf "https://res.cloudinary.com/%s/video/upload/%s" $account (path.Clean $thumbnail)) "ratio" $ratio "imageset" false + "height" 500 ) }} {{ $height := index $metadata "height" }} {{ $width := index $metadata "width" }} diff --git a/layouts/shortcodes/image.html b/layouts/shortcodes/image.html index 6f1dbe799..bb6fea498 100644 --- a/layouts/shortcodes/image.html +++ b/layouts/shortcodes/image.html @@ -26,6 +26,7 @@ {{- $error := false -}} {{- $plain := false -}} {{- $anchor := "" -}} +{{- $dataAttributes := "" -}} {{- if .IsNamedParams -}} {{ $url = .Get "src" | default "" -}} @@ -37,6 +38,7 @@ {{ $title = .Get "title" | default "" -}} {{ $caption = .Get "caption" | default "" -}} {{ $figclass = .Get "figclass" | default "" -}} + {{ $dataAttributes = .Get "dataAttributes" | default "" -}} {{ if isset .Params "mode" }}{{ $mode = partial "utilities/CastBool.html" (.Get "mode") }}{{ end -}} {{ if isset .Params "portrait" }}{{ $portrait = partial "utilities/CastBool.html" (.Get "portrait") }}{{ end -}} {{ if isset .Params "plain" }}{{ $plain = partial "utilities/CastBool.html" (.Get "plain") }}{{ end -}} @@ -65,6 +67,7 @@ "plain" $plain "anchor" $anchor "loading" $loading + "dataAttributes" $dataAttributes "page" .Page) -}} {{- end -}} From a8a46e9ab1123b84983afb9d0a722144a8171eba Mon Sep 17 00:00:00 2001 From: Dominik Oswald <6849456+d-oit@users.noreply.github.com> Date: Fri, 20 Dec 2024 19:00:41 +0100 Subject: [PATCH 2/2] add data- attribute to image and showcase the changes in the en/image.md --- exampleSite/content/en/blog/image.md | 9 +++++++++ exampleSite/hugo_stats.json | 1 + layouts/_default/_markup/render-image.html | 1 + layouts/partials/assets/helpers/image-definition.html | 4 ++-- layouts/partials/assets/image.html | 2 ++ layouts/shortcodes/image.html | 2 ++ 6 files changed, 17 insertions(+), 2 deletions(-) diff --git a/exampleSite/content/en/blog/image.md b/exampleSite/content/en/blog/image.md index ccadb6a28..ae61c6cd3 100644 --- a/exampleSite/content/en/blog/image.md +++ b/exampleSite/content/en/blog/image.md @@ -40,3 +40,12 @@ As an example, the following shortcode displays an image with rounded corners an ratio="21x9" caption="imgix image" class="rounded" anchor="Top" */>}} {{< /example >}} {{% comment %}}{{% /comment %}} + +## Local image + +{{% comment %}}{{% /comment %}} +{{< example lang="hugo" >}} +{{}} +{{< /example >}} +{{% comment %}}{{% /comment %}} diff --git a/exampleSite/hugo_stats.json b/exampleSite/hugo_stats.json index 3b2bfade6..0b63aea61 100644 --- a/exampleSite/hugo_stats.json +++ b/exampleSite/hugo_stats.json @@ -584,6 +584,7 @@ "lien", "liens-vers-des-tiers-et-utilisation-de-vos-informations", "link", + "local-image", "lottie-animation-0", "manage-cookie-preferences", "map", diff --git a/layouts/_default/_markup/render-image.html b/layouts/_default/_markup/render-image.html index 4e4669810..5eb0c34e3 100644 --- a/layouts/_default/_markup/render-image.html +++ b/layouts/_default/_markup/render-image.html @@ -12,4 +12,5 @@ "portrait" .Attributes.portrait "ratio" .Attributes.ratio "wrapper" .Attributes.wrapper + "dataAttributes" .Attributes.dataAttributes ) }} diff --git a/layouts/partials/assets/helpers/image-definition.html b/layouts/partials/assets/helpers/image-definition.html index 0b2ba1fb9..cae47e1bd 100644 --- a/layouts/partials/assets/helpers/image-definition.html +++ b/layouts/partials/assets/helpers/image-definition.html @@ -17,7 +17,7 @@ {{- $plain := .plain | default false }} {{- $anchor := .anchor }} {{- $alt := or $title $caption (T "image") }} -{{- $dataAttributes := .dataAttributes }} +{{- $dataAttributes := .dataAttributes | default "" }} @@ -76,7 +76,7 @@ {{ with $height }}height="{{ . }}"{{ end }} {{ with $width }}width="{{ . }}"{{ end }} {{ with $alt }}alt="{{ . }}"{{ end }} - {{ if $dataAttributes }}{{ . }}{{ end }} + {{ if $dataAttributes }}{{ with $dataAttributes }}{{ . | safeHTMLAttr }}{{ end }}{{ end }} > {{ if in $class "lightbox" }}