feat(helper/image): customize field used to get featured image

This commit is contained in:
Jimmy Cai 2020-09-11 17:54:16 +02:00
parent a2662603df
commit 2acd380e98
No known key found for this signature in database
GPG Key ID: 3EA408E527F37B18
2 changed files with 9 additions and 7 deletions

View File

@ -13,6 +13,7 @@ DefaultContentLanguage = "en" # Theme i18n support
[params]
mainSections = ["post"]
favicon = ""
featuredImageField = "image"
[params.dateFormat]
published = "Jan 02, 2006"
lastUpdated = "Jan 02, 2006 15:04 MST"

View File

@ -1,17 +1,18 @@
{{ $result := dict "exists" false "permalink" nil "resource" nil "isDefault" false }}
{{ $imageField := .Context.Params.image }}
{{ $imageField := default "image" .Context.Site.Params.featuredImageField }}
{{ $imageValue := index .Context.Params $imageField }}
{{ if $imageField }}
{{ if $imageValue }}
<!-- If page has `image` field set -->
{{ $result = merge $result (dict "exists" true) }}
{{ $url := urls.Parse $imageField }}
{{ $url := urls.Parse $imageValue }}
{{ if or (eq $url.Scheme "http") (eq $url.Scheme "https") }}
<!-- Is a external image -->
{{ $result = merge $result (dict "permalink" $imageField) }}
{{ $result = merge $result (dict "permalink" $imageValue) }}
{{ else }}
{{ $pageResourceImage := .Context.Resources.GetMatch (printf "%s" ($imageField | safeURL)) }}
{{ $siteResourceImage := resources.GetMatch (printf "%s" ($imageField | safeURL)) }}
{{ $pageResourceImage := .Context.Resources.GetMatch (printf "%s" ($imageValue | safeURL)) }}
{{ $siteResourceImage := resources.GetMatch (printf "%s" ($imageValue | safeURL)) }}
{{ if $pageResourceImage }}
<!-- If image is found under page bundle -->
@ -23,7 +24,7 @@
{{ $result = merge $result (dict "resource" $siteResourceImage) }}
{{ else }}
<!-- Can not find the image -->
{{ errorf "Failed loading image: %q" $imageField }}
{{ errorf "Failed loading image: %q" $imageValue }}
{{ $result = merge $result (dict "exists" false) }}
{{ end }}
{{ end }}