-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdx-components.tsx
64 lines (56 loc) · 1.82 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
import type { MDXComponents } from "mdx/types";
import Code from "./reusable-code";
import React from "react";
import Image from "next/image";
// React component you want, including inline styles,
// components from other libraries, and more.'
function slugify(str: any) {
return str
.toString()
.toLowerCase()
.trim() // Remove whitespace from both ends of a string
.replace(/\s+/g, "-") // Replace spaces with -
.replace(/&/g, "-and-") // Replace & with 'and'
.replace(/[^\w\-]+/g, "") // Remove all non-word characters except for -
.replace(/\-\-+/g, "-"); // Replace multiple - with single -
}
function createHeading(level: any) {
const Heading = ({ children }: any) => {
const slug = slugify(children);
return React.createElement(
`h${level}`,
{ id: slug },
React.createElement(
"a",
{
href: `#${slug}`,
className: "",
},
children // Ensure the children (text content) are inside the <a> tag
)
);
};
Heading.displayName = `Heading${level}`;
return Heading;
}
function RoundedImage(props: any) {
return <Image alt={props.alt} className="mt-4 rounded-lg" {...props} />;
}
export function useMDXComponents(components: MDXComponents): MDXComponents {
return {
h1: createHeading(1),
h2: createHeading(2),
h3: createHeading(3),
h4: createHeading(4),
h5: createHeading(5),
h6: createHeading(6),
Image: RoundedImage,
p: (props: any) => <p className="mb-4" {...props} />,
ul: (props: any) => <ul className="list-disc pl-6 mb-4" {...props} />,
ol: (props: any) => <ol className="list-decimal pl-6 mb-4" {...props} />,
li: (props: any) => <li className="mb-2" {...props} />,
pre: (props: any) => <pre className=" p-2 rounded mb-4" {...props} />,
code: Code,
...components,
};
}