Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support the #only-dark and #only-light feature of mkdocs-material by setting the data-gallery attribute #49

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.*>", repl_html, markdown)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to make that match non-greedy to not get two img tags at once


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