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 := "" }}
{{ 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.Get $url }}
{{ $featured = resources.GetRemote . }}
{{ end }}
{{ else }}
{{ $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 }}
<div id="hero" class="h-[150px] md:h-[200px]"></div>
{{ end }}
<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
id="background-image"
src="{{ $imageURL }}"
src="{{ . }}"
alt="Background Image"
class="absolute inset-0 w-full h-full object-cover">
<div
@@ -71,6 +81,6 @@
integrity="{{ $backgroundBlur.Data.Integrity }}"
data-blur-id="background-blur"
data-image-id="background-image"
data-image-url="{{ .RelPermalink }}"></script>
data-image-url="{{ . }}"></script>
{{ end }}
{{- end -}}

View File

@@ -1,39 +1,44 @@
{{ $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.Get $url }}
{{ $featured = resources.GetRemote . }}
{{ end }}
{{ else }}
{{ $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 }}
{{/* generate image URL if not hotlink */}}
{{ if not $featuredURL }}
{{ with $featured }}
{{ $featuredURL = .RelPermalink }}
{{ 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 }}
{{ end }}
{{ end }}
{{ end }}
{{ with $featuredURL }}

View File

@@ -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.Get $url }}
{{ $featured = resources.GetRemote . }}
{{ end }}
{{ else }}
{{ $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 . }}
{{ with $featuredURL }}
<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 }}
<figcaption class="text-sm text-neutral-700 dark:text-neutral-400 hover:underline text-center">
{{ $caption | markdownify }}
</figcaption>
{{ end }}
</figure>
{{ 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 -}}
{{ end }}

View File

@@ -1,65 +1,76 @@
{{ $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 . }}
{{ end }}
{{ 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 }}
{{ range slice "*cover*" "*thumbnail*" "*feature*" }}
{{ if not (or $background $backgroundURL) }}
{{ range slice "*cover*" "*thumbnail*" "*feature*" }}
{{ if not $background }}{{ $background = $images.GetMatch . }}{{ end }}
{{ end }}
{{ end }}
{{ with $background }}
{{/* generate image URL if not hotlink */}}
{{ if not $backgroundURL }}
{{ with $background }}
{{ $backgroundURL = .RelPermalink }}
{{ 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 }}
{{ 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 }}
{{ 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" }}
{{ 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 }}
{{/* generate image URL if not hotlink */}}
{{ if not $featuredURL }}
{{ with $featured }}
{{ $featuredURL = .RelPermalink }}
{{ 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 }}
{{ end }}
{{ end }}
{{ end }}
{{ $isParentList := eq (.Scratch.Get "scope") "list" }}

View File

@@ -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") }}