feat: load gallery from media dir & add gallery display options

Add gallery `order` with options "asc" or "desc"

Add gallery `resize_options` which supports Hugo image processing options.

BREAKING CHANGE:
Move gallery album folders to `assets/media/albums/`. Album name should be defined in shortcode as `album`.

Fix #2245
This commit is contained in:
George Cushen committed 2021-05-31 12:21:56 +01:00
1 parent 81ba175229
commit bbca6dea78
3 files changed
+31 -19

No files matched your search

+20 -2
View File
@@ -89,8 +89,26 @@
{{ end }}
{{ printf "<link rel=\"stylesheet\" href=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\">" (printf $css.fontAwesome.url $css.fontAwesome.version) $css.fontAwesome.sri | safeHTML }}
{{/* Workaround `.HasShortcode "gallery"` detection issue on `home/` WidgetPage v1 */}}
{{ if .HasShortcode "gallery" | or (and .IsHome (site.Params.require_fancybox | default true)) }}
{{/* Workaround `.HasShortcode "gallery"` not parsing page resources (widget page sections) */}}
{{ $has_gallery := false }}
{{/* Note: unless there is a root `/index.md` with `type: widget_page`, Hugo treats homepage as `page` type. */}}
{{ if (eq .Type "widget_page") | or (and .IsHome (eq .Type "page")) }}
{{/* Check if widget page sections use a gallery */}}
{{ $page := "/home/index.md" }}
{{ $context := cond .IsHome (site.GetPage $page) . }}
{{ range $context.Resources.ByType "page" }}
{{ if .HasShortcode "gallery" }}
{{ $has_gallery = true }}
{{ end }}
{{ end }}
{{ else }}
{{/* Single page */}}
{{ if .HasShortcode "gallery" }}
{{ $has_gallery = true }}
{{ end }}
{{ end }}
{{ $scr.Set "HasNestedGalleryShortcode" $has_gallery }}
{{ if $has_gallery | or site.Params.require_fancybox }}
{{ printf "<link rel=\"stylesheet\" href=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\" media=\"print\" onload=\"this.media='all'\">" (printf $css.fancybox.url $css.fancybox.version) $css.fancybox.sri | safeHTML }}
{{ end }}
+1 -1
View File
@@ -15,7 +15,7 @@
{{ end }}
{{/* Workaround `.HasShortcode "gallery"` detection issue on `home/` WidgetPage v1 */}}
{{ if .HasShortcode "gallery" | or (and .IsHome (site.Params.require_fancybox | default true)) }}
{{ if ($scr.Get "HasNestedGalleryShortcode") | or site.Params.require_fancybox }}
{{ printf "<script src=\"%s\" integrity=\"%s\" crossorigin=\"anonymous\"></script>" (printf $js.fancybox.url $js.fancybox.version) $js.fancybox.sri | safeHTML }}
{{ end }}
+10 -16
View File
@@ -1,26 +1,20 @@
{{/* Gallery Shortcode for Wowchemy. */}}
{{/* Load gallery images from page dir. */}}
{{/* Load gallery images from media library. */}}
{{/* Get album folder or default to `gallery/`. */}}
{{/* Get album folder or default to `media/albums/gallery/`. */}}
{{ $album := (.Get "album") | default "gallery" }}
{{ $album_path := path.Join "media" "albums" $album "*" }}
{{/* Set image path and page bundle that images are associated with. */}}
{{ $album_path := "" }}
{{ $resource_page := "" }}
{{ if eq .Page.Parent.Type "widget_page" }}
{{ $album_path = printf "%s/%s/*" (path.Base (path.Split .Page.Path).Dir) $album }}
{{ $resource_page = $.Page.Parent }}
{{ else }}
{{ $album_path = printf "%s/*" $album }}
{{ $resource_page = $.Page }}
{{ end }}
{{/* Gallery options */}}
{{ $sort_order := .Get "order" | default "asc" }}
{{ $resize_options := .Get "resize_options" | default "x190" }}
<div class="gallery">
{{/* Attempt to automatically load gallery images from page bundle */}}
{{ $images := ($resource_page.Resources.ByType "image").Match $album_path }}
{{ range $images }}
{{ $image := .Resize "x190" }}
{{ $images := resources.Match $album_path }}
{{ range (sort $images "Name" $sort_order) }}
{{ $image := .Resize $resize_options }}
{{/* Check if the user set a caption for this image */}}
{{ $filename := path.Base .Name }}
{{ $caption := "" }}
@@ -33,7 +27,7 @@
<img src="{{ $image.RelPermalink }}" loading="lazy" alt="{{ plainify $caption | default $filename }}" width="{{ $image.Width }}" height="{{ $image.Height }}">{{/* Width & height (or low res src) required for lazy loading. */}}
</a>
{{else}}
{{ errorf "Unable to load gallery in %s." .Page.File.Filename }}
{{ errorf "Unable to load gallery `%s` in `%s`." $album_path .Page.File.Filename }}
{{end}}
</div>