feat(hero): add support for hotlinkFeatureImages

This commit is contained in:
ZhenShuo Leo
2025-09-18 05:15:54 +08:00
parent 4b9e63fd99
commit e6968d877f
5 changed files with 144 additions and 137 deletions

View File

@@ -1,28 +1,42 @@
{{ $disableImageOptimization := .Page.Site.Params.disableImageOptimization | default false }} {{ $disableImageOptimization := site.Store.Get "disableImageOptimization" }}
{{ $featured := "" }} {{ $featured := "" }}
{{ if .Params.featureimage }} {{ $featuredURL := "" }}
{{ $url := .Params.featureimage }} {{ with .Params.featureimage }}
{{ if or (strings.HasPrefix $url "http:") (strings.HasPrefix $url "https:") }} {{ if or (strings.HasPrefix . "http:") (strings.HasPrefix . "https:") }}
{{ $featured = resources.GetRemote $url }} {{ if site.Params.hotlinkFeatureImages }}
{{ $featuredURL = . }}
{{ else }} {{ else }}
{{ $featured = resources.Get $url }} {{ $featured = resources.GetRemote . }}
{{ end }}
{{ else }}
{{ $featured = resources.Get . }}
{{ end }} {{ end }}
{{ end }} {{ end }}
{{ if not $featured }} {{ if not (or $featured $featuredURL) }}
{{ $images := $.Resources.ByType "image" }} {{ $images := .Resources.ByType "image" }}
{{ range slice "*background*" "*feature*" "*cover*" "*thumbnail*" }} {{ range slice "*background*" "*feature*" "*cover*" "*thumbnail*" }}
{{ if not $featured }}{{ $featured = $images.GetMatch . }}{{ end }} {{ if not $featured }}{{ $featured = $images.GetMatch . }}{{ end }}
{{ end }} {{ end }}
{{ end }} {{ end }}
{{ if not $featured }} {{ if not (or $featured $featuredURL) }}
{{ with .Site.Params.defaultBackgroundImage }} {{ $default := site.Store.Get "defaultBackgroundImage" }}
{{ if or (strings.HasPrefix . "http:") (strings.HasPrefix . "https:") }} {{ if $default.url }}
{{ $featured = resources.GetRemote . }} {{ $featuredURL = $default.url }}
{{ else }} {{ else if $default.obj }}
{{ $featured = resources.Get . }} {{ $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 }} {{ end }}
{{ end }} {{ end }}
@@ -39,18 +53,14 @@
) )
}} }}
{{- with $featured -}} {{- with $featuredURL -}}
{{ if $shouldAddHeaderSpace | default true }} {{ if $shouldAddHeaderSpace | default true }}
<div id="hero" class="h-[150px] md:h-[200px]"></div> <div id="hero" class="h-[150px] md:h-[200px]"></div>
{{ end }} {{ end }}
<div class="fixed inset-x-0 top-0 h-[800px] single_hero_background nozoom"> <div class="fixed inset-x-0 top-0 h-[800px] single_hero_background nozoom">
{{ $imageURL := .RelPermalink }}
{{ if not (or $disableImageOptimization (eq .MediaType.SubType "svg")) }}
{{ $imageURL = (.Resize (print ($.Site.Params.backgroundImageWidth | default "1200") "x")).RelPermalink }}
{{ end }}
<img <img
id="background-image" id="background-image"
src="{{ $imageURL }}" src="{{ . }}"
alt="Background Image" alt="Background Image"
class="absolute inset-0 w-full h-full object-cover"> class="absolute inset-0 w-full h-full object-cover">
<div <div
@@ -71,6 +81,6 @@
integrity="{{ $backgroundBlur.Data.Integrity }}" integrity="{{ $backgroundBlur.Data.Integrity }}"
data-blur-id="background-blur" data-blur-id="background-blur"
data-image-id="background-image" data-image-id="background-image"
data-image-url="{{ .RelPermalink }}"></script> data-image-url="{{ . }}"></script>
{{ end }} {{ end }}
{{- end -}} {{- end -}}

View File

@@ -1,39 +1,44 @@
{{ $disableImageOptimization := .Page.Site.Params.disableImageOptimization | default false }} {{ $disableImageOptimization := site.Store.Get "disableImageOptimization" }}
{{ $featured := "" }} {{ $featured := "" }}
{{ $featuredURL := "" }} {{ $featuredURL := "" }}
{{ if .Params.featureimage }} {{ with .Params.featureimage }}
{{ $url := .Params.featureimage }} {{ if or (strings.HasPrefix . "http:") (strings.HasPrefix . "https:") }}
{{ if or (strings.HasPrefix $url "http:") (strings.HasPrefix $url "https:") }} {{ if site.Params.hotlinkFeatureImages }}
{{ $featured = resources.GetRemote $url }} {{ $featuredURL = . }}
{{ else }} {{ else }}
{{ $featured = resources.Get $url }} {{ $featured = resources.GetRemote . }}
{{ end }}
{{ else }}
{{ $featured = resources.Get . }}
{{ end }} {{ end }}
{{ end }} {{ end }}
{{ if not $featured }} {{ if not (or $featured $featuredURL) }}
{{ $images := $.Resources.ByType "image" }} {{ $images := .Resources.ByType "image" }}
{{ range slice "*background*" "*feature*" "*cover*" "*thumbnail*" }} {{ range slice "*background*" "*feature*" "*cover*" "*thumbnail*" }}
{{ if not $featured }}{{ $featured = $images.GetMatch . }}{{ end }} {{ if not $featured }}{{ $featured = $images.GetMatch . }}{{ end }}
{{ end }} {{ end }}
{{ end }} {{ end }}
{{ if not $featured }} {{ if not (or $featured $featuredURL) }}
{{ with .Site.Params.defaultBackgroundImage }} {{ $default := site.Store.Get "defaultBackgroundImage" }}
{{ if or (strings.HasPrefix . "http:") (strings.HasPrefix . "https:") }} {{ if $default.url }}
{{ $featured = resources.GetRemote . }} {{ $featuredURL = $default.url }}
{{ else }} {{ else if $default.obj }}
{{ $featured = resources.Get . }} {{ $featured = $default.obj }}
{{ end }}
{{ end }} {{ end }}
{{ end }} {{ end }}
{{ with $featured }} {{/* generate image URL if not hotlink */}}
{{ if not $featuredURL }}
{{ with $featured }}
{{ $featuredURL = .RelPermalink }} {{ $featuredURL = .RelPermalink }}
{{ if not (or $disableImageOptimization (eq .MediaType.SubType "svg")) }} {{ if not (or $disableImageOptimization (eq .MediaType.SubType "svg")) }}
{{ $size := (print ($.Site.Params.backgroundImageWidth | default "1200") "x") }} {{ $size := site.Store.Get "backgroundImageWidth" }}
{{ $featuredURL = (.Resize $size).RelPermalink }} {{ $featuredURL = (.Resize $size).RelPermalink }}
{{ end }} {{ end }}
{{ end }}
{{ end }} {{ end }}
{{ with $featuredURL }} {{ with $featuredURL }}

View File

@@ -1,28 +1,42 @@
{{ $disableImageOptimization := .Page.Site.Params.disableImageOptimization | default false }} {{ $disableImageOptimization := site.Store.Get "disableImageOptimization" }}
{{ $featured := "" }} {{ $featured := "" }}
{{ if .Params.featureimage }} {{ $featuredURL := "" }}
{{ $url := .Params.featureimage }} {{ with .Params.featureimage }}
{{ if or (strings.HasPrefix $url "http:") (strings.HasPrefix $url "https:") }} {{ if or (strings.HasPrefix . "http:") (strings.HasPrefix . "https:") }}
{{ $featured = resources.GetRemote $url }} {{ if site.Params.hotlinkFeatureImages }}
{{ $featuredURL = . }}
{{ else }} {{ else }}
{{ $featured = resources.Get $url }} {{ $featured = resources.GetRemote . }}
{{ end }}
{{ else }}
{{ $featured = resources.Get . }}
{{ end }} {{ end }}
{{ end }} {{ end }}
{{ if not $featured }} {{ if not (or $featured $featuredURL) }}
{{ $images := $.Resources.ByType "image" }} {{ $images := .Resources.ByType "image" }}
{{ range slice "*background*" "*feature*" "*cover*" "*thumbnail*" }} {{ range slice "*background*" "*feature*" "*cover*" "*thumbnail*" }}
{{ if not $featured }}{{ $featured = $images.GetMatch . }}{{ end }} {{ if not $featured }}{{ $featured = $images.GetMatch . }}{{ end }}
{{ end }} {{ end }}
{{ end }} {{ end }}
{{ if not $featured }} {{ if not (or $featured $featuredURL) }}
{{ with .Site.Params.defaultBackgroundImage }} {{ $default := site.Store.Get "defaultBackgroundImage" }}
{{ if or (strings.HasPrefix . "http:") (strings.HasPrefix . "https:") }} {{ if $default.url }}
{{ $featured = resources.GetRemote . }} {{ $featuredURL = $default.url }}
{{ else }} {{ else if $default.obj }}
{{ $featured = resources.Get . }} {{ $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 }} {{ end }}
{{ end }} {{ end }}
@@ -35,49 +49,13 @@
{{- $alt := .Page.Title -}} {{- $alt := .Page.Title -}}
{{- with .Page.Params.alt }}{{ $alt = . }}{{ end -}} {{- with .Page.Params.alt }}{{ $alt = . }}{{ end -}}
{{- with $featured -}} {{ with $featuredURL }}
{{ if strings.HasSuffix $featured ".svg" }}
{{ with . }}
<figure> <figure>
<img class="w-full rounded-lg single_hero_round nozoom" alt="{{ $alt }}" src="{{ .RelPermalink }}"> <img class="w-full rounded-lg single_hero_round nozoom" alt="{{ $alt }}" src="{{ . }}">
{{ if $caption }} {{ if $caption }}
<figcaption class="text-sm text-neutral-700 dark:text-neutral-400 hover:underline text-center"> <figcaption class="text-sm text-neutral-700 dark:text-neutral-400 hover:underline text-center">
{{ $caption | markdownify }} {{ $caption | markdownify }}
</figcaption> </figcaption>
{{ end }} {{ end }}
</figure> </figure>
{{ end }} {{ end }}
{{ else if $disableImageOptimization }}
{{ with . }}
<figure>
<img
class="w-full rounded-lg single_hero_round nozoom"
alt="{{ $alt }}"
width="{{ .Width }}"
height="{{ .Height }}"
src="{{ .RelPermalink }}">
{{ if $caption }}
<figcaption class="text-sm text-neutral-700 dark:text-neutral-400 hover:underline text-center">
{{ $caption | markdownify }}
</figcaption>
{{ end }}
</figure>
{{ end }}
{{ else }}
{{ with .Resize (print ($.Site.Params.backgroundImageWidth | default "1200") "x") }}
<figure>
<img
class="w-full rounded-lg single_hero_round nozoom"
alt="{{ $alt }}"
width="{{ .Width }}"
height="{{ .Height }}"
src="{{ .RelPermalink }}">
{{ if $caption }}
<figcaption class="text-sm text-neutral-700 dark:text-neutral-400 hover:underline text-center">
{{ $caption | markdownify }}
</figcaption>
{{ end }}
</figure>
{{ end }}
{{ end }}
{{- end -}}

View File

@@ -1,65 +1,76 @@
{{ $disableImageOptimization := .Page.Site.Params.disableImageOptimization | default false }} {{ $disableImageOptimization := site.Store.Get "disableImageOptimization" }}
{{/* === Background === */}}
{{ $images := .Resources.ByType "image" }} {{ $images := .Resources.ByType "image" }}
{{ $background := $images.GetMatch "*background*" }} {{ $background := $images.GetMatch "*background*" }}
{{ $backgroundURL := "" }} {{ $backgroundURL := "" }}
{{ if not $background }} {{ if not (or $background $backgroundURL) }}
{{ with .Site.Params.defaultBackgroundImage }} {{ $default := site.Store.Get "defaultBackgroundImage" }}
{{ if or (strings.HasPrefix . "http:") (strings.HasPrefix . "https:") }} {{ if $default.url }}
{{ $background = resources.GetRemote . }} {{ $backgroundURL = $default.url }}
{{ else }} {{ else if $default.obj }}
{{ $background = resources.Get . }} {{ $background = $default.obj }}
{{ end }}
{{ end }} {{ end }}
{{ end }} {{ end }}
{{ range slice "*cover*" "*thumbnail*" "*feature*" }} {{ if not (or $background $backgroundURL) }}
{{ range slice "*cover*" "*thumbnail*" "*feature*" }}
{{ if not $background }}{{ $background = $images.GetMatch . }}{{ end }} {{ if not $background }}{{ $background = $images.GetMatch . }}{{ end }}
{{ end }}
{{ end }} {{ end }}
{{ with $background }} {{/* generate image URL if not hotlink */}}
{{ if not $backgroundURL }}
{{ with $background }}
{{ $backgroundURL = .RelPermalink }} {{ $backgroundURL = .RelPermalink }}
{{ if not (or $disableImageOptimization (eq .MediaType.SubType "svg")) }} {{ if not (or $disableImageOptimization (eq .MediaType.SubType "svg")) }}
{{ $size := (print ($.Site.Params.backgroundImageWidth | default "1200") "x") }} {{ $size := site.Store.Get "backgroundImageWidth" }}
{{ $backgroundURL = (.Resize $size).RelPermalink }} {{ $backgroundURL = (.Resize $size).RelPermalink }}
{{ end }} {{ end }}
{{ end }}
{{ $featured := "" }}
{{ $featuredURL := "" }}
{{ if .Params.featureimage }}
{{ $url := .Params.featureimage }}
{{ if or (strings.HasPrefix $url "http:") (strings.HasPrefix $url "https:") }}
{{ $featured = resources.GetRemote $url }}
{{ else }}
{{ $featured = resources.Get $url }}
{{ end }} {{ end }}
{{ end }} {{ end }}
{{ if not $featured }} {{/* === Featured === */}}
{{ $featured := "" }}
{{ $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 . }}
{{ end }}
{{ end }}
{{ if not (or $featured $featuredURL) }}
{{ $images := $.Resources.ByType "image" }} {{ $images := $.Resources.ByType "image" }}
{{ range slice "*feature*" "*cover*" "*thumbnail*" "*background*" }} {{ range slice "*feature*" "*cover*" "*thumbnail*" "*background*" }}
{{ if not $featured }}{{ $featured = $images.GetMatch . }}{{ end }} {{ if not $featured }}{{ $featured = $images.GetMatch . }}{{ end }}
{{ end }} {{ end }}
{{ end }} {{ end }}
{{ if not $featured }} {{ if not (or $featured $featuredURL) }}
{{ with .Site.Params.defaultFeaturedImage }} {{ $default := site.Store.Get "defaultFeaturedImage" }}
{{ if or (strings.HasPrefix . "http:") (strings.HasPrefix . "https:") }} {{ if $default.url }}
{{ $featured = resources.GetRemote . }} {{ $featuredURL = $default.url }}
{{ else }} {{ else if $default.obj }}
{{ $featured = resources.Get . }} {{ $featured = $default.obj }}
{{ end }}
{{ end }} {{ end }}
{{ end }} {{ end }}
{{ with $featured }} {{/* generate image URL if not hotlink */}}
{{ if not $featuredURL }}
{{ with $featured }}
{{ $featuredURL = .RelPermalink }} {{ $featuredURL = .RelPermalink }}
{{ if not (or $disableImageOptimization (eq .MediaType.SubType "svg")) }} {{ if not (or $disableImageOptimization (eq .MediaType.SubType "svg")) }}
{{ $size := (print ($.Site.Params.backgroundImageWidth | default "1200") "x") }} {{ $size := site.Store.Get "backgroundImageWidth" }}
{{ $featuredURL = (.Resize $size).RelPermalink }} {{ $featuredURL = (.Resize $size).RelPermalink }}
{{ end }} {{ end }}
{{ end }}
{{ end }} {{ end }}
{{ $isParentList := eq (.Scratch.Get "scope") "list" }} {{ $isParentList := eq (.Scratch.Get "scope") "list" }}

View File

@@ -32,3 +32,6 @@
{{ end }} {{ end }}
{{ end }} {{ end }}
{{ site.Store.Set "defaultBackgroundImage" (dict "url" $defaultBackgroundImageURL "obj" $defaultBackgroundImage) }} {{ site.Store.Set "defaultBackgroundImage" (dict "url" $defaultBackgroundImageURL "obj" $defaultBackgroundImage) }}
{{/* backgroundImageWidth */}}
{{ site.Store.Set "backgroundImageWidth" (print (site.Params.backgroundImageWidth | default "1200") "x") }}