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
Copy file name to clipboardExpand all lines: README.md
+58-56
Original file line number
Diff line number
Diff line change
@@ -54,6 +54,48 @@ While it may seem strange to see these two in the same file, this is one of the
54
54
55
55
### Additional Examples
56
56
57
+
<details>
58
+
<summary>Parsing Frontmatter</summary>
59
+
60
+
Markdown in general is often paired with frontmatter, and normally this means adding some extra custom processing to the way markdown is handled. To address this, `next-mdx-remote` comes with optional parsing of frontmatter, which can be enabled by passing `parseFrontmatter: true` to `serialize`.
_[`vfile-matter`](https://github.com/vfile/vfile-matter) is used to parse the frontmatter._
96
+
97
+
</details>
98
+
57
99
<details>
58
100
<summary>Passing custom data to a component with `scope`</summary>
59
101
@@ -133,7 +175,7 @@ export async function getStaticProps() {
133
175
Custom components from <code>MDXProvider</code><a id="mdx-provider"></a>
134
176
</summary>
135
177
136
-
If you want to make components available to any `<MDXRemote />` being rendered in your application, you can use [`<MDXProvider />`](https://mdxjs.com/advanced/components#mdxprovider) from `@mdx-js/react`.
178
+
If you want to make components available to any `<MDXRemote />` being rendered in your application, you can use [`<MDXProvider />`](https://mdxjs.com/docs/using-mdx/#mdx-provider) from `@mdx-js/react`.
137
179
138
180
```jsx
139
181
// pages/_app.jsx
@@ -180,7 +222,7 @@ export async function getStaticProps() {
180
222
Component names with dot (e.g. <code>motion.div</code>)
181
223
</summary>
182
224
183
-
Component names that contain a dot (`.`), such as those from `framer-motion`, can be rendered as long as the top-level namespace is declared in the MDX scope:
225
+
Component names that contain a dot (`.`), such as those from `framer-motion`, can be rendered the same way as other custom components, just pass `motion`in your components object.
184
226
185
227
```js
186
228
import { motion } from'framer-motion'
@@ -192,7 +234,7 @@ import { MDXRemote } from 'next-mdx-remote'
192
234
exportdefaultfunctionTestPage({ source }) {
193
235
return (
194
236
<div className="wrapper">
195
-
<MDXRemote {...source} scope={{ motion }} />
237
+
<MDXRemote {...source} components={{ motion }} />
196
238
</div>
197
239
)
198
240
}
@@ -246,9 +288,9 @@ export async function getStaticProps() {
246
288
247
289
This library exposes a function and a component, `serialize` and `<MDXRemote />`. These two are purposefully isolated into their own files -- `serialize` is intended to be run **server-side**, so within `getStaticProps`, which runs on the server/at build time. `<MDXRemote />` on the other hand is intended to be run on the client side, in the browser.
**`serialize`** consumes a string of MDX. It also can optionally be passed options which are [passed directly to MDX](https://mdxjs.com/advanced/plugins), and a scope object that can be included in the mdx scope. The function returns an object that is intended to be passed into `<MDXRemote />` directly.
293
+
**`serialize`** consumes a string of MDX. It can also optionally be passed options which are [passed directly to MDX](https://mdxjs.com/docs/extending-mdx/), and a scope object that can be included in the mdx scope. The function returns an object that is intended to be passed into `<MDXRemote />` directly.
252
294
253
295
```ts
254
296
serialize(
@@ -267,9 +309,8 @@ This library exposes a function and a component, `serialize` and `<MDXRemote />`
267
309
compilers: [],
268
310
filepath: '/some/file/path',
269
311
},
270
-
// Specify the target environment for the generated code. See esbuild docs:
271
-
// https://esbuild.github.io/api/#target
272
-
target: ['esnext'],
312
+
// Indicates whether or not to parse the frontmatter from the mdx source
313
+
parseFrontmatter: false,
273
314
}
274
315
)
275
316
```
@@ -284,51 +325,9 @@ This library exposes a function and a component, `serialize` and `<MDXRemote />`
284
325
<MDXRemote {...source} components={components} />
285
326
```
286
327
287
-
## Frontmatter & Custom Processing
288
-
289
-
Markdown in general is often paired with frontmatter, and normally this means adding some extra custom processing to the way markdown is handled. Luckily, this can be done entirely independently of `next-mdx-remote`, along with any extra custom processing necessary.
290
-
291
-
Let's walk through an example of how we could process frontmatter out of our MDX source:
// MDX text - can be from a local file, database, anywhere
314
-
constsource=`---
315
-
title: Test
316
-
---
317
-
318
-
Some **mdx** text, with a component <Test name={title}/>
319
-
`
320
-
321
-
const { content, data } =matter(source)
322
-
constmdxSource=awaitserialize(content, { scope: data })
323
-
return { props: { source: mdxSource, frontMatter: data } }
324
-
}
325
-
```
326
-
327
-
Nice and easy - since we get the content as a string originally and have full control, we can run any extra custom processing needed before passing it into `serialize`, and easily append extra data to the return value from `getStaticProps` without issue.
328
-
329
328
### Replacing default components
330
329
331
-
Rendering will use [`MDXProvider`](https://mdxjs.com/getting-started#mdxprovider) under the hood. This means you can replace HTML tags by custom components. Those components are listed in MDXJS [Table of components](https://mdxjs.com/table-of-components).
330
+
Rendering will use [`MDXProvider`](https://mdxjs.com/docs/using-mdx/#mdx-provider) under the hood. This means you can replace HTML tags by custom components. Those components are listed in MDXJS [Table of components](https://mdxjs.com/table-of-components/).
332
331
333
332
An example use case is rendering the content with your preferred styling library.
334
333
@@ -368,7 +367,7 @@ If you really insist though, check out [our official nextjs example implementati
368
367
369
368
### Environment Targets
370
369
371
-
The code generated by `next-mdx-remote`, which is used to actually render the MDX, is transformed to support: `>= node 12, es2020`.
370
+
The code generated by `next-mdx-remote`, which is used to actually render the MDX targets browsers with module support. If you need to support older browsers, consider transpiling the `compiledSource` output from `serialize`.
0 commit comments