Support global resources in figure shortcode (#362)

This commit is contained in:
Nedjo Rogers 2022-04-16 21:31:39 -07:00
parent 9774c78a2e
commit bf7a6dc2fa
3 changed files with 45 additions and 25 deletions

View File

@ -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" }}

View File

@ -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 }}

View File

@ -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" -}}
<figure{{ with $class }} class="{{ . }}"{{ end }}>
{{- if .Get "link" -}}
<a href="{{ .Get "src" }}"{{ with .Get "target" }} target="{{ . }}"{{ end }}{{ with .Get "rel" }} rel="{{ . }}"{{ end }}>