Skip to content

Commit 5b80de0

Browse files
committed
update
1 parent 8ad82be commit 5b80de0

1 file changed

Lines changed: 21 additions & 13 deletions

File tree

rehype-image.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,27 @@ export default function rehypeCustomizeImageSrc() {
8989

9090
let newAttrs = `src="${p1}${separator}x-oss-process=image/auto-orient,1/resize,w_2000/format,webp" data-lightense-src="${p1}" data-pswp-src="${p1}"`;
9191

92-
const finalWidth = meta?.width || origWidth;
93-
const finalHeight = meta?.height || origHeight;
94-
95-
if (finalWidth && finalHeight) {
96-
const ratio = (Number(finalWidth) / Number(finalHeight)).toFixed(4);
97-
const loadingAttr = isFirstImage
98-
? 'loading="eager" fetchpriority="high"'
99-
: 'loading="lazy"';
100-
newAttrs += ` width="${finalWidth}" height="${finalHeight}" data-pswp-width="${finalWidth}" data-pswp-height="${finalHeight}" style="aspect-ratio: ${ratio};" ${loadingAttr}`;
101-
} else if (isFirstImage) {
102-
newAttrs += ' loading="eager" fetchpriority="high"';
103-
} else {
104-
newAttrs += ' loading="lazy"';
92+
// Use metadata for aspect-ratio and lightbox, user-specified for display size
93+
const metaWidth = meta?.width;
94+
const metaHeight = meta?.height;
95+
96+
const loadingAttr = isFirstImage
97+
? 'loading="eager" fetchpriority="high"'
98+
: 'loading="lazy"';
99+
newAttrs += ` ${loadingAttr}`;
100+
101+
// Add lightbox dimensions from metadata
102+
if (metaWidth && metaHeight) {
103+
const ratio = (Number(metaWidth) / Number(metaHeight)).toFixed(4);
104+
newAttrs += ` data-pswp-width="${metaWidth}" data-pswp-height="${metaHeight}" style="aspect-ratio: ${ratio};"`;
105+
}
106+
107+
// User-specified dimensions for display (if provided)
108+
if (origWidth) {
109+
newAttrs += ` width="${origWidth}"`;
110+
}
111+
if (origHeight) {
112+
newAttrs += ` height="${origHeight}"`;
105113
}
106114

107115
const otherAttrs = fullMatch

0 commit comments

Comments
 (0)