Skip to content

Commit 1409b0d

Browse files
committed
Add manual mode
1 parent e5ce7c9 commit 1409b0d

File tree

10 files changed

+206
-11
lines changed

10 files changed

+206
-11
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
You can keep the whole site's images without lightbox effect but only enable lightbox effect on specific images or images on specific pages with the `manual` plugin option.
2+
3+
```yaml
4+
plugins:
5+
- glightbox:
6+
manual: true # enable manual mode, all images will not be added to the lightbox effect
7+
```
8+
9+
## Enable specific images
10+
11+
Enable lightbox effect on specific images by adding `on-glb` class to the image through markdown_extensions ```attr_list```. Enable ```attr_list``` via ```mkdocs.yml```:
12+
13+
```yaml
14+
markdown_extensions:
15+
- attr_list
16+
```
17+
18+
Check more details about ```attr_list``` on the [official document](https://python-markdown.github.io/extensions/attr_list/).
19+
20+
### Demo
21+
22+
Only images with ```on-glb``` class will be added to the lightbox effect. Other images on the site will not be added to the lightbox effect.
23+
24+
```yaml
25+
# mkdocs.yml
26+
markdown_extensions:
27+
- attr_list
28+
29+
plugins:
30+
- glightbox:
31+
manual: true
32+
```
33+
34+
```markdown
35+
36+
![Lanyu, Taiwan](../images/gallery/blueswen-lanyu.jpeg){ .on-glb }
37+
Lanyu, Taiwan. Credit: Blueswen
38+
39+
![Hallstatt, Austria](../images/gallery/blueswen-hallstatt.jpeg){ .on-glb }
40+
Hallstatt, Austria. Credit: Blueswen
41+
42+
![Madeira, Portugal](../images/gallery/blueswen-madeira.jpeg)
43+
Madeira, Portugal. Credit: Blueswen
44+
45+
```
46+
47+
<figure markdown>
48+
49+
![Lanyu, Taiwan](../images/gallery/blueswen-lanyu.jpeg)
50+
51+
<figcaption>Lanyu, Taiwan. Credit: Blueswen</figcaption>
52+
</figure>
53+
54+
<figure markdown>
55+
56+
![Hallstatt, Austria](../images/gallery/blueswen-hallstatt.jpeg)
57+
58+
<figcaption>Hallstatt, Austria. Credit: Blueswen</figcaption>
59+
</figure>
60+
61+
<figure markdown>
62+
63+
![Madeira, Portugal](../images/gallery/blueswen-madeira.jpeg){ .off-glb }
64+
65+
<figcaption>Madeira, Portugal. Credit: Blueswen</figcaption>
66+
</figure>
67+
68+
## Enable specific pages
69+
70+
Enable lightbox effect on specific pages, add page metadata ```glightbox: true``` through markdown_extensions ```meta```. Enable ```meta``` via ```mkdocs.yml```:
71+
72+
```yaml
73+
markdown_extensions:
74+
- meta
75+
```
76+
77+
Check more details about ```meta``` on the [official document](https://python-markdown.github.io/extensions/meta_data/).
78+
79+
### Demo
80+
81+
Only pages with `glightbox: true` meta will be added to the lightbox effect. Other images on the site will not be added to the lightbox effect.
82+
83+
```yaml
84+
# mkdocs.yml
85+
markdown_extensions:
86+
- meta
87+
88+
plugins:
89+
- glightbox:
90+
manual: true
91+
```
92+
93+
```markdown
94+
---
95+
glightbox: true
96+
---
97+
98+
![Castelo de São Jorge, Lisbon, Portugal](../images/gallery/blueswen-lisbon.jpeg)
99+
Cabo da Roca, Portugal. Credit: Blueswen
100+
101+
![Yosemite, USA.](../images/gallery/blueswen-yosemite.jpeg)
102+
Yosemite, USA. Credit: Blueswen
103+
104+
```
105+
106+
<figure markdown>
107+
108+
![Castelo de São Jorge, Lisbon, Portugal](../images/gallery/blueswen-lisbon.jpeg)
109+
110+
<figcaption>Cabo da Roca, Portugal. Credit: Blueswen</figcaption>
111+
</figure>
112+
113+
<figure markdown>
114+
115+
![Yosemite, USA.](../images/gallery/blueswen-yosemite.jpeg)
116+
117+
<figcaption>Yosemite, USA. Credit: Blueswen</figcaption>
118+
</figure>

demo-mkdocs/docs/index.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,17 @@ GLightbox is a pure javascript lightbox library with mobile support.
6969
| caption_position | bottom | Default captions position. (bottom, top, left, right) |
7070
| background | white | The background CSS of lightbox image. The background will shown when the image is transparent. You can use any CSS value for the background for example `#74b9ff` or `Gainsboro` or `none` for nothing. |
7171
| shadow | true | Enable or disable the shadow of lightbox image. Disable it when the background is `none` to prevent shadow around the transparent image. |
72-
| manual | false | When true, lightbox has to be enabled for each image manually by adding 'on-glb' class to it. |
72+
| manual | false | When true, lightbox has to be enabled for each image manually by adding `on-glb` class to it or adding `glightbox: true` meta on page. |
7373

7474
Check more options information on [GLightbox Docs](https://github.com/biati-digital/glightbox#lightbox-options).
7575

76-
5. For more flexibility, you can disable the lightbox with a [specific image](./disable/image.md) or a [specific page](./disable/page.md).
77-
6. Support lightbox image caption, check more details on [Caption](./caption/caption.md).
78-
7. Support grouping images as galleries, check more details on [Gallery](./gallery/gallery.md).
76+
5. For more flexibility:
77+
1. [Disable by image](./flexibility/disable-by-image.md): Disable the lightbox for specific images. Suitable for a few amount of images that don't need the lightbox effect.
78+
2. [Disable by page](./flexibility/disable-by-page.md): Disable the lightbox for specific pages. Suitable for a few amount of pages that don't need the lightbox effect.
79+
3. [Enable by image](./flexibility/disable-by-page-enable-by-image.md): Disable the lightbox for specific pages but enable some images on those pages. Suitable for a few amount of images that need the lightbox effect.
80+
4. [Disable globally but enable by image or page](./flexibility/enable-by-image-or-page.md): Disable the lightbox globally but enable specific images or specific pages. Suitable for a large number of images or pages that don't need the lightbox effect.
81+
6. Support lightbox image caption, check more details on [Caption](./caption/caption.md).
82+
7. Support grouping images as galleries, check more details on [Gallery](./gallery/gallery.md).
7983
8084
!!! note
8185

demo-mkdocs/mkdocs.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ nav:
6969
- Caption: caption/caption.md
7070
- Gallery: gallery/gallery.md
7171
- More Flexibility:
72-
- Disable by image: disable/image.md
73-
- Disable by page: disable/page.md
74-
- Enable by image: disable/manual.md
72+
- Disable by image: flexibility/disable-by-image.md
73+
- Disable by page: flexibility/disable-by-page.md
74+
- Enable by image: flexibility/disable-by-page-enable-by-image.md
75+
- Disable globally but enable by image or page: flexibility/enable-by-image-or-page.md

mkdocs_glightbox/plugin.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,19 @@ def wrap_img_with_anchor(self, match, plugin_config, skip_class, meta):
157157
classes = re.findall(r'class="([^"]+)"', img_attr)
158158
classes = [c for match in classes for c in match.split()]
159159

160-
if meta.get("glightbox-manual", False) or self.config["manual"]:
161-
if "on-glb" not in classes:
162-
return img_tag
163-
else:
160+
if self.config["manual"] and meta.get("glightbox", None) is True:
161+
# with manual mode but enable glightbox in page meta
164162
if set(skip_class) & set(classes):
163+
# skip image with off-glb and specific class
164+
return img_tag
165+
elif meta.get("glightbox-manual", False) or self.config["manual"]:
166+
# disable by page meta or global config
167+
if "on-glb" not in classes:
168+
# skip image without on-glb class
165169
return img_tag
170+
elif set(skip_class) & set(classes):
171+
# skip image with off-glb and specific class
172+
return img_tag
166173

167174
if self.using_material_privacy:
168175
# skip href attribute if using material privacy plugin, will be set by js code
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
glightbox: true
3+
---
4+
5+
![image-a](img.png)
6+
7+
![image-b](img.png){ .off-glb }
8+
9+
![image-c](img.png)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
site_name: test mkdocs_glightbox
2+
use_directory_urls: true
3+
4+
theme:
5+
name: material
6+
features:
7+
- navigation.instant
8+
9+
markdown_extensions:
10+
- attr_list
11+
- md_in_html
12+
13+
plugins:
14+
- glightbox

tests/test_builds.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,26 @@ def test_material(tmp_path):
192192
)
193193

194194

195+
def test_material_instant(tmp_path):
196+
"""
197+
Integrate with Material for MkDocs
198+
"""
199+
mkdocs_file = "mkdocs-material-instant.yml"
200+
testproject_path = validate_mkdocs_file(tmp_path, f"tests/fixtures/{mkdocs_file}")
201+
file = testproject_path / "site/index.html"
202+
contents = file.read_text(encoding="utf8")
203+
validate_static(contents)
204+
validate_script(contents)
205+
assert re.search(
206+
r"document\$\.subscribe\(\(\) => { lightbox.reload\(\) }\);",
207+
contents,
208+
)
209+
assert re.search(
210+
r'<a class="glightbox".*?href="img\.png".*?>\s*<img.*?src="img\.png".*?\/><\/a>',
211+
contents,
212+
)
213+
214+
195215
def test_use_directory_urls(tmp_path):
196216
"""
197217
Compatible with use_directory_urls is false or with --use-directory-urls and --use-directory-urls as args
@@ -691,3 +711,25 @@ def test_manual(tmp_path):
691711
r'<a class="glightbox".*?href="..\/img\.png".*?>\s*<img.*?src="..\/img\.png".*?\/><\/a>',
692712
contents,
693713
)
714+
715+
file = testproject_path / "site/manual_enable_by_page/index.html"
716+
contents = file.read_text(encoding="utf8")
717+
validate_static(contents, path="../")
718+
validate_script(contents)
719+
assert re.search(
720+
r'<a class="glightbox".*?href="..\/img\.png".*?>\s*<img.*?alt="image-a" src="..\/img\.png".*?\/><\/a>',
721+
contents,
722+
)
723+
724+
assert (
725+
re.search(
726+
r'<a class="glightbox".*?href="..\/img\.png".*?>\s*<img.*?alt="image-b" src="..\/img\.png".*?\/><\/a>',
727+
contents,
728+
)
729+
is None
730+
)
731+
732+
assert re.search(
733+
r'<a class="glightbox".*?href="..\/img\.png".*?>\s*<img.*?alt="image-c" src="..\/img\.png".*?\/><\/a>',
734+
contents,
735+
)

0 commit comments

Comments
 (0)