mirror of
https://github.com/tiennm99/HugoBlox-kit.git
synced 2026-10-11 12:11:45 +00:00
fix(blox): unify image path resolution logic across blocks
- Update 'resume-biography', 'research-areas', 'logos', and 'cta-image-paragraph' blocks - Add support for remote images (http/https) via resources.GetRemote - Add support for Page Resources (bundles) via .Resources.GetMatch - Normalize paths to handle 'assets/', 'media/', './' prefixes consistently - Maintain backward compatibility with global assets and static URLs
This commit is contained in:
1 parent
f500a081dd
commit
3323f61390
5 files changed
+250
-36
No files matched your search
@@ -28,15 +28,47 @@
|
||||
{{ $image_name := "" }}
|
||||
{{ $image_raw := index $item "image" }}
|
||||
{{ if eq (printf "%T" $image_raw) "string" }}{{ $image_name = strings.TrimSpace $image_raw }}{{ end }}
|
||||
{{ $image_path := "" }}
|
||||
{{ if $image_name }}{{ $image_path = path.Join "media" $image_name }}{{ end }}
|
||||
{{ $image := "" }}
|
||||
{{ if $image_path }}{{ $image = resources.GetMatch $image_path }}{{ end }}
|
||||
{{ if $image }}
|
||||
{{/* Resolve image from page bundle, site assets, remote, or static URLs */}}
|
||||
{{ $image_resource := false }}
|
||||
{{ $image_url := "" }}
|
||||
{{ if $image_name }}
|
||||
{{ $image_path := strings.TrimSpace (printf "%v" $image_name) }}
|
||||
{{ $is_remote := or (strings.HasPrefix $image_path "http://") (strings.HasPrefix $image_path "https://") }}
|
||||
|
||||
{{ if $is_remote }}
|
||||
{{ $remote := try (resources.GetRemote $image_path) }}
|
||||
{{ if and $remote (not $remote.Err) }}
|
||||
{{ $image_resource = $remote.Value }}
|
||||
{{ else }}
|
||||
{{ $image_url = $image_path }}
|
||||
{{ end }}
|
||||
{{ else }}
|
||||
{{ $image_path = strings.TrimPrefix "/" $image_path }}
|
||||
{{ $image_path = strings.TrimPrefix "./" $image_path }}
|
||||
{{ $image_path = strings.TrimPrefix "assets/" $image_path }}
|
||||
{{ $image_path = strings.TrimPrefix "media/" $image_path }}
|
||||
{{ $normalized := path.Clean $image_path }}
|
||||
|
||||
{{ if and $normalized (ne $normalized ".") }}
|
||||
{{ with $page }}
|
||||
{{ $image_resource = (.Resources.ByType "image").GetMatch $normalized }}
|
||||
{{ end }}
|
||||
{{ if not $image_resource }}
|
||||
{{ $image_resource = resources.Get (path.Join "media" $normalized) }}
|
||||
{{ end }}
|
||||
{{ if not $image_resource }}
|
||||
{{ $image_url = printf "/media/%s" $normalized }}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
|
||||
{{ $image_to_render := $image_resource }}
|
||||
{{ if $image_to_render }}
|
||||
<div class="gap-8 items-center py-8 px-4 mx-auto max-w-screen-xl xl:gap-16 md:grid md:grid-cols-2 sm:py-16 lg:px-6">
|
||||
{{ if ne $image.MediaType.SubType "gif" }}
|
||||
{{ if ne $image_to_render.MediaType.SubType "gif" }}
|
||||
{{ $responsive := partial "functions/process_responsive_image.html" (dict
|
||||
"image" $image
|
||||
"image" $image_to_render
|
||||
"mode" "fit"
|
||||
"sizes" (slice 400 600 800 1200)
|
||||
) }}
|
||||
@@ -49,11 +81,18 @@
|
||||
alt="{{$title | plainify}}" {{ if (modBool $idx 2)}}style="order: 1"{{end}}>
|
||||
{{ else }}
|
||||
<img class="w-full"
|
||||
src="{{$image.RelPermalink}}"
|
||||
width="{{$image.Width}}"
|
||||
height="{{$image.Height}}"
|
||||
src="{{$image_to_render.RelPermalink}}"
|
||||
width="{{$image_to_render.Width}}"
|
||||
height="{{$image_to_render.Height}}"
|
||||
alt="{{$title | plainify}}" {{ if (modBool $idx 2)}}style="order: 1"{{end}}>
|
||||
{{ end }}
|
||||
{{ else if $image_url }}
|
||||
<div class="gap-8 items-center py-8 px-4 mx-auto max-w-screen-xl xl:gap-16 md:grid md:grid-cols-2 sm:py-16 lg:px-6">
|
||||
{{ $is_external := or (strings.HasPrefix $image_url "http://") (strings.HasPrefix $image_url "https://") }}
|
||||
<img class="w-full"
|
||||
src="{{ if $is_external }}{{ $image_url }}{{ else }}{{ $image_url | relURL }}{{ end }}"
|
||||
alt="{{$title | plainify}}" {{ if (modBool $idx 2)}}style="order: 1"{{end}}>
|
||||
{{ end }}
|
||||
{{ else }}
|
||||
{{ warnf "%s uses the `cta-image-paragraph` blox which specifies a non-existent `image` at `assets/%s`. Skipping this item." $page.File.Path $image_path }}
|
||||
{{ end }}
|
||||
|
||||
@@ -111,9 +111,43 @@
|
||||
<div class="block p-4 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-800 transition-all duration-300">
|
||||
{{ end }}
|
||||
<div class="relative">
|
||||
{{/* Logo Image */}}
|
||||
{{/* Logo Image */}}
|
||||
{{ if $image_name }}
|
||||
{{ $img := resources.Get (printf "media/%s" $image_name) }}
|
||||
{{/* Resolve image from page bundle, site assets, remote, or static URLs */}}
|
||||
{{ $image_resource := false }}
|
||||
{{ $image_url := "" }}
|
||||
{{ $image_path := strings.TrimSpace (printf "%v" $image_name) }}
|
||||
{{ $is_remote := or (strings.HasPrefix $image_path "http://") (strings.HasPrefix $image_path "https://") }}
|
||||
|
||||
{{ if $is_remote }}
|
||||
{{ $remote := try (resources.GetRemote $image_path) }}
|
||||
{{ if and $remote (not $remote.Err) }}
|
||||
{{ $image_resource = $remote.Value }}
|
||||
{{ else }}
|
||||
{{ $image_url = $image_path }}
|
||||
{{ end }}
|
||||
{{ else }}
|
||||
{{ $image_path = strings.TrimPrefix "/" $image_path }}
|
||||
{{ $image_path = strings.TrimPrefix "./" $image_path }}
|
||||
{{ $image_path = strings.TrimPrefix "assets/" $image_path }}
|
||||
{{ $image_path = strings.TrimPrefix "media/" $image_path }}
|
||||
{{ $normalized := path.Clean $image_path }}
|
||||
|
||||
{{ if and $normalized (ne $normalized ".") }}
|
||||
{{ with $page }}
|
||||
{{ $image_resource = (.Resources.ByType "image").GetMatch $normalized }}
|
||||
{{ end }}
|
||||
{{ if not $image_resource }}
|
||||
{{ $image_resource = resources.Get (path.Join "media" $normalized) }}
|
||||
{{ end }}
|
||||
{{ if not $image_resource }}
|
||||
{{ $image_url = printf "/media/%s" $normalized }}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
|
||||
{{ $img := $image_resource }}
|
||||
{{ if $img }}
|
||||
{{ $isSVG := eq $img.MediaType.SubType "svg" }}
|
||||
{{ if not $isSVG }}
|
||||
@@ -124,6 +158,12 @@
|
||||
{{ if not $isSVG }}width="{{ $img.Width }}" height="{{ $img.Height }}"{{ end }}
|
||||
class="h-12 sm:h-14 lg:h-16 w-auto mx-auto object-contain filter grayscale opacity-60 group-hover:grayscale-0 group-hover:opacity-100 transition-all duration-300 group-hover:scale-110"
|
||||
loading="lazy">
|
||||
{{ else if $image_url }}
|
||||
{{ $is_external := or (strings.HasPrefix $image_url "http://") (strings.HasPrefix $image_url "https://") }}
|
||||
<img src="{{ if $is_external }}{{ $image_url }}{{ else }}{{ $image_url | relURL }}{{ end }}"
|
||||
alt="{{ $name | default "Partner logo" }}"
|
||||
class="h-12 sm:h-14 lg:h-16 w-auto mx-auto object-contain filter grayscale opacity-60 group-hover:grayscale-0 group-hover:opacity-100 transition-all duration-300 group-hover:scale-110"
|
||||
loading="lazy">
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
|
||||
|
||||
@@ -176,12 +176,49 @@
|
||||
{{/* Card Image/Icon Header */}}
|
||||
{{ $header_content := "" }}
|
||||
{{ if $image }}
|
||||
{{ $image_path := strings.TrimPrefix $image "/" }}
|
||||
{{ $img := resources.Get (printf "media/%s" $image_path) }}
|
||||
{{/* Resolve image from page bundle, site assets, remote, or static URLs */}}
|
||||
{{ $image_resource := false }}
|
||||
{{ $image_url := "" }}
|
||||
{{ $image_path := strings.TrimSpace (printf "%v" $image) }}
|
||||
{{ $is_remote := or (strings.HasPrefix $image_path "http://") (strings.HasPrefix $image_path "https://") }}
|
||||
|
||||
{{ if $is_remote }}
|
||||
{{ $remote := try (resources.GetRemote $image_path) }}
|
||||
{{ if and $remote (not $remote.Err) }}
|
||||
{{ $image_resource = $remote.Value }}
|
||||
{{ else }}
|
||||
{{ $image_url = $image_path }}
|
||||
{{ end }}
|
||||
{{ else }}
|
||||
{{ $image_path = strings.TrimPrefix "/" $image_path }}
|
||||
{{ $image_path = strings.TrimPrefix "./" $image_path }}
|
||||
{{ $image_path = strings.TrimPrefix "assets/" $image_path }}
|
||||
{{ $image_path = strings.TrimPrefix "media/" $image_path }}
|
||||
{{ $normalized := path.Clean $image_path }}
|
||||
|
||||
{{ if and $normalized (ne $normalized ".") }}
|
||||
{{ with $page }}
|
||||
{{ $image_resource = (.Resources.ByType "image").GetMatch $normalized }}
|
||||
{{ end }}
|
||||
{{ if not $image_resource }}
|
||||
{{ $image_resource = resources.Get (path.Join "media" $normalized) }}
|
||||
{{ end }}
|
||||
{{ if not $image_resource }}
|
||||
{{ $image_url = printf "/media/%s" $normalized }}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
|
||||
{{ $img := $image_resource }}
|
||||
{{ if $img }}
|
||||
{{ $img = $img.Fill "600x400 Center webp" }}
|
||||
{{ $alt := $name | plainify }}
|
||||
{{ $header_content = printf "<img src=\"%s\" alt=\"%s\" class=\"w-full h-full object-cover opacity-80 group-hover:opacity-100 group-hover:scale-110 transition-all duration-500\">" $img.RelPermalink $alt }}
|
||||
{{ else if $image_url }}
|
||||
{{ $alt := $name | plainify }}
|
||||
{{ $is_external := or (strings.HasPrefix $image_url "http://") (strings.HasPrefix $image_url "https://") }}
|
||||
{{ $src := cond $is_external $image_url ($image_url | relURL) }}
|
||||
{{ $header_content = printf "<img src=\"%s\" alt=\"%s\" class=\"w-full h-full object-cover opacity-80 group-hover:opacity-100 group-hover:scale-110 transition-all duration-500\">" $src $alt }}
|
||||
{{ end }}
|
||||
{{ else if $icon }}
|
||||
{{ $icon_html := partial "functions/get_icon" (dict "name" $icon "attributes" "class=\"w-24 h-24 text-white/80 group-hover:scale-110 transition-transform duration-300\"") }}
|
||||
|
||||
@@ -92,32 +92,74 @@
|
||||
|
||||
{{/* Banner Image */}}
|
||||
{{ $img := "" }}
|
||||
{{/* Resolve image from page bundle, site assets, remote, or static URLs */}}
|
||||
{{ $image_resource := false }}
|
||||
{{ $image_url := "" }}
|
||||
|
||||
{{ if $banner_filename }}
|
||||
{{- $img = resources.Get (path.Join "media" $banner_filename) -}}
|
||||
{{ if $img }}
|
||||
<div class="relative w-full h-64 rounded-2xl overflow-hidden shadow-xl mb-8">
|
||||
{{ if ne $img.MediaType.SubType "gif" }}
|
||||
{{ $responsive := partial "functions/process_responsive_image.html" (dict
|
||||
"image" $img
|
||||
"mode" "fill"
|
||||
"aspect_ratio" "16:9"
|
||||
"sizes" (slice 768 1024 1366 1920 2560)
|
||||
) }}
|
||||
<img class="absolute inset-0 w-full h-full object-cover"
|
||||
srcset="{{ $responsive.srcset }}"
|
||||
sizes="100vw"
|
||||
src="{{ $responsive.fallback.RelPermalink }}"
|
||||
alt="" />
|
||||
{{ else }}
|
||||
{{- $img = $img.Process "webp" -}}
|
||||
<img class="absolute inset-0 w-full h-full object-cover"
|
||||
src="{{$img.RelPermalink}}"
|
||||
alt="" />
|
||||
{{ $image_path := strings.TrimSpace (printf "%v" $banner_filename) }}
|
||||
{{ $is_remote := or (strings.HasPrefix $image_path "http://") (strings.HasPrefix $image_path "https://") }}
|
||||
|
||||
{{ if $is_remote }}
|
||||
{{ $remote := try (resources.GetRemote $image_path) }}
|
||||
{{ if and $remote (not $remote.Err) }}
|
||||
{{ $image_resource = $remote.Value }}
|
||||
{{ else }}
|
||||
{{ $image_url = $image_path }}
|
||||
{{ end }}
|
||||
{{ else }}
|
||||
{{ $image_path = strings.TrimPrefix "/" $image_path }}
|
||||
{{ $image_path = strings.TrimPrefix "./" $image_path }}
|
||||
{{ $image_path = strings.TrimPrefix "assets/" $image_path }}
|
||||
{{ $image_path = strings.TrimPrefix "media/" $image_path }}
|
||||
{{ $normalized := path.Clean $image_path }}
|
||||
|
||||
{{ if and $normalized (ne $normalized ".") }}
|
||||
{{ with $page }}
|
||||
{{ $image_resource = (.Resources.ByType "image").GetMatch $normalized }}
|
||||
{{ end }}
|
||||
</div>
|
||||
{{ if not $image_resource }}
|
||||
{{ $image_resource = resources.Get (path.Join "media" $normalized) }}
|
||||
{{ end }}
|
||||
{{ if not $image_resource }}
|
||||
{{ $image_url = printf "/media/%s" $normalized }}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
|
||||
{{ $img = $image_resource }}
|
||||
|
||||
{{ if $img }}
|
||||
<div class="relative w-full h-64 rounded-2xl overflow-hidden shadow-xl mb-8">
|
||||
{{ if ne $img.MediaType.SubType "gif" }}
|
||||
{{ $responsive := partial "functions/process_responsive_image.html" (dict
|
||||
"image" $img
|
||||
"mode" "fill"
|
||||
"aspect_ratio" "16:9"
|
||||
"sizes" (slice 768 1024 1366 1920 2560)
|
||||
) }}
|
||||
<img class="absolute inset-0 w-full h-full object-cover"
|
||||
srcset="{{ $responsive.srcset }}"
|
||||
sizes="100vw"
|
||||
src="{{ $responsive.fallback.RelPermalink }}"
|
||||
alt="" />
|
||||
{{ else }}
|
||||
{{- $img = $img.Process "webp" -}}
|
||||
<img class="absolute inset-0 w-full h-full object-cover"
|
||||
src="{{$img.RelPermalink}}"
|
||||
alt="" />
|
||||
{{ end }}
|
||||
</div>
|
||||
{{ else if $image_url }}
|
||||
{{ $is_external := or (strings.HasPrefix $image_url "http://") (strings.HasPrefix $image_url "https://") }}
|
||||
<div class="relative w-full h-64 rounded-2xl overflow-hidden shadow-xl mb-8">
|
||||
<img class="absolute inset-0 w-full h-full object-cover"
|
||||
src="{{ if $is_external }}{{ $image_url }}{{ else }}{{ $image_url | relURL }}{{ end }}"
|
||||
alt="" />
|
||||
</div>
|
||||
{{ end }}
|
||||
|
||||
{{/* Avatar */}}
|
||||
{{ $status_icon := "" }}
|
||||
{{ $status := $profile.status }}
|
||||
|
||||
@@ -57,8 +57,52 @@
|
||||
{{ with index $banner "filename" }}{{ $banner_filename = strings.TrimSpace (printf "%v" .) }}{{ end }}
|
||||
{{ end }}
|
||||
{{ if $banner_filename }}
|
||||
{{ $banner_path := strings.TrimPrefix $banner_filename "/" }}
|
||||
{{- $img = resources.Get (path.Join "media" $banner_path) -}}
|
||||
{{ $banner_path := strings.TrimPrefix "/" $banner_filename }}
|
||||
{{/* Resolve image from page bundle, site assets, remote, or static URLs */}}
|
||||
{{ $image_resource := false }}
|
||||
{{ $image_url := "" }}
|
||||
|
||||
{{ if $banner_filename }}
|
||||
{{ $image_path := strings.TrimSpace (printf "%v" $banner_filename) }}
|
||||
{{ $is_remote := or (strings.HasPrefix $image_path "http://") (strings.HasPrefix $image_path "https://") }}
|
||||
|
||||
{{ if $is_remote }}
|
||||
{{ $remote := try (resources.GetRemote $image_path) }}
|
||||
{{ if and $remote (not $remote.Err) }}
|
||||
{{ $image_resource = $remote.Value }}
|
||||
{{ else }}
|
||||
{{/* If remote fetch fails, fall back to the original URL */}}
|
||||
{{ $image_url = $image_path }}
|
||||
{{ end }}
|
||||
{{ else }}
|
||||
{{/* Normalize common prefixes and dot paths */}}
|
||||
{{ $image_path = strings.TrimPrefix "/" $image_path }}
|
||||
{{ $image_path = strings.TrimPrefix "./" $image_path }}
|
||||
{{ $image_path = strings.TrimPrefix "assets/" $image_path }}
|
||||
{{ $image_path = strings.TrimPrefix "media/" $image_path }}
|
||||
{{ $normalized := path.Clean $image_path }}
|
||||
|
||||
{{ if and $normalized (ne $normalized ".") }}
|
||||
{{/* Prefer page bundle image resources */}}
|
||||
{{ with $page }}
|
||||
{{ $image_resource = (.Resources.ByType "image").GetMatch $normalized }}
|
||||
{{ end }}
|
||||
|
||||
{{ if not $image_resource }}
|
||||
{{/* Look in global assets/media - use resources.Get for exact path matching */}}
|
||||
{{ $image_resource = resources.Get (path.Join "media" $normalized) }}
|
||||
{{ end }}
|
||||
|
||||
{{ if not $image_resource }}
|
||||
{{/* Static fallback (serves original size) */}}
|
||||
{{ $image_url = printf "/media/%s" $normalized }}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
|
||||
{{ $img = $image_resource }}
|
||||
|
||||
{{ if $img }}
|
||||
{{ if ne $img.MediaType.SubType "gif" }}
|
||||
{{ $responsive := partial "functions/process_responsive_image.html" (dict
|
||||
@@ -94,6 +138,18 @@
|
||||
</div>
|
||||
</div>
|
||||
{{ end }}
|
||||
{{ else if $image_url }}
|
||||
{{/* Static fallback for remote/unprocessed images */}}
|
||||
{{ $is_external := or (strings.HasPrefix $image_url "http://") (strings.HasPrefix $image_url "https://") }}
|
||||
<div class="w-full bg-gray-200 dark:bg-gray-900 flex flex-wrap items-center justify-center">
|
||||
<div class="w-full bg-white rounded dark:bg-gray-800">
|
||||
<div class="h-2/4 sm:h-64 overflow-hidden w-full">
|
||||
<img class="w-full object-cover"
|
||||
src="{{ if $is_external }}{{ $image_url }}{{ else }}{{ $image_url | relURL }}{{ end }}"
|
||||
alt="" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{{ end }}
|
||||
{{ end }}
|
||||
|
||||
|
||||
Reference in new issue
Block a user