Skip to content

Commit b5470a6

Browse files
committed
fix external image bug, update to pass all props, & ensure fallback size is applied
1 parent 041b40d commit b5470a6

File tree

2 files changed

+40
-10
lines changed

2 files changed

+40
-10
lines changed

src/components/docImage.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {DocImageClient} from './docImageClient';
66

77
export default function DocImage({
88
src,
9+
width: propsWidth,
10+
height: propsHeight,
911
...props
1012
}: Omit<React.HTMLProps<HTMLImageElement>, 'ref' | 'placeholder'>) {
1113
const {path: pagePath} = serverContext();
@@ -34,20 +36,26 @@ export default function DocImage({
3436
// parse the size from the URL hash (set by remark-image-size.js)
3537
const srcURL = new URL(src, 'https://example.com');
3638
const imgPath = srcURL.pathname;
37-
const [width, height] = srcURL.hash // #wxh
39+
const dimensions = srcURL.hash // #wxh
3840
.slice(1)
3941
.split('x')
4042
.map(s => parseInt(s, 10));
43+
44+
// Use parsed dimensions, fallback to props, then default to 800
45+
const width = !isNaN(dimensions[0]) ? dimensions[0] :
46+
(typeof propsWidth === 'number' ? propsWidth :
47+
typeof propsWidth === 'string' ? parseInt(propsWidth, 10) || 800 : 800);
48+
const height = !isNaN(dimensions[1]) ? dimensions[1] :
49+
(typeof propsHeight === 'number' ? propsHeight :
50+
typeof propsHeight === 'string' ? parseInt(propsHeight, 10) || 800 : 800);
4151

4252
return (
4353
<DocImageClient
4454
src={src}
4555
imgPath={imgPath}
4656
width={width}
4757
height={height}
48-
alt={props.alt ?? ''}
49-
style={props.style}
50-
className={props.className}
58+
{...props}
5159
/>
5260
);
5361
}

src/components/docImageClient.tsx

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@ import Image from 'next/image';
44

55
import {ImageLightbox} from './imageLightbox';
66

7-
interface DocImageClientProps {
8-
alt: string;
7+
interface DocImageClientProps extends Omit<React.HTMLProps<HTMLImageElement>, 'ref' | 'placeholder' | 'src' | 'width' | 'height'> {
98
height: number;
109
imgPath: string;
1110
src: string;
1211
width: number;
13-
className?: string;
14-
style?: React.CSSProperties;
1512
}
1613

1714
export function DocImageClient({
@@ -22,6 +19,7 @@ export function DocImageClient({
2219
alt,
2320
style,
2421
className,
22+
...props
2523
}: DocImageClientProps) {
2624
const handleContextMenu = (e: React.MouseEvent) => {
2725
e.preventDefault(); // Prevent default context menu
@@ -42,9 +40,32 @@ export function DocImageClient({
4240
}
4341
};
4442

43+
// Check if dimensions are valid (not NaN) for Next.js Image
44+
const isValidDimensions = !isNaN(width) && !isNaN(height) && width > 0 && height > 0;
45+
46+
// For external images or invalid dimensions, fall back to regular img tag
47+
if (src.startsWith('http') || !isValidDimensions) {
48+
return (
49+
<div onContextMenu={handleContextMenu} onClick={handleClick}>
50+
{/* eslint-disable-next-line @next/next/no-img-element */}
51+
<img
52+
src={src}
53+
alt={alt ?? ''}
54+
style={{
55+
width: '100%',
56+
height: 'auto',
57+
...style,
58+
}}
59+
className={className}
60+
{...props}
61+
/>
62+
</div>
63+
);
64+
}
65+
4566
return (
4667
<div onContextMenu={handleContextMenu} onClick={handleClick}>
47-
<ImageLightbox src={src} alt={alt} width={width} height={height}>
68+
<ImageLightbox src={src} alt={alt ?? ''} width={width} height={height}>
4869
<Image
4970
src={src}
5071
width={width}
@@ -55,7 +76,8 @@ export function DocImageClient({
5576
...style,
5677
}}
5778
className={className}
58-
alt={alt}
79+
alt={alt ?? ''}
80+
{...props}
5981
/>
6082
</ImageLightbox>
6183
</div>

0 commit comments

Comments
 (0)