-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdx-components.tsx
49 lines (46 loc) · 1.72 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
44
45
46
47
48
49
import type { MDXComponents } from 'mdx/types'
import React from "react";
import Image, { ImageProps } from "next/image";
export function useMDXComponents(components: MDXComponents): MDXComponents {
const handleId = (string?: string) => {
if (!string) return '';
return string.replaceAll(/[^a-zA-Z ]/g, '').replaceAll(' ', '-').toLowerCase();
}
return {
h1: ({ children }) => {
return <h1 className="supertitle mb-md" id={handleId(children?.toString())}>{ children }</h1>;
},
h2: ({ children }) => {
return <h2 className="title mt-lg mb-md" id={handleId(children?.toString())}>{ children }</h2>;
},
h3: ({ children }) => {
return <h3 className="subtitle mt-lg mb-md" id={handleId(children?.toString())}>{ children }</h3>;
},
h4: ({ children }) => <h4 className="subtitle mt-md mb-sm size-sm" id={handleId(children?.toString())}>{children}</h4>,
p: ({ children }) => <p className="size-body mb-md" style={{ lineHeight: '1.6' }}>{children}</p>,
a: ({ children, href }) => {
const isLinkExternal: boolean|undefined = href?.startsWith('http') || href?.startsWith('mailto') || href?.startsWith('tel') || href?.startsWith('sms') || href?.startsWith('tg');
return (
<a href={href}
className="ms-link"
target={isLinkExternal ? '_blank' : '_self'}
rel="noopener noreferrer"
>
{children}
</a>
);
},
img: (props) => {
return (
<Image
width={1920}
height={1080}
style={{ width: '100%', height: '100%', objectFit: 'cover', objectPosition: 'center' }}
className="r-lg"
{ ...(props as ImageProps) }
/>
);
},
...components,
}
}