diff --git a/layouts/partials/func/GetFeaturedImage.html b/layouts/partials/func/GetFeaturedImage.html index bb3b6ba..0b200d4 100644 --- a/layouts/partials/func/GetFeaturedImage.html +++ b/layouts/partials/func/GetFeaturedImage.html @@ -11,7 +11,7 @@ If no featured_image was set, and there's no "cover" image in page resources, then this partial returns false. - @return RelPermalink to featured image, or an empty string if not found. + @return a featured image resource, or false if not found. */}} @@ -19,24 +19,8 @@ {{ $coverImage := false }} {{ $matches := "feature,cover" }} {{/* Use the value from front matter if present */}} -{{ with .Params.featured_image }} - {{/* If we find a remote global Resource matching the exact value, we use it. */}} - {{ if (hasPrefix . "http") }} - {{ with resources.GetRemote . }} - {{ $coverImage = . }} - {{ end }} - {{ else }} - {{/* If we find a local global Resource matching the exact value, we use it. */}} - {{/* Matching uses relative paths so strip a leading / character if present. */}} - {{ with resources.GetMatch (trim . "/") }} - {{ $coverImage = . }} - {{ end }} - {{ end }} - {{/* If we find a Page Resource matching the exact value, we use it instead. */}} - {{/* Matching uses relative paths so strip a leading / character if present. */}} - {{ with $.Resources.GetMatch (trim . "/") }} - {{ $coverImage = . }} - {{ end }} +{{ if .Params.featured_image }} + {{ $coverImage = partial "func/GetImageResource" (dict "src" .Params.featured_image "pageResources" .Resources) }} {{/* Find the first image with 'cover' in the name in this page bundle. */}} {{ else }} {{ with .Resources.ByType "image" }} diff --git a/layouts/partials/func/GetImageResource b/layouts/partials/func/GetImageResource new file mode 100644 index 0000000..0c5a724 --- /dev/null +++ b/layouts/partials/func/GetImageResource @@ -0,0 +1,35 @@ +{{/* + GetImageResource + + This partial gets an image resource for a given string. + + A matching resource is returned if found in the following search order: + + * A page resource. + * If the string begins with "http", a remote global resource. + * Otherwise, a local global resource. + + If no matching resource is found, this partial returns false. + + @return an image resource, or false if not found. + +*/}} +{{ $imageResource := false }} +{{/* If we find a remote global Resource matching the exact value, we use it. */}} +{{ if (hasPrefix $.src "http") }} + {{ with resources.GetRemote $.src }} + {{ $imageResource = . }} + {{ end }} +{{ else }} + {{/* If we find a local global Resource matching the exact value, we use it. */}} + {{/* Matching uses relative paths so strip a leading / character if present. */}} + {{ with resources.GetMatch (trim $.src "/") }} + {{ $imageResource = . }} + {{ end }} +{{ end }} +{{/* If we find a Page Resource matching the exact value, we use it instead. */}} +{{/* Matching uses relative paths so strip a leading / character if present. */}} +{{ with $.pageResources.GetMatch (trim $.src "/") }} + {{ $imageResource = . }} +{{ end }} +{{ return $imageResource }} diff --git a/layouts/shortcodes/figure.html b/layouts/shortcodes/figure.html index e706af8..49eb159 100644 --- a/layouts/shortcodes/figure.html +++ b/layouts/shortcodes/figure.html @@ -1,11 +1,12 @@ -{{ $srcset := slice }} -{{/* Matching uses relative paths so strip a leading / character if present. */}} -{{- with .Page.Resources.GetMatch (trim (.Get "src") "/") -}} +{{- $srcset := slice -}} +{{- /* Matching uses relative paths so strip a leading / character if present. */ -}} +{{- $image := partial "func/GetImageResource" (dict "src" (.Get "src") "pageResources" .Page.Resources) -}} +{{- with $image -}} {{- /* Standard width of main is 70% (w-70-ns) */ -}} {{- $target_width := 0.70 -}} - {{ $srcset = partial "func/GetImageSrcset" (dict "original" . "target_width" $target_width) }} -{{ end }} -{{ $class := .Get "class" | default "mh0" }} + {{- $srcset = partial "func/GetImageSrcset" (dict "original" . "target_width" $target_width) -}} +{{- end -}} +{{- $class := .Get "class" | default "mh0" -}} {{- if .Get "link" -}}