Created
May 2, 2023 16:27
-
-
Save alexbevi/0c2afeec56b8ea0077127209d1b95e16 to your computer and use it in GitHub Desktop.
lightGallery inline gallery plugin for Jekyll
This file contains hidden or 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
# Title: LightGallery Tag for Jekyll | |
# Authors: Alex Bevilacqua <[email protected]> | |
# Description: incorporate the LightGallery inline JavaScript gallery | |
# | |
# Adaption of "Photos tag for Jekyll" by Devin Weaver, and the derived | |
# "Gallery Tag for Jekyll" by Kevin Brown. | |
# | |
# Installation: | |
# | |
# {% gallery_includes %} | |
# | |
# This macro should be added to the `<HEAD>` of your template(s) | |
# in order to load the libraries and stylesheets needed by LightGallery | |
# | |
# Usage: | |
# | |
# {% gallery %} | |
# photo1.jpg | |
# /path/to/photos/photo2.jpg | |
# {% endgallery %} | |
# | |
module Jekyll | |
class PhotosUtil | |
def initialize(context) | |
@context = context | |
end | |
def path_for(filename) | |
filename = filename.strip | |
prefix = (@context.environments.first['site']['baseurl'] unless filename =~ /^(?:\/|http)/i) || "" | |
"#{prefix}#{filename}" | |
end | |
end | |
class LightGalleryScriptIncludePatch < Liquid::Tag | |
def render(context) | |
# https://cdnjs.com/libraries/lightgallery | |
return <<-eof | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/lightgallery/2.7.1/lightgallery.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/lightgallery/2.7.1/plugins/zoom/lg-zoom.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/lightgallery/2.7.1/plugins/thumbnail/lg-thumbnail.min.js"></script> | |
<link href="https://cdnjs.cloudflare.com/ajax/libs/lightgallery/2.7.1/css/lightgallery-bundle.min.css" rel="stylesheet"> | |
<link href="https://cdnjs.cloudflare.com/ajax/libs/lightgallery/2.7.1/css/lg-transitions.min.css" rel="stylesheet"> | |
<link href="https://cdnjs.cloudflare.com/ajax/libs/lightgallery/2.7.1/css/lg-zoom.min.css" rel="stylesheet"> | |
<link href="https://cdnjs.cloudflare.com/ajax/libs/lightgallery/2.7.1/css/lg-thumbnail.min.css" rel="stylesheet"> | |
<style> | |
.inline-gallery-container { | |
width: 100%; | |
// set 60% height | |
height: 0; | |
padding-bottom: 65%; | |
} | |
</style> | |
eof | |
end | |
end | |
class LightGalleryTag < Liquid::Block | |
def initialize(tag_name, markup, tokens) | |
# No initializing needed | |
super | |
end | |
def render(context) | |
# Convert the entire content array into one large string | |
lines = super | |
# split the text by newlines | |
lines = lines.split("\n") | |
p = PhotosUtil.new(context) | |
gallery = "<div id='inline-gallery-container' class='inline-gallery-container'></div>" | |
gallery << "<script>" | |
gallery << "const lgContainer = document.getElementById('inline-gallery-container');" | |
gallery << "const inlineGallery = lightGallery(lgContainer, {" | |
gallery << " container: lgContainer," | |
gallery << " dynamic: true," | |
gallery << " hash: false," | |
gallery << " closable: false," | |
gallery << " showMaximizeIcon: true," | |
gallery << " appendSubHtmlTo: '.lg-item'," | |
gallery << " slideDelay: 100," | |
gallery << " dynamicEl: [ " | |
lines.each_with_index do |line, i| | |
next if line.empty? | |
filename, title = line.split(":") | |
title = (title.nil?) ? filename : title.strip | |
gallery << "{ src: '#{p.path_for(filename)}', thumb: '#{p.path_for(filename)}' }," | |
end | |
gallery << "], thumbWidth: 60, thumbHeight: \"40px\", thumbMargin: 4 });" | |
gallery << "inlineGallery.openGallery();" | |
gallery | |
end | |
end | |
end | |
Liquid::Template.register_tag('gallery', Jekyll::LightGalleryTag) | |
Liquid::Template.register_tag('gallery_includes', Jekyll::LightGalleryScriptIncludePatch) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment