image.html 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. {{ $result := dict "exists" false "permalink" nil "resource" nil "isDefault" false }}
  2. {{ $imageField := default "image" .Context.Site.Params.featuredImageField }}
  3. {{ $imageValue := index .Context.Params $imageField }}
  4. {{ if $imageValue }}
  5. <!-- If page has `image` field set -->
  6. {{ $result = merge $result (dict "exists" true) }}
  7. {{ $url := urls.Parse $imageValue }}
  8. {{ if or (eq $url.Scheme "http") (eq $url.Scheme "https") }}
  9. <!-- Is an external image -->
  10. {{ $result = merge $result (dict "permalink" $imageValue) }}
  11. {{ else }}
  12. {{ $pageResourceImage := .Context.Resources.GetMatch (printf "%s" ($imageValue | safeURL)) }}
  13. {{ if $pageResourceImage }}
  14. <!-- If image is found under page bundle -->
  15. {{ $result = merge $result (dict "permalink" $pageResourceImage.RelPermalink) }}
  16. <!-- Disable SVG image processing, not supported by Hugo -->
  17. {{ if ne (path.Ext $imageValue) ".svg" }}
  18. {{ $result = merge $result (dict "resource" $pageResourceImage) }}
  19. {{ end }}
  20. {{ else }}
  21. <!-- Can not find the image under page bundle. Could be a relative linked image -->
  22. {{ $result = merge $result (dict "permalink" (relURL $imageValue)) }}
  23. {{ end }}
  24. {{ end }}
  25. {{ else if and (ne .Type nil) (index .Context.Site.Params.defaultImage .Type) }}
  26. <!-- Type arg is set, check for defaultImage setting -->
  27. {{ $defaultImageSetting := index .Context.Site.Params.defaultImage .Type }}
  28. {{ if $defaultImageSetting.enabled }}
  29. {{ $result = merge $result (dict "isDefault" true) }}
  30. {{ $result = merge $result (dict "exists" true) }}
  31. {{ if $defaultImageSetting.local }}
  32. {{ $siteResourceImage := resources.GetMatch (printf "%s" ($defaultImageSetting.src | safeURL)) }}
  33. {{ if $siteResourceImage }}
  34. <!-- Try search image under site's assets folder -->
  35. {{ $result = merge $result (dict "permalink" $siteResourceImage.RelPermalink) }}
  36. {{ $result = merge $result (dict "resource" $siteResourceImage) }}
  37. {{ else }}
  38. <!-- Can not find the image -->
  39. {{ errorf "Failed loading image: %q" $defaultImageSetting.src }}
  40. {{ $result = merge $result (dict "exists" false) }}
  41. {{ end }}
  42. {{ else }}
  43. <!-- External image -->
  44. {{ $result = merge $result (dict "permalink" (relURL $defaultImageSetting.src)) }}
  45. {{ end }}
  46. {{ end }}
  47. {{ end }}
  48. {{ return $result }}