diff options
author | Ivan Boothe <git@rootwork.org> | 2022-04-08 03:39:41 +0300 |
---|---|---|
committer | Ivan Boothe <git@rootwork.org> | 2022-04-08 03:39:41 +0300 |
commit | 2e7afb82c973614609ccefeb855d12594b066538 (patch) | |
tree | 85848181db90bc5f8800c4febd713e8e6e4ab658 /assets | |
parent | 487c8615ce28fc30255e1e509a2b2f311fe16e63 (diff) |
provide a way to have alt text without a caption
Signed-off-by: Ivan Boothe <git@rootwork.org>
Diffstat (limited to 'assets')
-rw-r--r-- | assets/js/index.js | 46 |
1 files changed, 32 insertions, 14 deletions
diff --git a/assets/js/index.js b/assets/js/index.js index 6a1c0c1..007daf2 100644 --- a/assets/js/index.js +++ b/assets/js/index.js @@ -258,30 +258,48 @@ function fileClosure(){ modifyClass(figure, 'inline'); } - // Figure numbering - let captionText = image.title.trim().length ? image.title.trim() : alt; + // Image captions + let addCaption = true + let captionText = '' - if (captionText.length && !containsClass(image, 'alt' && !isInline)) { - imagePosition += 1; - image.dataset.pos = imagePosition; - const showImagePosition = showingImagePosition(); + if(image.title.trim().length) { + captionText = image.title.trim() + } else { + if(image.title === " ") { + addCaption = false + } else { + captionText = alt + } + } + // Don't add a caption for featured images, inline images, or empty text + if( + image.matches(`.${featuredImageClass}`) || + containsClass(image, 'alt' && !isInline) || + !captionText.length + ) { + addCaption = false + } + + if (addCaption) { let desc = document.createElement('figcaption'); desc.classList.add(imageAltClass); + // Add figure numbering + imagePosition += 1; + image.dataset.pos = imagePosition; + const showImagePosition = showingImagePosition(); const thisImgPos = image.dataset.pos; - // modify image caption is necessary captionText = showImagePosition ? `${showImagePositionLabel} ${thisImgPos}: ${captionText}` : captionText; desc.textContent = captionText; - if(!image.matches(`.${featuredImageClass}`)) { - // add a caption below image only if the image isn't a featured image - if(image.nextElementSibling) { - // check if a caption exist already and remove it - image.nextElementSibling.remove(); - } - image.insertAdjacentHTML('afterend', desc.outerHTML); + // If a caption exists, remove it + if(image.nextElementSibling) { + image.nextElementSibling.remove(); } + + // Insert caption + image.insertAdjacentHTML('afterend', desc.outerHTML); } }); |