This commit is contained in:
chengyongru 2025-12-10 19:01:14 +01:00 committed by GitHub
commit 2762d726d3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 104 additions and 0 deletions

View File

@ -72,6 +72,7 @@ const config: QuartzConfig = {
Plugin.CrawlLinks({ markdownLinkResolution: "shortest" }), Plugin.CrawlLinks({ markdownLinkResolution: "shortest" }),
Plugin.Description(), Plugin.Description(),
Plugin.Latex({ renderEngine: "katex" }), Plugin.Latex({ renderEngine: "katex" }),
Plugin.ViewImage(),
], ],
filters: [Plugin.RemoveDrafts()], filters: [Plugin.RemoveDrafts()],
emitters: [ emitters: [

View File

@ -11,3 +11,4 @@ export { SyntaxHighlighting } from "./syntax"
export { TableOfContents } from "./toc" export { TableOfContents } from "./toc"
export { HardLineBreaks } from "./linebreaks" export { HardLineBreaks } from "./linebreaks"
export { RoamFlavoredMarkdown } from "./roam" export { RoamFlavoredMarkdown } from "./roam"
export { ViewImage } from "./viewImage"

View File

@ -0,0 +1,102 @@
import { QuartzTransformerPlugin } from "../types"
export const ViewImage: QuartzTransformerPlugin = () => {
return {
name: "ViewImage",
externalResources() {
return {
css: [
{
content: `
img {
cursor: zoom-in !important;
border: 2px dashed #284b63 !important;
transition: border-color 0.2s ease !important;
}
img:hover {
border-color: #1a365d !important;
}
`,
inline: true,
},
],
js: [
{
src: "https://cdn.jsdelivr.net/gh/Tokinx/ViewImage/view-image.min.js",
loadTime: "afterDOMReady",
contentType: "external",
},
{
script: `
function initViewImage() {
if (window.ViewImage) {
const existingImages = document.querySelectorAll('img[data-viewimage]');
existingImages.forEach(img => {
img.removeAttribute('data-viewimage');
const newImg = img.cloneNode(true);
img.parentNode.replaceChild(newImg, img);
});
ViewImage.init('img');
console.log('ViewImage灯箱插件已初始化处理了', document.querySelectorAll('img').length, '张图片');
} else {
console.error('ViewImage库未加载成功');
}
}
function setupViewImageObserver() {
const observer = new MutationObserver(function(mutations) {
let shouldReinit = false;
mutations.forEach(function(mutation) {
if (mutation.type === 'childList') {
mutation.addedNodes.forEach(function(node) {
if (node.nodeType === Node.ELEMENT_NODE) {
const element = node;
if (element.tagName === 'IMG' || element.querySelector('img')) {
shouldReinit = true;
}
}
});
}
});
if (shouldReinit) {
console.log('检测到页面内容变化,重新初始化 ViewImage');
setTimeout(initViewImage, 50);
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
return observer;
}
document.addEventListener('DOMContentLoaded', function() {
initViewImage();
setupViewImageObserver();
});
document.addEventListener('nav', function() {
console.log('SPA 导航事件触发,准备重新初始化 ViewImage');
setTimeout(function() {
initViewImage();
}, 200);
});
`,
loadTime: "afterDOMReady",
contentType: "inline",
},
],
}
},
}
}
declare module "vfile" {
interface DataMap {
viewImage?: boolean
}
}