Skip to content

Commit

Permalink
Support compatibility with the privacy plugin of Material for MkDocs …
Browse files Browse the repository at this point in the history
…insiders (#25)
  • Loading branch information
blueswen committed Nov 18, 2023
1 parent 7d1d941 commit fa9cbbe
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
31 changes: 25 additions & 6 deletions mkdocs_glightbox/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"<body\\1<script>{js_code}</script></body>", output)
Expand Down Expand Up @@ -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'<a class="glightbox" href="{src}" data-type="image"'
if self.using_material_privacy:
# skip href attribute if using material privacy plugin, will be set by js code
a_tag = '<a class="glightbox" data-type="image"'
else:
src = re.search(r"src=[\"\']([^\"\']+)", img_attr).group(1)
a_tag = f'<a class="glightbox" href="{src}" data-type="image"'
# setting data-width and data-height with plugin options
for k, v in plugin_config.items():
a_tag += f' data-{k}="{v}"'
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from setuptools import setup, find_packages
from setuptools import find_packages, setup

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

setup(
name="mkdocs-glightbox",
version="0.3.4",
version="0.3.5",
author="Blueswen",
author_email="[email protected]",
url="https://blueswen.github.io/mkdocs-glightbox",
Expand Down

0 comments on commit fa9cbbe

Please sign in to comment.