You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -6,12 +6,23 @@ excerpt: How Contentlayer processes MDX when using local files as the content so
6
6
7
7
MDX brings JSX components to markdown, which can provide power and flexibility to the main body area of a content piece.
8
8
9
-
## MDX Content
9
+
```md
10
+
# Hello, World!
10
11
11
-
Contentlayer supports MDX processing via [`mdx-bundler`](https://github.com/kentcdodds/mdx-bundler). By default, Contentlayer processes the main content area of `.md` and `.mdx` files as markdown. You can enable this behavior using the `contentType` option in your document type definition.
12
+
This is my first MDX file. Here's a button element <button>Click me!</button>.
13
+
14
+
<MyComponent />
15
+
```
16
+
17
+
## MDX Content Type
18
+
19
+
Contentlayer supports MDX processing via [`mdx-bundler`](https://github.com/kentcdodds/mdx-bundler). By default, Contentlayer processes the main content area of `.md` and `.mdx` files as markdown.
20
+
21
+
You can enable MDX processing by setting the `contentType` option in your document type definition to `'mdx'` in your Contentlayer configuration.
12
22
13
23
```js
14
-
defineDocumentType(() => ({
24
+
// contentlayer.config.ts
25
+
constPost=defineDocumentType(() => ({
15
26
name:'Post',
16
27
filePathPattern:`**/*.mdx`,
17
28
contentType:'mdx',
@@ -21,44 +32,147 @@ defineDocumentType(() => ({
21
32
}))
22
33
```
23
34
24
-
## Usage from Next.js
35
+
## Usage in Next.js
36
+
37
+
To parse the contents of a MDX file in a Next.js page, use the `useMDXComponent` hook provided by `next-contentlayer/hooks`.
38
+
39
+
### Pages Directory
40
+
41
+
Here's an example implementation in Next.js using the legacy `/pages` directory:
exportdefaultfunction Page({ post }: { post:Post }) {
69
+
// Parse the MDX file via the useMDXComponent hook.
70
+
const MDXContent =useMDXComponent(post.body.code)
71
+
72
+
return (
73
+
<div>
74
+
{/* Some code ... */}
75
+
<MDXContent />
76
+
</div>
77
+
)
78
+
}
79
+
```
80
+
81
+
In this example, the `getStaticPaths` function returns an array of all valid post slugs for pre-rendering and the `getStaticProps` function retrieves the relevant MDX post for the current page. The `useMDXComponent` hook then processes the MDX file via [`mdx-bundler`](https://github.com/kentcdodds/mdx-bundler). Finally, the rendered content is displayed using the `MDXContent` component returned by the `useMDXComponent` hook.
82
+
83
+
### App Directory
84
+
85
+
Here's an example implementation in Next.js version 13 and above using the new `/app` directory:
const post =allPosts.find((post) =>post._raw.flattenedPath===params.slug)
102
+
103
+
// 404 if the post does not exist.
104
+
if (!post) notFound()
105
+
106
+
// Parse the MDX file via the useMDXComponent hook.
39
107
const MDXContent =useMDXComponent(post.body.code)
40
-
108
+
41
109
return (
42
110
<div>
43
111
{/* Some code ... */}
44
-
<MDXContentcomponents={{ MyButton }}/>
112
+
<MDXContent />
45
113
</div>
46
114
)
47
115
}
116
+
```
117
+
118
+
In this example, the `generateStaticParams` function returns an array of all valid post slugs for pre-rendering. The `useMDXComponent` hook then processes the MDX file for the current page via [`mdx-bundler`](https://github.com/kentcdodds/mdx-bundler). Finally, the rendered content is displayed using the `MDXContent` component returned by the `useMDXComponent` hook.
48
119
49
-
exportdefaultPage
120
+
## Custom MDX Components
121
+
122
+
You can override built-in HTML elements and create your own custom React components using the `components` prop of the `MDXContent` component returned by the `useMDXComponent` hook.
const post =allPosts.find((post) =>post._raw.flattenedPath===params.slug)
143
+
144
+
// 404 if the post does not exist.
145
+
if (!post) notFound()
146
+
147
+
// Parse the MDX file via the useMDXComponent hook.
148
+
const MDXContent =useMDXComponent(post.body.code)
149
+
150
+
return (
151
+
<div>
152
+
{/* Some code ... */}
153
+
<MDXContentcomponents={mdxComponents} /> {/* <= Include your custom MDX components here */}
154
+
</div>
155
+
)
156
+
}
50
157
```
51
158
159
+
In this example, we define a custom `mdxComponents` object that overrides the default `<a>` element to use the `next/link` component and adds a custom `MyComponent` component. Then, we include our custom components by passing the `mdxComponents` object to the `components` prop of the `MDXContent` component.
160
+
52
161
## MDX Plugins (remark/rehype)
53
162
54
163
You can add [remark](https://github.com/remarkjs/remark) and [rehype](https://github.com/rehypejs/rehype) plugins inside the `mdx` property when calling `makeSource` in your Contentlayer configuration.
0 commit comments