Skip to content

Commit a750812

Browse files
committed
Fix MDX image rendering
1 parent 3d4e2a0 commit a750812

3 files changed

Lines changed: 43 additions & 5 deletions

File tree

sites/docs/app/global.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1173,7 +1173,7 @@ figure.shiki code {
11731173
line-height: 1.45;
11741174
}
11751175

1176-
.docs-physics-image {
1176+
.docs-mdx-image {
11771177
display: block;
11781178
max-width: 100%;
11791179
height: auto;

sites/docs/lib/mdx-components.tsx

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,48 @@ import defaultMdxComponents from 'fumadocs-ui/mdx';
22
import type { ComponentProps } from 'react';
33
import { withBasePath } from './paths';
44

5+
type StaticImageLike = {
6+
src: string;
7+
width?: number;
8+
height?: number;
9+
};
10+
11+
type MdxImageProps = Omit<ComponentProps<'img'>, 'src'> & {
12+
src?: ComponentProps<'img'>['src'] | StaticImageLike;
13+
};
14+
515
function Anchor({ href, ...props }: ComponentProps<'a'>) {
616
return <a href={href ? withBasePath(href) : href} {...props} />;
717
}
818

9-
function Image({ className, src, ...props }: ComponentProps<'img'>) {
19+
function resolveImage(src: MdxImageProps['src']) {
20+
if (!src) return {};
21+
22+
if (typeof src === 'string') {
23+
return { src: withBasePath(src) };
24+
}
25+
26+
if (typeof src === 'object' && 'src' in src && typeof src.src === 'string') {
27+
return {
28+
src: withBasePath(src.src),
29+
width: src.width,
30+
height: src.height,
31+
};
32+
}
33+
34+
return {};
35+
}
36+
37+
function Image({ className, height, src, width, ...props }: MdxImageProps) {
38+
const image = resolveImage(src);
39+
1040
return (
1141
<img
12-
className={className ?? 'docs-physics-image'}
13-
src={typeof src === 'string' ? withBasePath(src) : src}
1442
{...props}
43+
className={className ?? 'docs-mdx-image'}
44+
height={height ?? image.height}
45+
src={image.src}
46+
width={width ?? image.width}
1547
/>
1648
);
1749
}

sites/docs/lib/paths.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
export const basePath = process.env.NEXT_PUBLIC_BASE_PATH ?? '';
22

33
export function withBasePath(path: string): string {
4-
if (!basePath || !path.startsWith('/') || path.startsWith('//')) {
4+
if (
5+
!basePath ||
6+
!path.startsWith('/') ||
7+
path.startsWith('//') ||
8+
path === basePath ||
9+
path.startsWith(`${basePath}/`)
10+
) {
511
return path;
612
}
713

0 commit comments

Comments
 (0)