Files
hugo-theme-stack/layouts/_partials/helper/image.html
T

53 lines
1.5 KiB
HTML
Raw Normal View History

/// Params:
/// Resources: Where to search for images
/// Image: Image URL
/// Returns:
/// Local: true if the image is local, false if it is remote
/// Resource: Hugo image resource object, not nil if the image is local
/// Permalink: Image URL
/// Height: Image height
/// Width: Image width
2020-09-11 18:13:39 +02:00
2026-01-30 15:07:14 +01:00
{{ $result := "" }}
2026-01-30 15:07:14 +01:00
{{ if .Image }}
{{ $url := urls.Parse .Image }}
2026-01-30 15:07:14 +01:00
{{ $resource := "" }}
{{ if not (in (slice "https" "http") $url.Scheme) }}
2026-01-30 15:07:14 +01:00
{{ $resource = .Resources.Get (printf "%s" (.Image | safeURL)) }}
{{ else }}
{{ with try (resources.GetRemote $url) }}
{{ with .Err }}
{{ errorf "%s" . }}
{{ else with .Value }}
{{ $resource = . }}
{{ else }}
{{ errorf "Unable to get remote resource %q" $url }}
{{ end }}
{{ end }}
{{ end }}
{{ if $resource }}
{{- $isSVG := eq $resource.MediaType.SubType "svg" -}}
{{- if $isSVG -}}
{{ $result = dict
2026-01-30 15:07:14 +01:00
"Local" true
"Resource" $resource
"Permalink" $resource.RelPermalink
"Height" nil
"Width" nil
}}
{{- else -}}
{{- $result = dict
"Local" true
"Resource" $resource
"Permalink" $resource.RelPermalink
"Height" $resource.Height
"Width" $resource.Width
}}
2026-01-30 15:07:14 +01:00
{{- end -}}
{{ end }}
2020-09-10 18:53:36 +02:00
{{ end }}
{{ return $result }}