-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmdx-components.tsx
More file actions
51 lines (46 loc) · 1.45 KB
/
mdx-components.tsx
File metadata and controls
51 lines (46 loc) · 1.45 KB
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
import type { MDXComponents } from "mdx/types";
import React from "react";
const withDataContent = (
Component: React.ComponentType<React.HTMLAttributes<HTMLHeadingElement>>,
) => {
return (props: React.HTMLAttributes<HTMLHeadingElement>) => {
const { children, ...rest } = props;
const textContent = String(children);
// generate a unique ID based on the text content
const headingId =
props.id || textContent.toLowerCase().replace(/\s+/g, "-");
return (
<Component {...rest} id={headingId} data-content={textContent}>
{children}
</Component>
);
};
};
export function useMDXComponents(components: MDXComponents): MDXComponents {
return {
...components,
h1: withDataContent((props) => (
<h1 className="text-3xl font-bold mt-8 mb-4" {...props} />
)),
h2: withDataContent((props) => (
<h2 className="text-2xl font-semibold mt-6 mb-3" {...props} />
)),
h3: withDataContent((props) => (
<h3 className="text-xl font-semibold mt-4 mb-2" {...props} />
)),
h4: (props) => <h4 className="text-lg font-semibold mb-4" {...props} />,
p: (props) => <p className="text-lg text-gray-700 mb-8" {...props} />,
ol: (props) => (
<ol
className="list-decimal ml-6 text-gray-700 text-lg space-y-2"
{...props}
/>
),
ul: (props) => (
<ul
className="list-disc ml-6 text-gray-700 text-lg space-y-2"
{...props}
/>
),
};
}