jQuery('.page p>a>img').each(function () { const captionSelector = 'title'; let $img = jQuery(this); let $a = $img.closest('a'); let $p = $img.closest('p'); if ($p.text().trim().length == 0) { // Img shoud not be an inline image let captionText = $img.attr(captionSelector); if (captionText !== undefined && captionText.length > 0) { let imgHtml = $a[0].outerHTML; // let figureAttr = ''; let imgClass = $img.attr('class'); if (imgClass !== undefined) { figureAttr += ' ' + 'class="' + imgClass + '"'; } let imgWidth = $img.attr('width'); if (imgWidth !== undefined) { figureAttr += ' ' + 'style="width:' + imgWidth + 'px;"'; } let figureHtml = '' + imgHtml + '
' + captionText + '
' + ''; $a.before(figureHtml); $a.remove(); if (location.hash) { location.href = location.hash; } } } });