From fa9cbbe7ff93335055a49269a2965ff10b724f6a Mon Sep 17 00:00:00 2001 From: blueswen Date: Sat, 18 Nov 2023 23:14:13 +0800 Subject: [PATCH] Support compatibility with the privacy plugin of Material for MkDocs insiders (#25) --- CHANGELOG | 4 ++++ mkdocs_glightbox/plugin.py | 31 +++++++++++++++++++++++++------ setup.py | 4 ++-- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 8a8f5b3..cdfc4d1 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,7 @@ +mkdocs-glightbox-0.3.5 (2023-11-18) + + * Supported compatibility with the privacy plugin of Material for MkDocs insiders (#25) + mkdocs-glightbox-0.3.4 (2023-04-25) * Fixed regex bug: quote issue and empty alt issue (#14 #19) diff --git a/mkdocs_glightbox/plugin.py b/mkdocs_glightbox/plugin.py index f9b93f6..7e0fee2 100644 --- a/mkdocs_glightbox/plugin.py +++ b/mkdocs_glightbox/plugin.py @@ -34,6 +34,14 @@ class LightboxPlugin(BasePlugin): ), ) + def on_config(self, config): + self.using_material = config["theme"].name == "material" + self.using_material_privacy = ( + self.using_material + and "material/privacy" in config["plugins"] + and config["plugins"]["material/privacy"].config.enabled + ) + def on_post_page(self, output, page, config, **kwargs): """Add css link tag, javascript script tag, and javascript code to initialize GLightbox""" # skip page with meta glightbox is false @@ -78,10 +86,17 @@ def on_post_page(self, output, page, config, **kwargs): lb_config["openEffect"] = plugin_config.get("effect", "zoom") lb_config["closeEffect"] = plugin_config.get("effect", "zoom") lb_config["slideEffect"] = plugin_config.get("slide_effect", "slide") - js_code = f"const lightbox = GLightbox({json.dumps(lb_config)});" - if config["theme"].name == "material" or "navigation.instant" in config[ - "theme" - ]._vars.get("features", []): + js_code = "" + if self.using_material_privacy: + js_code += """document.querySelectorAll('.glightbox').forEach(function(element) { + var imgSrc = element.querySelector('img').src; + element.setAttribute('href', imgSrc); +}); +""" + js_code += f"const lightbox = GLightbox({json.dumps(lb_config)});" + if self.using_material or "navigation.instant" in config["theme"]._vars.get( + "features", [] + ): # support compatible with mkdocs-material Instant loading feature js_code = "document$.subscribe(() => {" + js_code + "})" output = body_regex.sub(f"{js_code}", output) @@ -127,8 +142,12 @@ def wrap_img_with_anchor(self, match, plugin_config, skip_class, meta): if set(skip_class) & set(classes): return img_tag - src = re.search(r"src=[\"\']([^\"\']+)", img_attr).group(1) - a_tag = f'