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 = { ...@@ -727,8 +727,18 @@ const nextConfig = {
const withMDX = createMDX({ const withMDX = createMDX({
options: { options: {
remarkPlugins: [], remarkPlugins: [
rehypePlugins: [['rehype-katex', { strict: true, throwOnError: true }]], // 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) ...@@ -737,7 +747,7 @@ export default withMDX(nextConfig)
> **Good to know**: > **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 ## Remote MDX
......
...@@ -21,7 +21,22 @@ declare namespace nextMDX { ...@@ -21,7 +21,22 @@ declare namespace nextMDX {
* *
* @see https://mdxjs.com/packages/mdx/#api * @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) { ...@@ -5,15 +5,22 @@ function interopDefault(mod) {
return mod.default || 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) { async function importPlugin(plugin, projectRoot) {
if (Array.isArray(plugin) && typeof plugin[0] === 'string') { if (Array.isArray(plugin) && typeof plugin[0] === 'string') {
const path = require.resolve(plugin[0], { paths: [projectRoot] }) plugin[0] = await importPluginForPath(plugin[0], projectRoot)
plugin[0] = interopDefault( }
// "use pathToFileUrl to make esm import()s work with absolute windows paths": if (typeof plugin === 'string') {
// on windows import("C:\\path\\to\\file") is not valid, so we need to use file:// URLs plugin = await importPluginForPath(plugin, projectRoot)
// https://github.com/vercel/next.js/commit/fbf9e12de095e0237d4ba4aa6139d9757bd20be9
await import(process.platform === 'win32' ? pathToFileURL(path) : path)
)
} }
return plugin 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']) { ...@@ -9,6 +9,9 @@ for (const type of ['with-mdx-rs', 'without-mdx-rs']) {
'@mdx-js/loader': '^2.2.1', '@mdx-js/loader': '^2.2.1',
'@mdx-js/react': '^2.2.1', '@mdx-js/react': '^2.2.1',
'rehype-katex': '7.0.1', 'rehype-katex': '7.0.1',
'rehype-slug': '6.0.0',
'remark-gfm': '4.0.1',
'remark-toc': '9.0.0',
}, },
env: { env: {
WITH_MDX_RS: type === 'with-mdx-rs' ? 'true' : 'false', WITH_MDX_RS: type === 'with-mdx-rs' ? 'true' : 'false',
...@@ -62,10 +65,22 @@ for (const type of ['with-mdx-rs', 'without-mdx-rs']) { ...@@ -62,10 +65,22 @@ for (const type of ['with-mdx-rs', 'without-mdx-rs']) {
}) })
if (type === 'without-mdx-rs') { if (type === 'without-mdx-rs') {
it('should run plugins', async () => { it('should run rehype plugins', async () => {
const html = await next.render('/rehype-plugin') const $ = await next.render$('/rehype-plugin')
const html = $('html').html()
expect(html.includes('<mi>C</mi>')).toBe(true) expect(html.includes('<mi>C</mi>')).toBe(true)
expect(html.includes('<mi>L</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' ...@@ -2,8 +2,11 @@ import nextMDX from '@next/mdx'
const withMDX = nextMDX({ const withMDX = nextMDX({
extension: /\.mdx?$/, extension: /\.mdx?$/,
options: { options: {
remarkPlugins: [], remarkPlugins: ['remark-gfm', ['remark-toc', { heading: 'The Table' }]],
rehypePlugins: [['rehype-katex', { strict: true, throwOnError: true }]], 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