From 2fb663e1fdb89b3de2ef1f92c8b4bf361b732852 Mon Sep 17 00:00:00 2001 From: squidfunk Date: Fri, 24 Apr 2026 16:04:53 +0200 Subject: [PATCH] fix: text adjacent to image included in link to open image in lightbox (#579) Signed-off-by: squidfunk --- python/zensical/extensions/glightbox.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/python/zensical/extensions/glightbox.py b/python/zensical/extensions/glightbox.py index bbfaaf3..cacdc3a 100644 --- a/python/zensical/extensions/glightbox.py +++ b/python/zensical/extensions/glightbox.py @@ -83,12 +83,21 @@ class GlightboxTreeprocessor(Treeprocessor): if parent is None or parent.tag == "a": return - # Wrap image with anchor + # Create anchor element with appropriate attributes based on the image + # and config settings, then wrap the image with the anchor + anchor = self._build_anchor(img) + anchor.append(img) + + # If there's text adjacent to the image element, we need to clear it + # from the image and move it to the anchor, see https://t.ly/dLA-l + anchor.tail = img.tail + img.tail = None + + # Remove image from current position and append to anchor, then insert + # anchor back into the original position in the tree index = list(parent).index(img) - el = self._build_anchor(img) parent.remove(img) - el.append(img) - parent.insert(index, el) + parent.insert(index, anchor) def _build_anchor(self, img: Element) -> Element: """Construct the anchor from image attributes."""