Skip to content

Commit 8de423c

Browse files
authored
Fix types of @next/mdx (#82238)
### What? This fixes a couple of things: - Although it’s rare for MDX plugins to accept more than 1 option, they may accept any number of arguments. - In addition to `remarkPlugins` and `rehypePlugins`, now `recmaPlugins` is also patched to accept strings - TypeScript property Descriptions are preserved for patched values. ### Why? Because it is broken. ### How? Fixes #77297
1 parent 498349c commit 8de423c

File tree

4 files changed

+25
-15
lines changed

4 files changed

+25
-15
lines changed

packages/next-mdx/index.d.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import type { RuleSetConditionAbsolute } from 'webpack'
44

55
type WithMDX = (config: NextConfig) => NextConfig
66

7+
type PluggableListName = 'remarkPlugins' | 'rehypePlugins' | 'recmaPlugins'
8+
79
declare namespace nextMDX {
810
interface NextMDXOptions {
911
/**
@@ -21,21 +23,16 @@ declare namespace nextMDX {
2123
*
2224
* @see https://mdxjs.com/packages/mdx/#api
2325
*/
24-
options?: Options & {
25-
remarkPlugins?:
26-
| (
27-
| string
28-
| [name: string, options: any]
29-
| NonNullable<Options['remarkPlugins']>[number]
30-
)[]
31-
| Options['remarkPlugins']
32-
rehypePlugins?:
33-
| (
34-
| string
35-
| [name: string, options: any]
36-
| NonNullable<Options['rehypePlugins']>[number]
37-
)[]
38-
| Options['rehypePlugins']
26+
options?: {
27+
[Key in keyof Options]: Key extends PluggableListName
28+
?
29+
| (
30+
| string
31+
| [name: string, ...options: any[]]
32+
| NonNullable<Options[Key]>[number]
33+
)[]
34+
| Options[Key]
35+
: Options[Key]
3936
}
4037
}
4138
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Recma plugin
2+
3+
{/* eslint-disable-next-line no-undef, no-unused-expressions */}
4+
{typeof filepath === 'undefined' ? null : filepath}

test/e2e/app-dir/mdx/mdx.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ for (const type of ['with-mdx-rs', 'without-mdx-rs']) {
88
'@next/mdx': 'canary',
99
'@mdx-js/loader': '^2.2.1',
1010
'@mdx-js/react': '^2.2.1',
11+
'recma-export-filepath': '1.2.0',
1112
'rehype-katex': '7.0.1',
1213
'rehype-slug': '6.0.0',
1314
'remark-gfm': '4.0.1',
@@ -65,6 +66,13 @@ for (const type of ['with-mdx-rs', 'without-mdx-rs']) {
6566
})
6667

6768
if (type === 'without-mdx-rs') {
69+
it('should run recma plugins', async () => {
70+
const $ = await next.render$('/recma-plugin')
71+
const html = $('html').html()
72+
expect(html.includes('recma-plugin/page.mdx')).toBe(true)
73+
expect($('#recma-plugin').text()).toBe('Recma plugin')
74+
})
75+
6876
it('should run rehype plugins', async () => {
6977
const $ = await next.render$('/rehype-plugin')
7078
const html = $('html').html()

test/e2e/app-dir/mdx/next.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const withMDX = nextMDX({
77
'rehype-slug',
88
['rehype-katex', { strict: true, throwOnError: true }],
99
],
10+
recmaPlugins: ['recma-export-filepath'],
1011
},
1112
})
1213

0 commit comments

Comments
 (0)