From cba1dfa1fe89bb3b61bccc065191f695320983cf Mon Sep 17 00:00:00 2001 From: Xiaokang2022 <2951256653@qq.com> Date: Wed, 11 Sep 2024 03:43:02 +0800 Subject: [PATCH 1/3] Support the `#only-dark` feature by setting the `data-gallery` property --- mkdocs_glightbox/plugin.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/mkdocs_glightbox/plugin.py b/mkdocs_glightbox/plugin.py index e0a1e4f..b28a29e 100644 --- a/mkdocs_glightbox/plugin.py +++ b/mkdocs_glightbox/plugin.py @@ -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", @@ -121,6 +122,32 @@ def on_post_page(self, output, page, config, **kwargs): return output + def on_page_markdown(self, markdown, *args, **kwargs): + """Support the #only-dark feature by setting the data-gallery property""" + if not self.config["auto_themed"]: + 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("", 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 From 317104d9155f6a63071a918d4ff41a73d3f00d1c Mon Sep 17 00:00:00 2001 From: Xiaokang2022 <2951256653@qq.com> Date: Wed, 11 Sep 2024 04:29:53 +0800 Subject: [PATCH 2/3] Added page metadata `glightbox.auto_themed` --- mkdocs_glightbox/plugin.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mkdocs_glightbox/plugin.py b/mkdocs_glightbox/plugin.py index b28a29e..7284c6d 100644 --- a/mkdocs_glightbox/plugin.py +++ b/mkdocs_glightbox/plugin.py @@ -122,9 +122,9 @@ def on_post_page(self, output, page, config, **kwargs): return output - def on_page_markdown(self, markdown, *args, **kwargs): + 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"]: + if not self.config["auto_themed"] or not page.meta.get("glightbox.auto_themed", True): return markdown def repl_md(match): From 7d76845fd098b485a54ab773a668c15f9935694f Mon Sep 17 00:00:00 2001 From: Xiaokang2022 <2951256653@qq.com> Date: Mon, 7 Oct 2024 13:16:32 +0800 Subject: [PATCH 3/3] fix: change the img tag regex matching to non-greedy --- mkdocs_glightbox/plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mkdocs_glightbox/plugin.py b/mkdocs_glightbox/plugin.py index 7284c6d..3daaed7 100644 --- a/mkdocs_glightbox/plugin.py +++ b/mkdocs_glightbox/plugin.py @@ -144,7 +144,7 @@ def repl_html(match): return html markdown = re.sub("!\\[[^\\]]*\\]\\([^)]*\\)", repl_md, markdown) - markdown = re.sub("", repl_html, markdown) + markdown = re.sub("]*>", repl_html, markdown) return markdown