-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdx-components.tsx
141 lines (133 loc) · 3.52 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import type { MDXComponents } from "mdx/types";
import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { vscDarkPlus } from "react-syntax-highlighter/dist/cjs/styles/prism";
import { slug } from "github-slugger";
/* Types */
import type {
AnchorHTMLAttributes,
DetailedHTMLProps,
HTMLAttributes,
ReactNode,
} from "react";
/**
* Converts a react node to a slug
*
* Usually the value is a string, but sometimes it contains
* a `<code>` element, which is a react node. This function
* converts the value to a string.
*/
const makeSlug = (value: ReactNode | ReactNode[]): string => {
if (!Array.isArray(value)) return slug(value?.toString() ?? "");
return slug(
value
.map((node) => {
if (
typeof node === "string" ||
typeof node === "number" ||
typeof node === "boolean"
)
return node;
if (isNodeWithChildren(node)) {
return makeSlug(node.props.children);
}
return "";
})
.join(""),
);
};
const isNodeWithChildren = (
value: unknown,
): value is { props: { children: string | number | boolean | Array<any> } } => {
return (
typeof value === "object" &&
value !== null &&
"props" in value &&
value.props !== null &&
typeof value.props === "object" &&
"children" in value.props &&
value.props.children !== null &&
typeof value.props.children !== "undefined" &&
(typeof value.props.children === "string" ||
typeof value.props.children === "number" ||
typeof value.props.children === "boolean" ||
Array.isArray(value.props.children))
);
};
export const components = {
a: (
props: DetailedHTMLProps<
AnchorHTMLAttributes<HTMLAnchorElement>,
HTMLAnchorElement
>,
) => <a {...props} target="_blank" rel="noopener noreferrer" />,
pre: function Pre({ children }: { children?: ReactNode }) {
return <>{children}</>;
},
code(props: DetailedHTMLProps<HTMLAttributes<HTMLElement>, HTMLElement>) {
const { children, className, ...rest } = props;
const match = /language-(\w+)/.exec(className || "");
return match ? (
<SyntaxHighlighter language={match[1]} style={vscDarkPlus}>
{String(children)}
</SyntaxHighlighter>
) : (
<code {...rest} className={className}>
{children}
</code>
);
},
h1(
props: DetailedHTMLProps<
HTMLAttributes<HTMLHeadingElement>,
HTMLHeadingElement
>,
) {
return <h1 id={makeSlug(props.children)} {...props} />;
},
h2(
props: DetailedHTMLProps<
HTMLAttributes<HTMLHeadingElement>,
HTMLHeadingElement
>,
) {
return <h2 id={makeSlug(props.children)} {...props} />;
},
h3(
props: DetailedHTMLProps<
HTMLAttributes<HTMLHeadingElement>,
HTMLHeadingElement
>,
) {
return <h3 id={makeSlug(props.children)} {...props} />;
},
h4(
props: DetailedHTMLProps<
HTMLAttributes<HTMLHeadingElement>,
HTMLHeadingElement
>,
) {
return <h4 id={makeSlug(props.children)} {...props} />;
},
h5(
props: DetailedHTMLProps<
HTMLAttributes<HTMLHeadingElement>,
HTMLHeadingElement
>,
) {
return <h5 id={makeSlug(props.children)} {...props} />;
},
h6(
props: DetailedHTMLProps<
HTMLAttributes<HTMLHeadingElement>,
HTMLHeadingElement
>,
) {
return <h6 id={makeSlug(props.children)} {...props} />;
},
};
export function useMDXComponents(original: MDXComponents): MDXComponents {
return {
...original,
...components,
};
}