The galleryImage shortcode uses eleventy-img to take the full size image referenced by the URL and generate the thumbnail image and images for the fullscreen lightbox. Next it generates the markup expected by photoswipe. Since photoswipe requires the dimensions of each image to be explictly specified, this info is taken from the metadata returned by eleventy-img and injected into the markup.
This is also where I ran into a bug with eleventy-img where it wouldn't handle rotated images properly and would generate flipped images as an output. There seems to have been attempts to fix it but the bug is still present in version 2.0.1. My solution to this was to detect if images are rotated and generate full size images that have the correct orientation which elventy-img is able to handle properly.
async function galleryImageShortcode(src, alt) {
...
const metadata = await sharp(src).metadata();
if (metadata.orientation > 1) {
console.log('Rotated image detected:', src, metadata.orientation);
await sharp(src).rotate().toFile(`correct/${src.split("/").pop()}`);
}
...
}
You might have also noticed the string replacement .replace(/(\r\n|\n|\r)/gm, "")
This is to remove all the new lines in the generated HTML and prevent the markdown parser from generating empty paragraph tags.
All of this would generate the following HTML
<div class="gallery" id="gallery-uzupis">
<a href="/gen/1-tibeto-skveras-2000w.jpeg" data-pswp-height="1333" data-pswp-width="2000" target="_blank">
<img alt="Tiberto Skveras" src="/gen/1-tibeto-skveras-192w.jpeg">
</a>
<a href="/gen/2-tibetan-flags-2000w.jpeg" data-pswp-height="1333" data-pswp-width="2000" target="_blank">
<img alt="Tibetan Flags" src="/gen/2-tibetan-flags-192w.jpeg">
</a>
</div>
<script type="module">
import PhotoSwipeLightbox from '/js/photoswipe-lightbox.esm.min.js';
import PhotoSwipe from '/js/photoswipe.esm.min.js';
const lightbox = new PhotoSwipeLightbox({
gallery: '#gallery-uzupis',
children: 'a',
pswpModule: PhotoSwipe,
preload: [1, 1]
});
lightbox.init();
</script>
which would be styled with this CSS
.gallery {
margin: 32px 0;
column-count: 4;
column-gap: 8px;
}
.gallery a {
display: grid;
grid-template-rows: 1fr auto;
margin-bottom: 8px;
break-inside: avoid;
text-decoration: none;
}
.gallery img {
max-width: 100%;
display: block;
grid-row: 1 / -1;
grid-column: 1;
}
and voila!