Skip to content

Commit 25e0c8f

Browse files
committed
fix: resolve image resources correctly with subdirectory baseURLs
This fix handles Hugo's resource resolution issue when deploying to subdirectory paths like GitHub Pages. The resources.Match function was failing because it was looking for images with the full baseURL path instead of the relative path. Changes: - Strip baseURL prefix from imagePath before resource matching - Add fallback logic for resource resolution - Maintain backward compatibility with existing configurations
1 parent 7cdb1d2 commit 25e0c8f

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

layouts/shortcodes/gallery.html

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,46 @@
1818
{{- $useExifCaption := false -}}
1919
{{- $images = .Page.Params.Resources -}} <!-- Fetch the resources defined in the frontmatter -->
2020
{{- else }}
21-
{{- $images = resources.Match (printf "%s/*" $imagePath) -}} <!-- If not "submit", fall back to matching path -->
21+
<!-- Fix for subdirectory baseURL deployments like GitHub Pages -->
22+
{{- $cleanImagePath := $imagePath -}}
23+
24+
<!-- Strip baseURL prefix if it exists in the imagePath -->
25+
{{- $baseURLPath := strings.TrimSuffix "/" (strings.TrimPrefix ($.Site.BaseURL | urls.Parse).Path "/") -}}
26+
{{- if and (ne $baseURLPath "") (hasPrefix $imagePath (printf "/%s/" $baseURLPath)) -}}
27+
{{- $cleanImagePath = strings.TrimPrefix (printf "/%s/" $baseURLPath) $imagePath -}}
28+
{{- else if hasPrefix $imagePath "/" -}}
29+
{{- $cleanImagePath = strings.TrimPrefix "/" $imagePath -}}
30+
{{- end -}}
31+
32+
<!-- Ensure trailing slash for directory matching -->
33+
{{- if not (hasSuffix $cleanImagePath "/") -}}
34+
{{- $cleanImagePath = printf "%s/" $cleanImagePath -}}
35+
{{- end -}}
36+
37+
<!-- Try to match images with cleaned path -->
38+
{{- $images = resources.Match (printf "%s*" $cleanImagePath) -}}
2239
{{- $images = where $images "MediaType.MainType" "image" -}}
40+
41+
<!-- If no images found, try alternative matching patterns -->
42+
{{- if eq (len $images) 0 -}}
43+
<!-- Try without leading slash -->
44+
{{- $altPath := strings.TrimPrefix "/" $imagePath -}}
45+
{{- if not (hasSuffix $altPath "/") -}}
46+
{{- $altPath = printf "%s/" $altPath -}}
47+
{{- end -}}
48+
{{- $images = resources.Match (printf "%s*" $altPath) -}}
49+
{{- $images = where $images "MediaType.MainType" "image" -}}
50+
{{- end -}}
51+
52+
<!-- If still no images found, try the original path -->
53+
{{- if eq (len $images) 0 -}}
54+
{{- $originalPath := $imagePath -}}
55+
{{- if not (hasSuffix $originalPath "/") -}}
56+
{{- $originalPath = printf "%s/" $originalPath -}}
57+
{{- end -}}
58+
{{- $images = resources.Match (printf "%s*" $originalPath) -}}
59+
{{- $images = where $images "MediaType.MainType" "image" -}}
60+
{{- end -}}
2361
{{- end }}
2462

2563
{{- if $useRandomImageStack -}}
@@ -127,4 +165,4 @@
127165
</div>
128166
{{ end }}
129167
{{- end }}
130-
</div>
168+
</div>

0 commit comments

Comments
 (0)