liwenyip / hugo-easy-gallery

Automagical css image gallery in Hugo using shortcodes, with optional lightbox/carousel gadget using PhotoSwipe and jQuery.
MIT License
573 stars 111 forks source link

hugo-easy-gallery

2022-02-28 - Help wanted: Unfortunately I am no longer able to maintain this project myself (I have a busy day job that has nothing to do with web development or computer coding, and any spare time is taken up with family + other committments) so I am looking for collaborators to help maintain this project i.e. review and merge pull requests and help with issues. Thanks to Kishor Bhat and Emiel Hollander who have already generously volunteered. We could use a few more collaborators - please email me if you're interested :-)

Automagical css image gallery in Hugo using shortcodes, with optional lightbox/carousel gadget using PhotoSwipe and jQuery.

New: Create a gallery of all images in a directory with just one line of shortcode, see demo.

Need help?

Fixed an issue or made an improvement?

Demo

Image Gallery Features

PhotoSwipe Features

Installation

Put files in following places:

NB load-photoswipe.html loads jQuery from cdnjs.cloudflare.com.

If you want, you could (depending on a front matter param) conditionally load load-photoswipe.html or its contents from the footer of your template. But I've consciously chosen to load PhotoSwipe using a shortcode so that you don't have to modify your template if you don't want to.

Theme integration

Put files in the relevant folders within your theme.

Delete /layouts/shortcodes/load-photoswipe.html.

Rename /layouts/shortcodes/load-photoswipe-theme.html to /layouts/shortcodes/load-photoswipe.html.

Add the following lines to the footer of your template, just before </body>:

(Omit the jQuery line if jQuery is already loaded elsewhere in your template; just make sure that jQuery is loaded before this code.)

<!-- Load PhotoSwipe js if the load-photoswipe shortcode has been used -->
{{ if ($.Scratch.Get "photoswipeloaded") }}
<script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>
<script src="https://github.com/liwenyip/hugo-easy-gallery/raw/master/js/load-photoswipe.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.1/photoswipe.min.js" integrity="sha256-UplRCs9v4KXVJvVY+p+RSo5Q4ilAUXh7kpjyIP5odyc=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/photoswipe/4.1.1/photoswipe-ui-default.min.js" integrity="sha256-PWHOlUzc96pMc8ThwRIXPn8yH4NOLu42RQ0b9SpnpFk=" crossorigin="anonymous"></script>
{{ end }}

{{< figure >}} shortcode usage

Specifying your image files:

Optional parameters:

Optional parameters for standalone {{< figure >}} shortcodes only (i.e. don't use on {{< figure >}} inside {{< gallery >}} - strange things may happen if you do):

{{< gallery >}} shortcode usage

To specify a directory of image files:

{{< gallery dir="/img/your-directory-of-images/" />}}

To specify individual image files:

{{< gallery >}}
  {{< figure src="https://github.com/liwenyip/hugo-easy-gallery/raw/master/image1.jpg" >}}
  {{< figure src="https://github.com/liwenyip/hugo-easy-gallery/raw/master/image2.jpg" >}}
  {{< figure src="https://github.com/liwenyip/hugo-easy-gallery/raw/master/image3.jpg" >}}
{{< /gallery >}}

Optional parameters:

PhotoSwipe usage

You can optionally have different captions on page vs in PhotoSwipe:

CSS Hackers

hugo-easy-gallery.css is designed to provide square tiles in a container with max-width: 768px.

Here are some pointers if you want to adapt the CSS:

Issues

I've tested this with the beautifulhugo theme. If things don't work properly with other themes, raise an issue on GitHub, or even better fix the issue and submit a pull request :-)

Credits

These blog posts helped me immensely: