Skip to content

Conversation

saasan
Copy link

@saasan saasan commented Jul 28, 2025

Description

This PR fixes the issue where the PhotoSwipe script wraps images that are already inside <a> tags, causing the original link functionality to be lost.

Related Issue

Fixes #187

Problem

The PhotoSwipe script in _includes/extensions/photo-swipe.html currently wraps all <img> elements (except those with emoji class) in a new <a class="photo-swipe"> element. This includes images that are already wrapped in links, breaking their original functionality.

Before (buggy behavior):

<!-- Original markup -->
<a href="https://example.com">
  <img src="image.jpg">
</a>

<!-- After PhotoSwipe processing -->
<a href="https://example.com">
  <a class="photo-swipe" href="image.jpg" ...>
    <img src="image.jpg">
  </a>
</a>

The original link to https://example.com becomes inaccessible.

Solution

Added a check to skip images whose parent element is already an <a> tag, preserving their original link behavior.

After this fix:

<!-- Images already in links are preserved -->
<a href="https://example.com">
  <img src="image.jpg">
</a>

<!-- Standalone images still get PhotoSwipe functionality -->
<a class="photo-swipe" href="standalone.jpg" ...>
  <img src="standalone.jpg">
</a>

Changes

  • Added condition el.parentElement?.tagName.toLowerCase() === "a" to skip images that are already inside anchor tags
  • Preserves original link functionality for linked images
  • Maintains PhotoSwipe functionality for standalone images

Testing

  • ✅ Linked images maintain their original links and don't trigger PhotoSwipe
  • ✅ Standalone images still get PhotoSwipe functionality
  • ✅ Images with emoji class continue to be ignored as before
  • ✅ No breaking changes to existing PhotoSwipe behavior

Files Changed

  • _includes/extensions/photo-swipe.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: PhotoSwipe script wraps already linked images, breaking original links
1 participant