Skip to content

Commit

Permalink
Merge pull request #49 from Xiaokang2022/main
Browse files Browse the repository at this point in the history
feat: Support the `#only-dark` and `#only-light` feature of mkdocs-material by setting the `data-gallery` attribute
  • Loading branch information
blueswen authored Oct 16, 2024
2 parents f876e43 + 7d76845 commit 14e152a
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions mkdocs_glightbox/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class LightboxPlugin(BasePlugin):
("zoomable", config_options.Type(bool, default=True)),
("draggable", config_options.Type(bool, default=True)),
("skip_classes", config_options.Type(list, default=[])),
("auto_themed", config_options.Type(bool, default=False)),
("auto_caption", config_options.Type(bool, default=False)),
(
"caption_position",
Expand Down Expand Up @@ -121,6 +122,32 @@ def on_post_page(self, output, page, config, **kwargs):

return output

def on_page_markdown(self, markdown, page, config, files, **kwargs):
"""Support the #only-dark feature by setting the data-gallery property"""
if not self.config["auto_themed"] or not page.meta.get("glightbox.auto_themed", True):
return markdown

def repl_md(match):
md = match.group()
if "#only-light" in md or "#gh-light-mode-only" in md:
return md + "{data-gallery='light'}"
elif "#only-dark" in md or "#gh-dark-mode-only" in md:
return md + "{data-gallery='dark'}"
return md

def repl_html(match):
html = match.group()
if "#only-light" in html or "#gh-light-mode-only" in html:
return f'{html[:4]} data-gallery="light" {html[4:]}'
elif "#only-dark" in html or "#gh-dark-mode-only" in html:
return f'{html[:4]} data-gallery="dark" {html[4:]}'
return html

markdown = re.sub("!\\[[^\\]]*\\]\\([^)]*\\)", repl_md, markdown)
markdown = re.sub("<img\\s+[^>]*>", repl_html, markdown)

return markdown

def on_page_content(self, html, page, config, **kwargs):
"""Wrap img tag with anchor tag with glightbox class and attributes from config"""
# skip page with meta glightbox is false
Expand Down

0 comments on commit 14e152a

Please sign in to comment.