blog

Log | Files | Refs

get-page-images.html (1434B)


      1 {{- $imgs := slice }}
      2 {{- $imgParams := .Params.images }}
      3 {{- $resources := .Resources.ByType "image" -}}
      4 {{/* Find featured image resources if the images parameter is empty. */}}
      5 {{- if not $imgParams }}
      6   {{- $featured := $resources.GetMatch "*feature*" -}}
      7   {{- if not $featured }}{{ $featured = $resources.GetMatch "{*cover*,*thumbnail*}" }}{{ end -}}
      8   {{- with $featured }}
      9     {{- $imgs = $imgs | append (dict
     10       "Image" .
     11       "RelPermalink" .RelPermalink
     12       "Permalink" .Permalink) }}
     13   {{- end }}
     14 {{- end }}
     15 {{/* Use the first one of site images as the fallback. */}}
     16 {{- if and (not $imgParams) (not $imgs) }}
     17   {{- with site.Params.images }}
     18     {{- $imgParams = first 1 . }}
     19   {{- end }}
     20 {{- end }}
     21 {{/* Parse page's images parameter. */}}
     22 {{- range $imgParams }}
     23   {{- $img := . }}
     24   {{- $url := urls.Parse $img }}
     25   {{- if eq $url.Scheme "" }}
     26     {{/* Internal image. */}}
     27     {{- with $resources.GetMatch $img -}}
     28       {{/* Image resource. */}}
     29       {{- $imgs = $imgs | append (dict
     30         "Image" .
     31         "RelPermalink" .RelPermalink
     32         "Permalink" .Permalink) }}
     33     {{- else }}
     34       {{- $imgs = $imgs | append (dict
     35         "RelPermalink" (relURL $img)
     36         "Permalink" (absURL $img)
     37       ) }}
     38     {{- end }}
     39   {{- else }}
     40     {{/* External image */}}
     41     {{- $imgs = $imgs | append (dict
     42       "RelPermalink" $img
     43       "Permalink" $img
     44     ) }}
     45   {{- end }}
     46 {{- end }}
     47 {{- return $imgs }}