-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
Showing
4 changed files
with
43 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters