Unverified Commit 115eff72 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Turbopack: Support string without options for @next/mdx (#81713)

## What?

Adds support for `remarkPlugins: ['remark-gfm']` and `rehypePlugins:
['rehype-slug']`.

Previously you had to nest an array like `remarkPlugins:
[['remark-gfm']]` but that's a bug, that case is only for passing
options. Directly passing the string without options should be allowed.
This PR implements that.
parent c3e6680b
......@@ -727,8 +727,18 @@ const nextConfig = {
const withMDX = createMDX({
options: {
remarkPlugins: [],
rehypePlugins: [['rehype-katex', { strict: true, throwOnError: true }]],
remarkPlugins: [
// Without options
'remark-gfm',
// With options
['remark-toc', { heading: 'The Table' }],
],
rehypePlugins: [
// Without options
'rehype-slug',
// With options
['rehype-katex', { strict: true, throwOnError: true }],
],
},
})
......@@ -737,7 +747,7 @@ export default withMDX(nextConfig)
> **Good to know**:
>
> remark and rehype plugins without serializable options cannot be used yet with [Turbopack](/docs/app/api-reference/turbopack), due to [inability to pass JavaScript functions to Rust](https://github.com/vercel/next.js/issues/71819#issuecomment-2461802968)
> remark and rehype plugins without serializable options cannot be used yet with [Turbopack](/docs/app/api-reference/turbopack), because JavaScript functions can't be passed to Rust.
## Remote MDX
......
......@@ -21,7 +21,22 @@ declare namespace nextMDX {
*
* @see https://mdxjs.com/packages/mdx/#api
*/
options?: Options
options?: Options & {
remarkPlugins?:
| (
| string
| [name: string, options: any]
| NonNullable<Options['remarkPlugins']>[number]
)[]
| Options['remarkPlugins']
rehypePlugins?:
| (
| string
| [name: string, options: any]
| NonNullable<Options['rehypePlugins']>[number]
)[]
| Options['rehypePlugins']
}
}
}
......
......@@ -5,15 +5,22 @@ function interopDefault(mod) {
return mod.default || mod
}
async function importPluginForPath(pluginPath, projectRoot) {
const path = require.resolve(pluginPath, { paths: [projectRoot] })
return interopDefault(
// "use pathToFileUrl to make esm import()s work with absolute windows paths":
// on windows import("C:\\path\\to\\file") is not valid, so we need to use file:// URLs
// https://github.com/vercel/next.js/commit/fbf9e12de095e0237d4ba4aa6139d9757bd20be9
await import(process.platform === 'win32' ? pathToFileURL(path) : path)
)
}
async function importPlugin(plugin, projectRoot) {
if (Array.isArray(plugin) && typeof plugin[0] === 'string') {
const path = require.resolve(plugin[0], { paths: [projectRoot] })
plugin[0] = interopDefault(
// "use pathToFileUrl to make esm import()s work with absolute windows paths":
// on windows import("C:\\path\\to\\file") is not valid, so we need to use file:// URLs
// https://github.com/vercel/next.js/commit/fbf9e12de095e0237d4ba4aa6139d9757bd20be9
await import(process.platform === 'win32' ? pathToFileURL(path) : path)
)
plugin[0] = await importPluginForPath(plugin[0], projectRoot)
}
if (typeof plugin === 'string') {
plugin = await importPluginForPath(plugin, projectRoot)
}
return plugin
}
......
# The Table
# Remark plugin
Testing emoji: :dog: :+1:
## Todo List
- [ ] Implement
- [x] Implemented
......@@ -9,6 +9,9 @@ for (const type of ['with-mdx-rs', 'without-mdx-rs']) {
'@mdx-js/loader': '^2.2.1',
'@mdx-js/react': '^2.2.1',
'rehype-katex': '7.0.1',
'rehype-slug': '6.0.0',
'remark-gfm': '4.0.1',
'remark-toc': '9.0.0',
},
env: {
WITH_MDX_RS: type === 'with-mdx-rs' ? 'true' : 'false',
......@@ -62,10 +65,22 @@ for (const type of ['with-mdx-rs', 'without-mdx-rs']) {
})
if (type === 'without-mdx-rs') {
it('should run plugins', async () => {
const html = await next.render('/rehype-plugin')
it('should run rehype plugins', async () => {
const $ = await next.render$('/rehype-plugin')
const html = $('html').html()
expect(html.includes('<mi>C</mi>')).toBe(true)
expect(html.includes('<mi>L</mi>')).toBe(true)
expect($('#rehype-plugin').text()).toBe('Rehype plugin')
})
it('should run remark plugins', async () => {
const $ = await next.render$('/remark-plugin')
const html = $('html').html()
expect(html.includes('The Table')).toBe(true)
expect($('a[href="#todo-list"]').text()).toBe('Todo List')
expect($('input[type="checkbox"]').length).toBe(2)
expect($('input[type="checkbox"][checked]').length).toBe(1)
})
}
})
......
......@@ -2,8 +2,11 @@ import nextMDX from '@next/mdx'
const withMDX = nextMDX({
extension: /\.mdx?$/,
options: {
remarkPlugins: [],
rehypePlugins: [['rehype-katex', { strict: true, throwOnError: true }]],
remarkPlugins: ['remark-gfm', ['remark-toc', { heading: 'The Table' }]],
rehypePlugins: [
'rehype-slug',
['rehype-katex', { strict: true, throwOnError: true }],
],
},
})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment