From 8fa0e043df5d084eee13a172d3008b7d4a17baae Mon Sep 17 00:00:00 2001 From: blueswen Date: Sat, 30 Dec 2023 18:21:16 +0800 Subject: [PATCH] Support only enable glightbox with on-glb class in given page (#28) --- mkdocs_glightbox/plugin.py | 8 ++++++-- tests/fixtures/docs/enable_by_image.md | 7 +++++++ tests/test_builds.py | 21 +++++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/docs/enable_by_image.md diff --git a/mkdocs_glightbox/plugin.py b/mkdocs_glightbox/plugin.py index c682529..66907b9 100644 --- a/mkdocs_glightbox/plugin.py +++ b/mkdocs_glightbox/plugin.py @@ -139,8 +139,12 @@ def wrap_img_with_anchor(self, match, plugin_config, skip_class, meta): classes = re.findall(r'class="([^"]+)"', img_attr) classes = [c for match in classes for c in match.split()] - if set(skip_class) & set(classes): - return img_tag + if meta.get("glightbox-manual", False): + if "on-glb" not in classes: + return img_tag + else: + if set(skip_class) & set(classes): + return img_tag if self.using_material_privacy: # skip href attribute if using material privacy plugin, will be set by js code diff --git a/tests/fixtures/docs/enable_by_image.md b/tests/fixtures/docs/enable_by_image.md new file mode 100644 index 0000000..d62953b --- /dev/null +++ b/tests/fixtures/docs/enable_by_image.md @@ -0,0 +1,7 @@ +--- +glightbox-manual: true +--- + +![image](img.png){.on-glb} + +![image](img.png) diff --git a/tests/test_builds.py b/tests/test_builds.py index e932dbb..179a3ec 100644 --- a/tests/test_builds.py +++ b/tests/test_builds.py @@ -633,3 +633,24 @@ def test_privacy(tmp_path): rf"{patch_script}", contents, ) + + +def test_enable_by_image(tmp_path): + """ + Enable by the image with on-glb class + """ + mkdocs_file = "mkdocs-material.yml" + testproject_path = validate_mkdocs_file(tmp_path, f"tests/fixtures/{mkdocs_file}") + file = testproject_path / "site/enable_by_image/index.html" + contents = file.read_text(encoding="utf8") + path = "../" + validate_static(contents, path=path) + validate_script(contents) + assert re.search( + rf'

image<\/p>', + contents, + ) + assert re.search( + rf'<\/a>', + contents, + )