From fade430e88f050bd1350ffe917176529b3d76d99 Mon Sep 17 00:00:00 2001 From: stereobooster Date: Tue, 31 Oct 2023 17:53:15 +0100 Subject: [PATCH 1/2] Example of integration of picture partial --- config/_default/params.toml | 1 + exampleSite/config/_default/params.toml | 1 + layouts/_default/_markup/render-image.html | 78 +++++--------------- layouts/partials/picture.html | 85 ++++++++++++++++++++++ layouts/partials/pictureDefaults.html | 7 ++ layouts/shortcodes/figure.html | 52 ++++--------- 6 files changed, 126 insertions(+), 98 deletions(-) create mode 100644 layouts/partials/picture.html create mode 100644 layouts/partials/pictureDefaults.html diff --git a/config/_default/params.toml b/config/_default/params.toml index db391cb7..21e4748f 100644 --- a/config/_default/params.toml +++ b/config/_default/params.toml @@ -12,6 +12,7 @@ autoSwitchAppearance = true enableSearch = false enableCodeCopy = false enableImageLazyLoading = true +enableImageWebp = true # robots = "" fingerprintAlgorithm = "sha256" diff --git a/exampleSite/config/_default/params.toml b/exampleSite/config/_default/params.toml index d828f120..41c7a5b0 100644 --- a/exampleSite/config/_default/params.toml +++ b/exampleSite/config/_default/params.toml @@ -12,6 +12,7 @@ autoSwitchAppearance = true enableSearch = true enableCodeCopy = true enableImageLazyLoading = true +enableImageWebp = true # robots = "" fingerprintAlgorithm = "sha256" diff --git a/layouts/_default/_markup/render-image.html b/layouts/_default/_markup/render-image.html index ac82a6aa..2e7a5b53 100644 --- a/layouts/_default/_markup/render-image.html +++ b/layouts/_default/_markup/render-image.html @@ -1,64 +1,20 @@ {{ $url := urls.Parse .Destination }} {{ $altText := .Text }} {{ $caption := .Title }} -{{ $lazyLoad := $.Page.Site.Params.enableImageLazyLoading | default true }} -{{ if findRE "^https?" $url.Scheme }} -
- {{ $altText }} - {{ with $caption }}
{{ . | markdownify }}
{{ end }} -
-{{ else }} - {{ $resource := "" }} - {{ if $.Page.Resources.GetMatch ($url.String) }} - {{ $resource = $.Page.Resources.GetMatch ($url.String) }} - {{ else if resources.GetMatch ($url.String) }} - {{ $resource = resources.Get ($url.String) }} - {{ end }} - {{ with $resource }} -
- {{ $altText }} - {{ with $caption }}
{{ . | markdownify }}
{{ end }} -
- {{ else }} -
- {{ $altText }} - {{ with $caption }}
{{ . | markdownify }}
{{ end }} -
- {{ end }} -{{ end }} \ No newline at end of file +{{ $class := "mx-auto my-0 rounded-md" }} + +{{ $file := $url.Path }} +{{ $img := .Page.Resources.GetMatch $file }} +{{- if and (not $img) .Page.File }} + {{ $path := path.Join .Page.File.Dir $file }} + {{ $img = resources.Get $path }} +{{ end -}} + +
+ {{- with $img -}} + {{ partial "pictureDefaults.html" (dict "img" . "alt" $altText "class" $class) }} + {{- else -}} + {{ $altText }} + {{- end -}} + {{ with $caption }}
{{ . | markdownify }}
{{ end }} +
diff --git a/layouts/partials/picture.html b/layouts/partials/picture.html new file mode 100644 index 00000000..4d5aa7e3 --- /dev/null +++ b/layouts/partials/picture.html @@ -0,0 +1,85 @@ +{{ $img := .img }} +{{ $alt := .alt }} +{{ $class := .class }} +{{ $lazy := .lazy }} +{{ $webp := .webp }} +{{ $lqip := .lqip }} + +{{ with $img }} + {{ if (eq .MediaType.SubType "svg") }} + {{ $width := ""}} + {{ $height := ""}} + {{ $svgContent := .Content }} + {{ range (findRESubmatch `]*width=["']([.0-9]*)["'a-zA-Z]` $svgContent 1) }} + {{ $width = index . 1 }} + {{ end }} + {{ range (findRESubmatch `]*height=["']([.0-9]*)["'a-zA-Z]` $svgContent 1) }} + {{ $height = index . 1 }} + {{ end }} + {{ if (eq "" $width $height) }} + {{ range (findRESubmatch `]*viewBox=["']?([.0-9]*) ([.0-9]*) ([.0-9]*) ([.0-9]*)` $svgContent 1) }} + {{ $width = index . 3 }} + {{ $height = index . 4 }} + {{ end }} + {{ end }} + {{ if (eq "" $width $height) }} + {{ warnf "Can't detect width and height for SVG %s" .RelPermalink }} + {{/* do not use lazy without dimensions */}} + {{ $lazy = false }} + {{ end }} + + {{ . }} + + {{ else }} + + {{ if $webp }} + + {{ end }} + {{ . }} + + {{ end }} +{{ end }} \ No newline at end of file diff --git a/layouts/partials/pictureDefaults.html b/layouts/partials/pictureDefaults.html new file mode 100644 index 00000000..1e31be6c --- /dev/null +++ b/layouts/partials/pictureDefaults.html @@ -0,0 +1,7 @@ +{{ $img := .img }} +{{ $alt := .alt }} +{{ $class := .class }} +{{ $lazy := $.Page.Site.Params.enableImageLazyLoading | default true }} +{{ $webp := $.Page.Site.Params.enableImageWebp | default true }} +{{ $lqip := false }} +{{ partial "picture.html" (dict "img" $img "alt" $alt "class" $class "lazy" $lazy "webp" $webp "lqip" $lqip) }} \ No newline at end of file diff --git a/layouts/shortcodes/figure.html b/layouts/shortcodes/figure.html index 7028c6c1..f0d49dfe 100644 --- a/layouts/shortcodes/figure.html +++ b/layouts/shortcodes/figure.html @@ -6,45 +6,23 @@ {{ $caption := .Get "caption" }} {{ $href := .Get "href" }} {{ $class := .Get "class" }} + + {{ $file := $url.Path }} + {{ $img := .Page.Resources.GetMatch $file }} + {{- if and (not $img) .Page.File }} + {{ $path := path.Join .Page.File.Dir $file }} + {{ $img = resources.Get $path }} + {{ end -}} + {{ with $href }}{{ end }} - {{ $altText }} + + {{- with $img -}} + {{ partial "pictureDefaults.html" (dict "img" . "alt" $altText "class" $class) }} + {{- else -}} + {{ $altText }} + {{- end -}} + {{ with $href }}{{ end }} {{ with $caption }}
{{ . | markdownify }}
{{ end }} From a6fde7cba919c13f04a8db39d688953ca0e088b4 Mon Sep 17 00:00:00 2001 From: stereobooster Date: Tue, 31 Oct 2023 18:14:03 +0100 Subject: [PATCH 2/2] integrate in single.html --- layouts/_default/single.html | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/layouts/_default/single.html b/layouts/_default/single.html index 340e95a1..90e41daa 100644 --- a/layouts/_default/single.html +++ b/layouts/_default/single.html @@ -15,25 +15,9 @@ {{ with $feature }}
- {{ $.Params.featureAlt | default $.Params.coverAlt | default + {{ $altText := $.Params.featureAlt | default $.Params.coverAlt | default "" }} + {{ $class := "mb-6 -mt-4 rounded-md" }} + {{ partial "pictureDefaults.html" (dict "img" . "alt" $altText "class" $class) }} {{ with $.Params.coverCaption }}
{{ . | markdownify }}
{{ end }}