fix: enhance image caption handling by using title attribute if available (#1265)

* fix: enhance image caption handling by using title attribute if available

* feat: support markdown syntax inside image caption
This commit is contained in:
Jimmy
2026-02-17 18:38:06 +01:00
committed by GitHub
parent 3806cc9230
commit b4e44fda57
2 changed files with 20 additions and 2 deletions
+15 -2
View File
@@ -12,6 +12,15 @@ const wrap = (figures: HTMLElement[]) => {
}
}
const unescapeHtml = (html: string) => {
/// Replace special HTML entities with their corresponding characters
return html.replace(/&/g, '&')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&quot;/g, '"')
.replace(/&#39;/g, "'");
}
export default (container: HTMLElement) => {
/// The process of wrapping image with figure tag is done using JavaScript instead of only Hugo markdown render hook
/// because it can not detect whether image is being wrapped by a link or not
@@ -64,9 +73,13 @@ export default (container: HTMLElement) => {
figure.appendChild(el);
/// Add figcaption if it exists
if (img.hasAttribute('alt')) {
let caption = img.getAttribute('alt');
if (img.getAttribute('data-title-escaped')) {
caption = unescapeHtml(img.getAttribute('data-title-escaped')!);
}
if (caption) {
const figcaption = document.createElement('figcaption');
figcaption.innerText = img.getAttribute('alt')!;
figcaption.innerHTML = caption;
figure.appendChild(figcaption);
}
}