-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdx-components.tsx
43 lines (40 loc) · 1.38 KB
/
mdx-components.tsx
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
import type { MDXComponents } from "mdx/types";
import Image, { ImageProps } from "next/image";
// hack to fix image props
type NewImageProps = Omit<
ImageProps,
"src" | "alt" | "width" | "height" | "placeholder"
> & {
src?: ImageProps["src"] | undefined;
alt?: ImageProps["alt"] | undefined;
width?: ImageProps["width"] | string | undefined;
height?: ImageProps["height"] | string | undefined;
placeholder?: ImageProps["placeholder"] | string | undefined;
};
// This file allows you to provide custom React components
// to be used in MDX files. You can import and use any
// React component you want, including components from
// other libraries.
// This file is required to use MDX in `app` directory.
export function useMDXComponents(components: MDXComponents): MDXComponents {
return {
// Allows customizing built-in components, e.g. to add styling.
// h1: ({ children }) => <h1 style={{ fontSize: "100px" }}>{children}</h1>,
img: (props: NewImageProps) => {
const newAlt: string =
typeof props.alt === "string" ? props.alt : "no alt provided";
const newSrc: string =
typeof props.src === "string" ? props.src : "nosrc";
return (
<Image
alt={newAlt}
width={Number(props.width)}
height={Number(props.height)}
src={newSrc}
loading="lazy"
/>
);
},
...components,
};
}