-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmdx-components.tsx
More file actions
44 lines (35 loc) · 1.07 KB
/
mdx-components.tsx
File metadata and controls
44 lines (35 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import NextImage, { ImageProps } from "next/image";
import * as runtime from "react/jsx-runtime";
// Custom Image component that handles the fill prop correctly
function Image(props: Omit<ImageProps, "fill"> & { fill?: boolean }) {
const { fill, ...rest } = props;
if (fill) {
return <NextImage {...rest} fill />;
}
return <NextImage {...rest} />;
}
// Custom img component for standard HTML img tags in MDX
function img(props: React.ImgHTMLAttributes<HTMLImageElement>) {
const { src, alt, ...rest } = props;
if (!src) return null;
return (
// eslint-disable-next-line @next/next/no-img-element
<img src={src} alt={alt || ""} className="rounded-lg" {...rest} />
);
}
const sharedComponents = {
Image,
img,
};
// Parse MDX code and return React component
const useMDXComponent = (code: string) => {
const fn = new Function(code);
return fn({ ...runtime }).default;
};
interface MdxProps {
code: string;
}
export function Mdx({ code }: MdxProps) {
const Component = useMDXComponent(code);
return <Component components={sharedComponents} />;
}