@@ -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 } "'
0 commit comments