Skip to content

Commit

Permalink
feat: load featured image from media library (#2799)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Agos95 authored Sep 2, 2022
1 parent 56994f7 commit 1fe36f5
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -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 }}
2 changes: 1 addition & 1 deletion modules/wowchemy/layouts/partials/jsonld/article.html
Original file line number Diff line number Diff line change
@@ -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" }}
Expand Down
2 changes: 1 addition & 1 deletion modules/wowchemy/layouts/partials/jsonld/event.html
Original file line number Diff line number Diff line change
@@ -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 }}

<script type="application/ld+json">
Expand Down
3 changes: 2 additions & 1 deletion modules/wowchemy/layouts/partials/page_header.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{{ $page := . }}
{{ $featured := (.Resources.ByType "image").GetMatch "*featured*" }}
{{ $featured := partial "functions/get_featured_image.html" $page }}

{{ $anchor := $page.Params.image.focal_point | default "Smart" }}

{{/* Set default titles for node pages */}}
Expand Down
3 changes: 2 additions & 1 deletion modules/wowchemy/layouts/partials/site_head.html
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@

{{/* Get page image for sharing. */}}
{{ $sharing_image := resources.GetMatch (path.Join "media" "sharing.*") }}
{{ $featured_image := (.Resources.ByType "image").GetMatch "*featured*" }}
{{/* Search for an image "*featured*" in post folder */}}
{{ $featured_image := partial "functions/get_featured_image.html" . }}
{{ $avatar_image := (.Resources.ByType "image").GetMatch "avatar*" }}
{{ $has_logo := fileExists "assets/media/logo.png" | or (fileExists "assets/media/logo.svg") }}
{{ $og_image := "" }}
Expand Down
16 changes: 8 additions & 8 deletions modules/wowchemy/layouts/partials/views/card.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@
{{ partial "page_metadata" (dict "page" $item "is_list" 1) }}
{{ end }}

{{ $resource := ($item.Resources.ByType "image").GetMatch "*featured*" }}
{{ $resource := partial "functions/get_featured_image.html" $item }}
{{ $anchor := $item.Params.image.focal_point | default "Smart" }}
{{ with $resource }}
{{ $image := .Fill (printf "808x455 webp %s" $anchor) }}
<a href="{{ $link }}" {{ $target | safeHTMLAttr }}>
<div class="img-hover-zoom">
<img src="{{ $image.RelPermalink }}" height="{{ $image.Height }}" width="{{ $image.Width }}"
class="article-banner" alt="{{ $item.Title }}" loading="lazy">
</div>
</a>
{{ $image := .Fill (printf "808x455 webp %s" $anchor) }}
<a href="{{ $link }}" {{ $target | safeHTMLAttr }}>
<div class="img-hover-zoom">
<img src="{{ $image.RelPermalink }}" height="{{ $image.Height }}" width="{{ $image.Width }}"
class="article-banner" alt="{{ $item.Title }}" loading="lazy">
</div>
</a>
{{end}}

<div class="section-subheading article-title mb-1 mt-3">
Expand Down
12 changes: 6 additions & 6 deletions modules/wowchemy/layouts/partials/views/compact.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@

</div>
<div class="ml-3">
{{ $resource := ($item.Resources.ByType "image").GetMatch "*featured*" }}
{{ $resource := partial "functions/get_featured_image.html" $item }}
{{ with $resource }}
{{ $image := .Resize "150x webp" }}
<a href="{{$link}}" {{ $target | safeHTMLAttr }}>
<img src="{{ $image.RelPermalink }}" height="{{ $image.Height }}" width="{{ $image.Width }}"
alt="{{ $item.Title }}" loading="lazy">
</a>
{{ $image := .Resize "150x webp" }}
<a href="{{$link}}" {{ $target | safeHTMLAttr }}>
<img src="{{ $image.RelPermalink }}" height="{{ $image.Height }}" width="{{ $image.Width }}"
alt="{{ $item.Title }}" loading="lazy">
</a>
{{end}}
</div>
</div>
2 changes: 1 addition & 1 deletion modules/wowchemy/layouts/partials/views/masonry.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
{{ $summary = $item.Summary }}
{{ end }}

{{ $resource := ($item.Resources.ByType "image").GetMatch "*featured*" }}
{{ $resource := partial "functions/get_featured_image.html" $item }}

<div class="card">
{{ with $resource }}
Expand Down
13 changes: 7 additions & 6 deletions modules/wowchemy/layouts/partials/views/showcase.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@

</div>
<div class="col-12 col-md-6 order-first {{$order}}">
{{ $resource := ($item.Resources.ByType "image").GetMatch "*featured*" }}
{{ $resource := partial "functions/get_featured_image.html" $item }}

{{ with $resource }}
{{ $image := .Resize "540x webp" }}
{{if $do_link}}<a href="{{ $link }}" {{ $target | safeHTMLAttr }}>{{end}}
<img src="{{ $image.RelPermalink }}" height="{{ $image.Height }}" width="{{ $image.Width }}"
alt="{{ $item.Title }}" loading="lazy">
{{if $do_link}}</a>{{end}}
{{ $image := .Resize "540x webp" }}
{{if $do_link}}<a href="{{ $link }}" {{ $target | safeHTMLAttr }}>{{end}}
<img src="{{ $image.RelPermalink }}" height="{{ $image.Height }}" width="{{ $image.Width }}"
alt="{{ $item.Title }}" loading="lazy">
{{if $do_link}}</a>{{end}}
{{end}}
</div>
</div>
Expand Down

1 comment on commit 1fe36f5

@ShoGinn
Copy link

@ShoGinn ShoGinn commented on 1fe36f5 Sep 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is huge @Agos95! Thanks a ton for doing this!

I have a friend who keeps forgetting to change the name of the file!

Please sign in to comment.