Skip to content

Commit 2c1b15b

Browse files
committed
Skip image in anchor tag
1 parent 1ad11f0 commit 2c1b15b

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

mkdocs_glightbox/plugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def on_post_page(self, output, page, config, **kwargs):
9393
return str(soup)
9494

9595
def on_page_content(self, html, page, config, **kwargs):
96-
"""Wrap img tag with archive tag with glightbox class and attributes from config"""
96+
"""Wrap img tag with anchor tag with glightbox class and attributes from config"""
9797
# skip page with meta glightbox is false
9898
if "glightbox" in page.meta and page.meta.get("glightbox", True) is False:
9999
return html
@@ -105,7 +105,7 @@ def on_page_content(self, html, page, config, **kwargs):
105105
soup = BeautifulSoup(html, "html.parser")
106106
imgs = soup.find_all("img")
107107
for img in imgs:
108-
if set(skip_class) & set(img.get("class", [])):
108+
if set(skip_class) & set(img.get("class", [])) or img.parent.name == "a":
109109
continue
110110
a = soup.new_tag("a")
111111
a["class"] = "glightbox"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[![image](img.png)](https://blueswen.github.io/mkdocs-glightbox/)

tests/test_builds.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,3 +557,19 @@ def test_static(tmp_path):
557557
os.path.join(testproject_path, "site/assets/javascripts/glightbox.min.js")
558558
)
559559

560+
561+
def test_image_in_anchor(tmp_path):
562+
"""
563+
Disable when image in an anchor tag
564+
"""
565+
mkdocs_file = "mkdocs.yml"
566+
testproject_path = validate_mkdocs_file(tmp_path, f"tests/fixtures/{mkdocs_file}")
567+
file = testproject_path / "site/image_in_anchor/index.html"
568+
contents = file.read_text(encoding="utf8")
569+
path = "../"
570+
validate_static(contents, path)
571+
validate_script(contents)
572+
assert re.search(
573+
rf'<a class="glightbox" .* href="{path}img.png"><img .* src="{path}img.png"\/><\/a>',
574+
contents,
575+
) is None

0 commit comments

Comments
 (0)