/* * fancybox.js * - initialize fancybox library * - automatically wrap images */ document.addEventListener("DOMContentLoaded", function () { document .querySelectorAll(".markdown-body img:not(figure img, .no-fancybox)") .forEach((img) => { if (img.closest("figure")) return; // 跳过已经在
内的 let src = img.getAttribute("src"); let alt = img.getAttribute("alt") || ""; // 获取 alt 作为 title // 创建 标签包裹 let link = document.createElement("a"); link.setAttribute("href", src); link.setAttribute("data-fancybox", "gallery"); // Fancybox 组 link.setAttribute("data-caption", alt); // 设置 title link.setAttribute("aria-label", alt); // 辅助功能优化 // 创建
并添加

let figcaption = document.createElement("figcaption"); let captionTitle = document.createElement("h4"); captionTitle.textContent = alt; figcaption.appendChild(captionTitle); // 创建
并包裹 +
let figure = document.createElement("figure"); figure.appendChild(link); figure.appendChild(figcaption); // 替换原始 img img.parentNode.insertBefore(figure, img); link.appendChild(img); // 将 img 插入 }); }); Fancybox.bind("[data-fancybox]");