{{/* Function to retrieve the featured image */}}
{{/* Inputs: page context */}}
{{/* Output: image resource, or nil if not found */}}

{{/*
    Featured image is searched in this order:
      1. Search for a file `*featured*` in the page directory
      2. Search for a file `.Params.image.filename` in the page directory
      3. Search for a file `.Params.image.filename` in the `assets/media/` directory
*/}}

{{/* Search for an image "*featured*" in page folder */}}
{{ $resource := (.Resources.ByType "image").GetMatch "*featured*" }}
{{ if not (reflect.IsResource $resource) }}
  {{/* Otherwise fall back the image file specified in front matter */}}
  {{ $filename := "" }}
  {{ if and .Params.image (reflect.IsMap .Params.image) }}
    {{ $filename = .Params.image.filename }}
  {{ end }}
  {{/* Search in page folder */}}
  {{ $resource = (.Resources.ByType "image").GetMatch $filename }}
  {{/* Otherwise in `assets/media/` folder */}}
  {{ if not (reflect.IsResource $resource) }} {{ $resource = resources.GetMatch (path.Join "media" $filename) }} {{ end }}
{{ end }}

{{ return $resource }}
