@@ -295,9 +295,39 @@ if (document.querySelectorAll('pre code').length > 0) {
295295
296296// Image Modal functionality
297297function initImageModal ( ) {
298+ // Create modal elements first
299+ createImageModal ( ) ;
300+
301+ // Initialize with current elements
302+ setupImageModalListeners ( ) ;
303+
304+ // Also setup a mutation observer to detect when new mermaid elements are added
305+ const observer = new MutationObserver ( function ( mutations ) {
306+ mutations . forEach ( function ( mutation ) {
307+ if ( mutation . addedNodes . length > 0 ) {
308+ mutation . addedNodes . forEach ( function ( node ) {
309+ if ( node . nodeType === 1 && ( node . classList . contains ( 'mermaid' ) || node . querySelector ( '.mermaid' ) ) ) {
310+ // New mermaid diagram detected, re-setup listeners
311+ setTimeout ( ( ) => {
312+ setupImageModalListeners ( ) ;
313+ } , 100 ) ;
314+ }
315+ } ) ;
316+ }
317+ } ) ;
318+ } ) ;
319+
320+ observer . observe ( document . body , {
321+ childList : true ,
322+ subtree : true
323+ } ) ;
324+ }
325+
326+ function createImageModal ( ) {
298327 // Create modal elements
299328 const modal = document . createElement ( 'div' ) ;
300329 modal . className = 'image-modal' ;
330+ modal . id = 'imageModal' ;
301331 modal . innerHTML = `
302332 <div class="image-modal-content">
303333 <button class="image-modal-close" aria-label="Close modal">
@@ -319,8 +349,13 @@ function initImageModal() {
319349 ` ;
320350
321351 document . body . appendChild ( modal ) ;
352+ }
353+
354+ function setupImageModalListeners ( ) {
355+ // Get existing modal elements
356+ const modal = document . getElementById ( 'imageModal' ) ;
357+ if ( ! modal ) return ;
322358
323- // Get modal elements
324359 const modalContent = modal . querySelector ( '.image-modal-content' ) ;
325360 const modalImg = modal . querySelector ( 'img' ) ;
326361 const modalInfo = modal . querySelector ( '.image-modal-info' ) ;
@@ -329,6 +364,10 @@ function initImageModal() {
329364 const modalNext = modal . querySelector ( '.image-modal-next' ) ;
330365 const modalLoading = modal . querySelector ( '.image-modal-loading' ) ;
331366
367+ // Remove existing listeners to prevent duplicates
368+ const oldListeners = modal . getAttribute ( 'data-listeners-added' ) ;
369+ if ( oldListeners === 'true' ) return ;
370+
332371 // Get all images and mermaid diagrams in post content
333372 const images = document . querySelectorAll ( '.post-content img, .page-content img' ) ;
334373 const mermaidDiagrams = document . querySelectorAll ( '.post-content pre.mermaid, .page-content pre.mermaid' ) ;
@@ -572,4 +611,7 @@ function initImageModal() {
572611 modal . addEventListener ( 'touchmove' , function ( e ) {
573612 e . preventDefault ( ) ;
574613 } , { passive : false } ) ;
614+
615+ // Mark listeners as added
616+ modal . setAttribute ( 'data-listeners-added' , 'true' ) ;
575617}
0 commit comments