From e6968d877f4985404728fb0827d5c34a7e422f23 Mon Sep 17 00:00:00 2001 From: ZhenShuo Leo <98386542+ZhenShuo2021@users.noreply.github.com> Date: Thu, 18 Sep 2025 05:15:54 +0800 Subject: [PATCH] feat(hero): add support for `hotlinkFeatureImages` --- layouts/partials/hero/background.html | 52 ++++++---- layouts/partials/hero/basic.html | 45 +++++---- layouts/partials/hero/big.html | 96 +++++++------------ layouts/partials/hero/thumbAndBackground.html | 85 +++++++++------- layouts/partials/init.html | 3 + 5 files changed, 144 insertions(+), 137 deletions(-) diff --git a/layouts/partials/hero/background.html b/layouts/partials/hero/background.html index 026faad6..59bebb3b 100644 --- a/layouts/partials/hero/background.html +++ b/layouts/partials/hero/background.html @@ -1,28 +1,42 @@ -{{ $disableImageOptimization := .Page.Site.Params.disableImageOptimization | default false }} +{{ $disableImageOptimization := site.Store.Get "disableImageOptimization" }} {{ $featured := "" }} -{{ if .Params.featureimage }} - {{ $url := .Params.featureimage }} - {{ if or (strings.HasPrefix $url "http:") (strings.HasPrefix $url "https:") }} - {{ $featured = resources.GetRemote $url }} +{{ $featuredURL := "" }} +{{ with .Params.featureimage }} + {{ if or (strings.HasPrefix . "http:") (strings.HasPrefix . "https:") }} + {{ if site.Params.hotlinkFeatureImages }} + {{ $featuredURL = . }} + {{ else }} + {{ $featured = resources.GetRemote . }} + {{ end }} {{ else }} - {{ $featured = resources.Get $url }} + {{ $featured = resources.Get . }} {{ end }} {{ end }} -{{ if not $featured }} - {{ $images := $.Resources.ByType "image" }} +{{ if not (or $featured $featuredURL) }} + {{ $images := .Resources.ByType "image" }} {{ range slice "*background*" "*feature*" "*cover*" "*thumbnail*" }} {{ if not $featured }}{{ $featured = $images.GetMatch . }}{{ end }} {{ end }} {{ end }} -{{ if not $featured }} - {{ with .Site.Params.defaultBackgroundImage }} - {{ if or (strings.HasPrefix . "http:") (strings.HasPrefix . "https:") }} - {{ $featured = resources.GetRemote . }} - {{ else }} - {{ $featured = resources.Get . }} +{{ if not (or $featured $featuredURL) }} + {{ $default := site.Store.Get "defaultBackgroundImage" }} + {{ if $default.url }} + {{ $featuredURL = $default.url }} + {{ else if $default.obj }} + {{ $featured = $default.obj }} + {{ end }} +{{ end }} + +{{/* generate image URL if not hotlink */}} +{{ if not $featuredURL }} + {{ with $featured }} + {{ $featuredURL = .RelPermalink }} + {{ if not (or $disableImageOptimization (eq .MediaType.SubType "svg")) }} + {{ $size := site.Store.Get "backgroundImageWidth" }} + {{ $featuredURL = (.Resize $size).RelPermalink }} {{ end }} {{ end }} {{ end }} @@ -39,18 +53,14 @@ ) }} -{{- with $featured -}} +{{- with $featuredURL -}} {{ if $shouldAddHeaderSpace | default true }}
{{ end }}
- {{ $imageURL := .RelPermalink }} - {{ if not (or $disableImageOptimization (eq .MediaType.SubType "svg")) }} - {{ $imageURL = (.Resize (print ($.Site.Params.backgroundImageWidth | default "1200") "x")).RelPermalink }} - {{ end }} Background Image
+ data-image-url="{{ . }}"> {{ end }} {{- end -}} diff --git a/layouts/partials/hero/basic.html b/layouts/partials/hero/basic.html index 36c6f8dc..801f2cca 100644 --- a/layouts/partials/hero/basic.html +++ b/layouts/partials/hero/basic.html @@ -1,38 +1,43 @@ -{{ $disableImageOptimization := .Page.Site.Params.disableImageOptimization | default false }} +{{ $disableImageOptimization := site.Store.Get "disableImageOptimization" }} {{ $featured := "" }} {{ $featuredURL := "" }} -{{ if .Params.featureimage }} - {{ $url := .Params.featureimage }} - {{ if or (strings.HasPrefix $url "http:") (strings.HasPrefix $url "https:") }} - {{ $featured = resources.GetRemote $url }} +{{ with .Params.featureimage }} + {{ if or (strings.HasPrefix . "http:") (strings.HasPrefix . "https:") }} + {{ if site.Params.hotlinkFeatureImages }} + {{ $featuredURL = . }} + {{ else }} + {{ $featured = resources.GetRemote . }} + {{ end }} {{ else }} - {{ $featured = resources.Get $url }} + {{ $featured = resources.Get . }} {{ end }} {{ end }} -{{ if not $featured }} - {{ $images := $.Resources.ByType "image" }} +{{ if not (or $featured $featuredURL) }} + {{ $images := .Resources.ByType "image" }} {{ range slice "*background*" "*feature*" "*cover*" "*thumbnail*" }} {{ if not $featured }}{{ $featured = $images.GetMatch . }}{{ end }} {{ end }} {{ end }} -{{ if not $featured }} - {{ with .Site.Params.defaultBackgroundImage }} - {{ if or (strings.HasPrefix . "http:") (strings.HasPrefix . "https:") }} - {{ $featured = resources.GetRemote . }} - {{ else }} - {{ $featured = resources.Get . }} - {{ end }} +{{ if not (or $featured $featuredURL) }} + {{ $default := site.Store.Get "defaultBackgroundImage" }} + {{ if $default.url }} + {{ $featuredURL = $default.url }} + {{ else if $default.obj }} + {{ $featured = $default.obj }} {{ end }} {{ end }} -{{ with $featured }} - {{ $featuredURL = .RelPermalink }} - {{ if not (or $disableImageOptimization (eq .MediaType.SubType "svg")) }} - {{ $size := (print ($.Site.Params.backgroundImageWidth | default "1200") "x") }} - {{ $featuredURL = (.Resize $size).RelPermalink }} +{{/* generate image URL if not hotlink */}} +{{ if not $featuredURL }} + {{ with $featured }} + {{ $featuredURL = .RelPermalink }} + {{ if not (or $disableImageOptimization (eq .MediaType.SubType "svg")) }} + {{ $size := site.Store.Get "backgroundImageWidth" }} + {{ $featuredURL = (.Resize $size).RelPermalink }} + {{ end }} {{ end }} {{ end }} diff --git a/layouts/partials/hero/big.html b/layouts/partials/hero/big.html index 2a0ad286..a867f78a 100644 --- a/layouts/partials/hero/big.html +++ b/layouts/partials/hero/big.html @@ -1,28 +1,42 @@ -{{ $disableImageOptimization := .Page.Site.Params.disableImageOptimization | default false }} +{{ $disableImageOptimization := site.Store.Get "disableImageOptimization" }} {{ $featured := "" }} -{{ if .Params.featureimage }} - {{ $url := .Params.featureimage }} - {{ if or (strings.HasPrefix $url "http:") (strings.HasPrefix $url "https:") }} - {{ $featured = resources.GetRemote $url }} +{{ $featuredURL := "" }} +{{ with .Params.featureimage }} + {{ if or (strings.HasPrefix . "http:") (strings.HasPrefix . "https:") }} + {{ if site.Params.hotlinkFeatureImages }} + {{ $featuredURL = . }} + {{ else }} + {{ $featured = resources.GetRemote . }} + {{ end }} {{ else }} - {{ $featured = resources.Get $url }} + {{ $featured = resources.Get . }} {{ end }} {{ end }} -{{ if not $featured }} - {{ $images := $.Resources.ByType "image" }} +{{ if not (or $featured $featuredURL) }} + {{ $images := .Resources.ByType "image" }} {{ range slice "*background*" "*feature*" "*cover*" "*thumbnail*" }} {{ if not $featured }}{{ $featured = $images.GetMatch . }}{{ end }} {{ end }} {{ end }} -{{ if not $featured }} - {{ with .Site.Params.defaultBackgroundImage }} - {{ if or (strings.HasPrefix . "http:") (strings.HasPrefix . "https:") }} - {{ $featured = resources.GetRemote . }} - {{ else }} - {{ $featured = resources.Get . }} +{{ if not (or $featured $featuredURL) }} + {{ $default := site.Store.Get "defaultBackgroundImage" }} + {{ if $default.url }} + {{ $featuredURL = $default.url }} + {{ else if $default.obj }} + {{ $featured = $default.obj }} + {{ end }} +{{ end }} + +{{/* generate image URL if not hotlink */}} +{{ if not $featuredURL }} + {{ with $featured }} + {{ $featuredURL = .RelPermalink }} + {{ if not (or $disableImageOptimization (eq .MediaType.SubType "svg")) }} + {{ $size := site.Store.Get "backgroundImageWidth" }} + {{ $featuredURL = (.Resize $size).RelPermalink }} {{ end }} {{ end }} {{ end }} @@ -35,49 +49,13 @@ {{- $alt := .Page.Title -}} {{- with .Page.Params.alt }}{{ $alt = . }}{{ end -}} -{{- with $featured -}} - {{ if strings.HasSuffix $featured ".svg" }} - {{ with . }} -
- {{ $alt }} - {{ if $caption }} -
- {{ $caption | markdownify }} -
- {{ end }} -
+{{ with $featuredURL }} +
+ {{ $alt }} + {{ if $caption }} +
+ {{ $caption | markdownify }} +
{{ end }} - {{ else if $disableImageOptimization }} - {{ with . }} -
- {{ $alt }} - {{ if $caption }} -
- {{ $caption | markdownify }} -
- {{ end }} -
- {{ end }} - {{ else }} - {{ with .Resize (print ($.Site.Params.backgroundImageWidth | default "1200") "x") }} -
- {{ $alt }} - {{ if $caption }} -
- {{ $caption | markdownify }} -
- {{ end }} -
- {{ end }} - {{ end }} -{{- end -}} +
+{{ end }} diff --git a/layouts/partials/hero/thumbAndBackground.html b/layouts/partials/hero/thumbAndBackground.html index e378537d..877ca934 100644 --- a/layouts/partials/hero/thumbAndBackground.html +++ b/layouts/partials/hero/thumbAndBackground.html @@ -1,64 +1,75 @@ -{{ $disableImageOptimization := .Page.Site.Params.disableImageOptimization | default false }} +{{ $disableImageOptimization := site.Store.Get "disableImageOptimization" }} +{{/* === Background === */}} {{ $images := .Resources.ByType "image" }} {{ $background := $images.GetMatch "*background*" }} {{ $backgroundURL := "" }} -{{ if not $background }} - {{ with .Site.Params.defaultBackgroundImage }} - {{ if or (strings.HasPrefix . "http:") (strings.HasPrefix . "https:") }} - {{ $background = resources.GetRemote . }} - {{ else }} - {{ $background = resources.Get . }} +{{ if not (or $background $backgroundURL) }} + {{ $default := site.Store.Get "defaultBackgroundImage" }} + {{ if $default.url }} + {{ $backgroundURL = $default.url }} + {{ else if $default.obj }} + {{ $background = $default.obj }} + {{ end }} +{{ end }} + +{{ if not (or $background $backgroundURL) }} + {{ range slice "*cover*" "*thumbnail*" "*feature*" }} + {{ if not $background }}{{ $background = $images.GetMatch . }}{{ end }} + {{ end }} +{{ end }} + +{{/* generate image URL if not hotlink */}} +{{ if not $backgroundURL }} + {{ with $background }} + {{ $backgroundURL = .RelPermalink }} + {{ if not (or $disableImageOptimization (eq .MediaType.SubType "svg")) }} + {{ $size := site.Store.Get "backgroundImageWidth" }} + {{ $backgroundURL = (.Resize $size).RelPermalink }} {{ end }} {{ end }} {{ end }} -{{ range slice "*cover*" "*thumbnail*" "*feature*" }} - {{ if not $background }}{{ $background = $images.GetMatch . }}{{ end }} -{{ end }} - -{{ with $background }} - {{ $backgroundURL = .RelPermalink }} - {{ if not (or $disableImageOptimization (eq .MediaType.SubType "svg")) }} - {{ $size := (print ($.Site.Params.backgroundImageWidth | default "1200") "x") }} - {{ $backgroundURL = (.Resize $size).RelPermalink }} - {{ end }} -{{ end }} - +{{/* === Featured === */}} {{ $featured := "" }} {{ $featuredURL := "" }} -{{ if .Params.featureimage }} - {{ $url := .Params.featureimage }} - {{ if or (strings.HasPrefix $url "http:") (strings.HasPrefix $url "https:") }} - {{ $featured = resources.GetRemote $url }} +{{ with .Params.featureimage }} + {{ if or (strings.HasPrefix . "http:") (strings.HasPrefix . "https:") }} + {{ if site.Params.hotlinkFeatureImages }} + {{ $featuredURL = . }} + {{ else }} + {{ $featured = resources.GetRemote . }} + {{ end }} {{ else }} - {{ $featured = resources.Get $url }} + {{ $featured = resources.Get . }} {{ end }} {{ end }} -{{ if not $featured }} +{{ if not (or $featured $featuredURL) }} {{ $images := $.Resources.ByType "image" }} {{ range slice "*feature*" "*cover*" "*thumbnail*" "*background*" }} {{ if not $featured }}{{ $featured = $images.GetMatch . }}{{ end }} {{ end }} {{ end }} -{{ if not $featured }} - {{ with .Site.Params.defaultFeaturedImage }} - {{ if or (strings.HasPrefix . "http:") (strings.HasPrefix . "https:") }} - {{ $featured = resources.GetRemote . }} - {{ else }} - {{ $featured = resources.Get . }} - {{ end }} +{{ if not (or $featured $featuredURL) }} + {{ $default := site.Store.Get "defaultFeaturedImage" }} + {{ if $default.url }} + {{ $featuredURL = $default.url }} + {{ else if $default.obj }} + {{ $featured = $default.obj }} {{ end }} {{ end }} -{{ with $featured }} - {{ $featuredURL = .RelPermalink }} - {{ if not (or $disableImageOptimization (eq .MediaType.SubType "svg")) }} - {{ $size := (print ($.Site.Params.backgroundImageWidth | default "1200") "x") }} - {{ $featuredURL = (.Resize $size).RelPermalink }} +{{/* generate image URL if not hotlink */}} +{{ if not $featuredURL }} + {{ with $featured }} + {{ $featuredURL = .RelPermalink }} + {{ if not (or $disableImageOptimization (eq .MediaType.SubType "svg")) }} + {{ $size := site.Store.Get "backgroundImageWidth" }} + {{ $featuredURL = (.Resize $size).RelPermalink }} + {{ end }} {{ end }} {{ end }} diff --git a/layouts/partials/init.html b/layouts/partials/init.html index 6c03fa96..dd83e4e7 100644 --- a/layouts/partials/init.html +++ b/layouts/partials/init.html @@ -32,3 +32,6 @@ {{ end }} {{ end }} {{ site.Store.Set "defaultBackgroundImage" (dict "url" $defaultBackgroundImageURL "obj" $defaultBackgroundImage) }} + +{{/* backgroundImageWidth */}} +{{ site.Store.Set "backgroundImageWidth" (print (site.Params.backgroundImageWidth | default "1200") "x") }}