From 5001b28387cc9bc7ae0e592856d90ac53a8328e3 Mon Sep 17 00:00:00 2001 From: Sean Date: Tue, 23 Jun 2020 06:34:09 -0700 Subject: [PATCH] Add automatic cover image support (#303) * Add support for auto-detection of featured images. * Rename Partial, Add Documentation Renamed the partial to func/GetFeaturedImage.html. Additionally added more documentation in the partial to explain how it worked, and what values were returned. Co-authored-by: Sean Zimmermann --- layouts/partials/func/GetFeaturedImage.html | 35 +++++++++++++++++++++ layouts/partials/page-header.html | 2 +- layouts/partials/summary-with-image.html | 6 ++-- layouts/post/summary-with-image.html | 7 +++-- 4 files changed, 43 insertions(+), 7 deletions(-) create mode 100644 layouts/partials/func/GetFeaturedImage.html diff --git a/layouts/partials/func/GetFeaturedImage.html b/layouts/partials/func/GetFeaturedImage.html new file mode 100644 index 0000000..c00b086 --- /dev/null +++ b/layouts/partials/func/GetFeaturedImage.html @@ -0,0 +1,35 @@ +{{/* + GetFeaturedImage + + This partial gets the url for featured image for a given page. + + If a featured_image was set in the page's front matter, then that will be used. + + If not set, this will search page resources to find an image that contains the word + "cover", and if found, returns the path to that resource. + + If no featured_image was set, and there's no "cover" image in page resources, then + this partial returns an empty string (which evaluates to false). + + @return Permalink to featured image, or an empty string if not found. + +*/}} + +{{/* Declare a new string variable, $linkToCover */}} +{{ $linkToCover := "" }} + +{{/* Use the value from front matter if present */}} +{{ if .Params.featured_image }} + {{ $linkToCover = .Params.featured_image }} + +{{/* Find the first image with 'cover' in the name in this page bundle. */}} +{{ else }} + {{ $img := (.Resources.ByType "image").GetMatch "*cover*" }} + {{ with $img }} + {{ $linkToCover = .Permalink }} + {{ end }} +{{ end }} + +{{/* return either a permalink, or an empty string. Note that partials can only have a single +return statement, so this needs to be at the end of the partial (and not in the if block) */}} +{{ return $linkToCover }} \ No newline at end of file diff --git a/layouts/partials/page-header.html b/layouts/partials/page-header.html index 8cd6d4f..9f2ebd5 100644 --- a/layouts/partials/page-header.html +++ b/layouts/partials/page-header.html @@ -1,4 +1,4 @@ -{{ $featured_image := .Params.featured_image }} +{{ $featured_image := partial "func/GetFeaturedImage.html" . }} {{ if $featured_image }} {{/* Trimming the slash and adding absURL make sure the image works no matter where our site lives */}} {{ $featured_image := (trim $featured_image "/") | absURL }} diff --git a/layouts/partials/summary-with-image.html b/layouts/partials/summary-with-image.html index 962981f..702a305 100644 --- a/layouts/partials/summary-with-image.html +++ b/layouts/partials/summary-with-image.html @@ -1,8 +1,8 @@ -{{ $featured_image := .Params.featured_image }} +{{ $featured_image := partial "func/GetFeaturedImage.html" . }}