generated from betagouv/template-nextjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdx-components.tsx
67 lines (64 loc) · 2.05 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
64
65
66
67
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";
import { ReactNode } from "react";
import { MDXComponents } from "mdx/types";
export const mdxComponents = {
h1: ({ children }: { children: ReactNode }) => (
<h1 className={fr.cx("fr-h1")}>{children}</h1>
),
h2: ({ children }: { children: ReactNode }) => (
<h2 className={fr.cx("fr-mt-3w", "fr-h2")}>{children}</h2>
),
h3: ({ children }: { children: ReactNode }) => (
<h3 className={fr.cx("fr-mt-3w", "fr-h3")}>{children}</h3>
),
h4: ({ children }: { children: ReactNode }) => (
<h4 className={fr.cx("fr-mt-3w", "fr-h4")}>{children}</h4>
),
table: (props: { children: ReactNode }) => {
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: { href: string }) => {
if (
props.href &&
(props.href?.startsWith("http") || props.href?.startsWith("//"))
) {
return <Link {...props} target="_blank" rel="noopener noreferrer" />;
}
return <Link {...props} />;
},
blockquote: (props: { children: ReactNode }) => {
if (
props.children &&
Array.isArray(props.children) &&
props.children.length === 3
) {
return <CallOut>{props.children[1].props.children}</CallOut>;
}
return <CallOut>{props.children}</CallOut>;
},
};
// customize how MDX components are rendered - use DSFR components when possible
export function useMDXComponents(components: MDXComponents): MDXComponents {
// @ts-ignore TODO
return {
...mdxComponents,
...components,
};
}