Skip to content

Commit

Permalink
try catch prevent Uncaught TypeError (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
blueswen committed May 3, 2024
1 parent 7d2c0ab commit dc38a6c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
12 changes: 10 additions & 2 deletions mkdocs_glightbox/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,16 @@ def on_post_page(self, output, page, config, **kwargs):
js_code = ""
if self.using_material_privacy:
js_code += """document.querySelectorAll('.glightbox').forEach(function(element) {
var imgSrc = element.querySelector('img').src;
element.setAttribute('href', imgSrc);
try {
var img = element.querySelector('img');
if (img && img.src) {
element.setAttribute('href', img.src);
} else {
console.log('No img element with src attribute found');
}
} catch (error) {
console.log('Error:', error);
}
});
"""
js_code += f"const lightbox = GLightbox({json.dumps(lb_config)});\n"
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/docs/edge_cases.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p class="glightbox">Not an image</p>

0 comments on commit dc38a6c

Please sign in to comment.