forked from SocialGouv/template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdx-components.tsx
63 lines (61 loc) · 1.97 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import type { MDXComponents } from "mdx/types";
import Image, { ImageProps } from "next/image";
import Link from "next/link";
import { CallOut } from "@codegouvfr/react-dsfr/CallOut";
import { Table } from "@codegouvfr/react-dsfr/Table";
import { fr } from "@codegouvfr/react-dsfr";
// customize how MDX components are rendered - use DSFR components when possible
export function useMDXComponents(components: MDXComponents): MDXComponents {
return {
h1: ({ children }) => <h1 className={fr.cx("fr-h1")}>{children}</h1>,
h2: ({ children }) => (
<h2 className={fr.cx("fr-mt-3w", "fr-h2")}>{children}</h2>
),
h3: ({ children }) => (
<h3 className={fr.cx("fr-mt-3w", "fr-h3")}>{children}</h3>
),
h4: ({ children }) => (
<h4 className={fr.cx("fr-mt-3w", "fr-h4")}>{children}</h4>
),
// @ts-ignore
table: (props) => {
if (
props.children &&
Array.isArray(props.children) &&
props.children.length === 2
) {
const [head, body] = props.children;
const headers = head.props.children.props.children.map(
(child: any) => child.props.children
);
const data = body.props.children.map((row: any) =>
row.props.children.map((cell: any) => cell.props.children)
);
return <Table headers={headers} data={data} />;
}
return <div></div>;
},
a: (props) => {
if (
props.href &&
(props.href?.startsWith("http") || props.href?.startsWith("//"))
) {
//@ts-ignore
return <Link {...props} target="_blank" rel="noopener noreferrer" />;
}
//@ts-ignore
return <Link {...props} />;
},
blockquote: (props) => {
if (
props.children &&
Array.isArray(props.children) &&
props.children.length === 3
) {
return <CallOut>{props.children[1].props.children}</CallOut>;
}
return <CallOut>{props.children}</CallOut>;
},
...components,
};
}