Skip to content

Commit 83184c0

Browse files
committed
Support glightbox built-in gallery feature (#11)
1 parent 10d203e commit 83184c0

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

mkdocs_glightbox/plugin.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,20 +110,36 @@ def on_page_content(self, html, page, config, **kwargs):
110110
a = soup.new_tag("a")
111111
a["class"] = "glightbox"
112112
a["href"] = img.get("src", "")
113+
# setting data-width and data-height with plugin options
113114
for k, v in plugin_config.items():
114115
a[f"data-{k}"] = v
115-
# alt as title when auto_caption is enabled
116-
if self.config["auto_caption"] or (
117-
"glightbox.auto_caption" in page.meta
118-
and page.meta.get("glightbox.auto_caption", False) is True
119-
):
120-
a["data-title"] = img.get("data-title", img.get("alt", ""))
121-
else:
122-
a["data-title"] = img.get("data-title", "")
123-
a["data-description"] = img.get("data-description", "")
124-
a["data-desc-position"] = img.get(
125-
"data-caption-position", self.config["caption_position"]
126-
)
116+
slide_options = ["title", "description", "caption-position", "gallery"]
117+
for option in slide_options:
118+
attr = f"data-{option}"
119+
if attr == "data-title":
120+
# alt as title when auto_caption is enabled
121+
if self.config["auto_caption"] or (
122+
"glightbox.auto_caption" in page.meta
123+
and page.meta.get("glightbox.auto_caption", False) is True
124+
):
125+
val = img.get("data-title", img.get("alt", ""))
126+
else:
127+
val = img.get("data-title", "")
128+
elif attr == "data-caption-position":
129+
# plugin option caption_position as default
130+
val = img.get(
131+
"data-caption-position", self.config["caption_position"]
132+
)
133+
else:
134+
val = img.get(attr, "")
135+
136+
# skip val is empty
137+
if val != "":
138+
# convert data-caption-position to data-desc-position
139+
if attr == "data-caption-position":
140+
a["data-desc-position"] = val
141+
else:
142+
a[attr] = val
127143
img.wrap(a)
128144
return str(soup)
129145

0 commit comments

Comments
 (0)