From 1fe36f537464966d7b7346e033eb35979a10b554 Mon Sep 17 00:00:00 2001 From: Federico Agostini <43784886+Agos95@users.noreply.github.com> Date: Fri, 2 Sep 2022 20:58:04 +0200 Subject: [PATCH] feat: load featured image from media library (#2799) Adds support for loading featured image from media library via `image.filename` param as an alternative to placing a `featured.*` image in the page folder. Supports setting a default feature image for all pages via Hugo Cascade in Hugo Config. Supports featured image reuse without duplicating the image within each page folder. --- .../functions/get_featured_image.html | 23 +++++++++++++++++++ .../layouts/partials/jsonld/article.html | 2 +- .../layouts/partials/jsonld/event.html | 2 +- .../layouts/partials/page_header.html | 3 ++- .../wowchemy/layouts/partials/site_head.html | 3 ++- .../wowchemy/layouts/partials/views/card.html | 16 ++++++------- .../layouts/partials/views/compact.html | 12 +++++----- .../layouts/partials/views/masonry.html | 2 +- .../layouts/partials/views/showcase.html | 13 ++++++----- 9 files changed, 51 insertions(+), 25 deletions(-) create mode 100644 modules/wowchemy/layouts/partials/functions/get_featured_image.html diff --git a/modules/wowchemy/layouts/partials/functions/get_featured_image.html b/modules/wowchemy/layouts/partials/functions/get_featured_image.html new file mode 100644 index 00000000..03a02853 --- /dev/null +++ b/modules/wowchemy/layouts/partials/functions/get_featured_image.html @@ -0,0 +1,23 @@ +{{/* 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 post directory + 2. Search for a file `.Params.image.filename` in the post directory + 3. Search for a file `.Params.image.filename` in the `assets/media/` directory +*/}} + +{{/* Search for an image "*featured*" in post folder */}} +{{ $resource := (.Resources.ByType "image").GetMatch "*featured*" }} +{{ if eq $resource nil }} + {{/* Otherwise fall back the image file specified in front matter */}} + {{ $filename := .Params.image.filename }} + {{/* Search in post folder */}} + {{ $resource = (.Resources.ByType "image").GetMatch $filename }} + {{/* Otherwise in `assets/media/` folder */}} + {{ if eq $resource nil }} {{ $resource = resources.GetMatch (path.Join "media" $filename) }} {{ end }} +{{ end }} + +{{ return $resource }} diff --git a/modules/wowchemy/layouts/partials/jsonld/article.html b/modules/wowchemy/layouts/partials/jsonld/article.html index e3c202af..188a0bee 100644 --- a/modules/wowchemy/layouts/partials/jsonld/article.html +++ b/modules/wowchemy/layouts/partials/jsonld/article.html @@ -1,6 +1,6 @@ {{- $page := .page }} {{ $summary := .summary }} -{{ $featured_image := ($page.Resources.ByType "image").GetMatch "*featured*" }} +{{ $featured_image := partial "functions/get_featured_image.html" $page }} {{/* Get schema type. */}} {{ $schema := "Article" }} diff --git a/modules/wowchemy/layouts/partials/jsonld/event.html b/modules/wowchemy/layouts/partials/jsonld/event.html index a8189451..e0add5b8 100644 --- a/modules/wowchemy/layouts/partials/jsonld/event.html +++ b/modules/wowchemy/layouts/partials/jsonld/event.html @@ -1,6 +1,6 @@ {{ $page := .page }} {{ $summary := .summary }} -{{ $featured_image := ($page.Resources.ByType "image").GetMatch "*featured*" }} +{{ $featured_image := partial "funcctions/get_featured_image.html" $page }} {{ $author := partial "functions/get_author_name" $page }}