Skip to content

Commit fa9cbbe

Browse files
committed
Support compatibility with the privacy plugin of Material for MkDocs insiders (#25)
1 parent 7d1d941 commit fa9cbbe

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
mkdocs-glightbox-0.3.5 (2023-11-18)
2+
3+
* Supported compatibility with the privacy plugin of Material for MkDocs insiders (#25)
4+
15
mkdocs-glightbox-0.3.4 (2023-04-25)
26

37
* Fixed regex bug: quote issue and empty alt issue (#14 #19)

mkdocs_glightbox/plugin.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ class LightboxPlugin(BasePlugin):
3434
),
3535
)
3636

37+
def on_config(self, config):
38+
self.using_material = config["theme"].name == "material"
39+
self.using_material_privacy = (
40+
self.using_material
41+
and "material/privacy" in config["plugins"]
42+
and config["plugins"]["material/privacy"].config.enabled
43+
)
44+
3745
def on_post_page(self, output, page, config, **kwargs):
3846
"""Add css link tag, javascript script tag, and javascript code to initialize GLightbox"""
3947
# skip page with meta glightbox is false
@@ -78,10 +86,17 @@ def on_post_page(self, output, page, config, **kwargs):
7886
lb_config["openEffect"] = plugin_config.get("effect", "zoom")
7987
lb_config["closeEffect"] = plugin_config.get("effect", "zoom")
8088
lb_config["slideEffect"] = plugin_config.get("slide_effect", "slide")
81-
js_code = f"const lightbox = GLightbox({json.dumps(lb_config)});"
82-
if config["theme"].name == "material" or "navigation.instant" in config[
83-
"theme"
84-
]._vars.get("features", []):
89+
js_code = ""
90+
if self.using_material_privacy:
91+
js_code += """document.querySelectorAll('.glightbox').forEach(function(element) {
92+
var imgSrc = element.querySelector('img').src;
93+
element.setAttribute('href', imgSrc);
94+
});
95+
"""
96+
js_code += f"const lightbox = GLightbox({json.dumps(lb_config)});"
97+
if self.using_material or "navigation.instant" in config["theme"]._vars.get(
98+
"features", []
99+
):
85100
# support compatible with mkdocs-material Instant loading feature
86101
js_code = "document$.subscribe(() => {" + js_code + "})"
87102
output = body_regex.sub(f"<body\\1<script>{js_code}</script></body>", output)
@@ -127,8 +142,12 @@ def wrap_img_with_anchor(self, match, plugin_config, skip_class, meta):
127142
if set(skip_class) & set(classes):
128143
return img_tag
129144

130-
src = re.search(r"src=[\"\']([^\"\']+)", img_attr).group(1)
131-
a_tag = f'<a class="glightbox" href="{src}" data-type="image"'
145+
if self.using_material_privacy:
146+
# skip href attribute if using material privacy plugin, will be set by js code
147+
a_tag = '<a class="glightbox" data-type="image"'
148+
else:
149+
src = re.search(r"src=[\"\']([^\"\']+)", img_attr).group(1)
150+
a_tag = f'<a class="glightbox" href="{src}" data-type="image"'
132151
# setting data-width and data-height with plugin options
133152
for k, v in plugin_config.items():
134153
a_tag += f' data-{k}="{v}"'

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
from setuptools import setup, find_packages
1+
from setuptools import find_packages, setup
22

33
with open("README.md", encoding="utf-8") as f:
44
long_description = f.read()
55

66
setup(
77
name="mkdocs-glightbox",
8-
version="0.3.4",
8+
version="0.3.5",
99
author="Blueswen",
1010
author_email="[email protected]",
1111
url="https://blueswen.github.io/mkdocs-glightbox",

0 commit comments

Comments
 (0)