Skip to content

Commit 8280b10

Browse files
committed
fix: simplify resource resolution for subdirectory baseURL deployments
The previous complex fix had bugs in the path parsing logic. This simpler approach directly handles the common case where Hugo tries to resolve resources with the baseURL path prefix, which fails with resources.Match. This fix: - Directly strips known problematic prefixes - Uses a cleaner fallback approach - Adds debug information to help identify issues
1 parent 361fd8c commit 8280b10

File tree

1 file changed

+11
-19
lines changed

1 file changed

+11
-19
lines changed

layouts/shortcodes/gallery.html

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@
2121
<!-- Fix for subdirectory baseURL deployments like GitHub Pages -->
2222
{{- $cleanImagePath := $imagePath -}}
2323

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 -}}
24+
<!-- Remove common problematic prefixes that cause resources.Match to fail -->
25+
{{- if hasPrefix $imagePath "/hinode-mod-image-lightbox-gallery/" -}}
26+
{{- $cleanImagePath = strings.TrimPrefix "/hinode-mod-image-lightbox-gallery/" $imagePath -}}
2827
{{- else if hasPrefix $imagePath "/" -}}
2928
{{- $cleanImagePath = strings.TrimPrefix "/" $imagePath -}}
3029
{{- end -}}
@@ -38,25 +37,18 @@
3837
{{- $images = resources.Match (printf "%s*" $cleanImagePath) -}}
3938
{{- $images = where $images "MediaType.MainType" "image" -}}
4039

41-
<!-- If no images found, try alternative matching patterns -->
40+
<!-- Debug: Add warning if no images found to help troubleshoot -->
4241
{{- 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 -}}
42+
{{- warnf "Gallery: No images found for path '%s' (cleaned from '%s'). Available resources pattern would be: '%s*'" $cleanImagePath $imagePath $cleanImagePath -}}
43+
44+
<!-- Try alternative patterns -->
45+
{{- $altPath := "img/tests/" -}}
4846
{{- $images = resources.Match (printf "%s*" $altPath) -}}
4947
{{- $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 -}}
48+
49+
{{- if gt (len $images) 0 -}}
50+
{{- warnf "Gallery: Found %d images using fallback path '%s'" (len $images) $altPath -}}
5751
{{- end -}}
58-
{{- $images = resources.Match (printf "%s*" $originalPath) -}}
59-
{{- $images = where $images "MediaType.MainType" "image" -}}
6052
{{- end -}}
6153
{{- end }}
6254

0 commit comments

Comments
 (0)