Commit 7c76bfe 1 parent 8a6242a commit 7c76bfe Copy full SHA for 7c76bfe
File tree 3 files changed +19
-12
lines changed
eslint-plugin-mdx/src/rules
3 files changed +19
-12
lines changed Original file line number Diff line number Diff line change @@ -24,7 +24,8 @@ import {
24
24
ParserOptions ,
25
25
} from './types'
26
26
27
- export const mdxProcessor = unified ( ) . use ( remarkParse ) . use ( remarkMdx ) . freeze ( )
27
+ export const mdProcessor = unified ( ) . use ( remarkParse ) . freeze ( )
28
+ export const mdxProcessor = mdProcessor ( ) . use ( remarkMdx ) . freeze ( )
28
29
29
30
export const AST_PROPS = [ 'body' , 'comments' , 'tokens' ] as const
30
31
export const ES_NODE_TYPES : readonly string [ ] = [ 'export' , 'import' , 'jsx' ]
@@ -161,7 +162,7 @@ export class Parser {
161
162
return this . _eslintParse ( code , options )
162
163
}
163
164
164
- const root = mdxProcessor . parse ( code ) as Parent
165
+ const root = ( isMdx ? mdxProcessor : mdProcessor ) . parse ( code ) as Parent
165
166
166
167
this . _ast = {
167
168
...normalizePosition ( root . position ) ,
Original file line number Diff line number Diff line change @@ -41,7 +41,7 @@ export const requirePkg = <T>(
41
41
let searchSync : ( searchFrom ?: string ) => CosmiconfigResult
42
42
let remarkProcessor : Processor
43
43
44
- export const getRemarkProcessor = ( searchFrom : string ) => {
44
+ export const getRemarkProcessor = ( searchFrom : string , isMdx : boolean ) => {
45
45
if ( ! searchSync ) {
46
46
searchSync = cosmiconfigSync ( 'remark' , {
47
47
packageProp : 'remarkConfig' ,
@@ -66,6 +66,12 @@ export const getRemarkProcessor = (searchFrom: string) => {
66
66
// just ignore if the package does not exist
67
67
}
68
68
69
+ const initProcessor = remarkProcessor ( ) . use ( { settings } ) . use ( remarkStringify )
70
+
71
+ if ( isMdx ) {
72
+ initProcessor . use ( remarkMdx )
73
+ }
74
+
69
75
return plugins
70
76
. reduce ( ( processor , pluginWithSettings ) => {
71
77
const [ plugin , ...pluginSettings ] = Array . isArray ( pluginWithSettings )
@@ -78,6 +84,6 @@ export const getRemarkProcessor = (searchFrom: string) => {
78
84
: plugin ,
79
85
...pluginSettings ,
80
86
)
81
- } , remarkProcessor ( ) . use ( { settings } ) . use ( remarkStringify ) . use ( remarkMdx ) )
87
+ } , initProcessor )
82
88
. freeze ( )
83
89
}
Original file line number Diff line number Diff line change @@ -23,21 +23,21 @@ export const remark: Rule.RuleModule = {
23
23
const filename = context . getFilename ( )
24
24
const extname = path . extname ( filename )
25
25
const sourceCode = context . getSourceCode ( )
26
- const extensions = new Set (
27
- DEFAULT_EXTENSIONS . concat (
28
- context . parserOptions . extensions || [ ] ,
29
- MARKDOWN_EXTENSIONS ,
30
- context . parserOptions . markdownExtensions || [ ] ,
31
- ) ,
26
+ const options = context . parserOptions
27
+ const isMdx = DEFAULT_EXTENSIONS . concat ( options . extensions || [ ] ) . includes (
28
+ extname ,
32
29
)
30
+ const isMarkdown = MARKDOWN_EXTENSIONS . concat (
31
+ options . markdownExtensions || [ ] ,
32
+ ) . includes ( extname )
33
33
return {
34
34
Program ( node ) {
35
35
/* istanbul ignore if */
36
- if ( ! extensions . has ( extname ) ) {
36
+ if ( ! isMdx && ! isMarkdown ) {
37
37
return
38
38
}
39
39
const sourceText = sourceCode . getText ( node )
40
- const remarkProcessor = getRemarkProcessor ( filename )
40
+ const remarkProcessor = getRemarkProcessor ( filename , isMdx )
41
41
const file = vfile ( {
42
42
path : filename ,
43
43
contents : sourceText ,
You can’t perform that action at this time.
0 commit comments