Skip to content

Commit 7c76bfe

Browse files
committed
feat: remark-mdx is not compatible with normal markdown syntaxes
mdx-js/mdx#1042
1 parent 8a6242a commit 7c76bfe

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

packages/eslint-mdx/src/parser.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ import {
2424
ParserOptions,
2525
} from './types'
2626

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()
2829

2930
export const AST_PROPS = ['body', 'comments', 'tokens'] as const
3031
export const ES_NODE_TYPES: readonly string[] = ['export', 'import', 'jsx']
@@ -161,7 +162,7 @@ export class Parser {
161162
return this._eslintParse(code, options)
162163
}
163164

164-
const root = mdxProcessor.parse(code) as Parent
165+
const root = (isMdx ? mdxProcessor : mdProcessor).parse(code) as Parent
165166

166167
this._ast = {
167168
...normalizePosition(root.position),

packages/eslint-plugin-mdx/src/rules/helper.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const requirePkg = <T>(
4141
let searchSync: (searchFrom?: string) => CosmiconfigResult
4242
let remarkProcessor: Processor
4343

44-
export const getRemarkProcessor = (searchFrom: string) => {
44+
export const getRemarkProcessor = (searchFrom: string, isMdx: boolean) => {
4545
if (!searchSync) {
4646
searchSync = cosmiconfigSync('remark', {
4747
packageProp: 'remarkConfig',
@@ -66,6 +66,12 @@ export const getRemarkProcessor = (searchFrom: string) => {
6666
// just ignore if the package does not exist
6767
}
6868

69+
const initProcessor = remarkProcessor().use({ settings }).use(remarkStringify)
70+
71+
if (isMdx) {
72+
initProcessor.use(remarkMdx)
73+
}
74+
6975
return plugins
7076
.reduce((processor, pluginWithSettings) => {
7177
const [plugin, ...pluginSettings] = Array.isArray(pluginWithSettings)
@@ -78,6 +84,6 @@ export const getRemarkProcessor = (searchFrom: string) => {
7884
: plugin,
7985
...pluginSettings,
8086
)
81-
}, remarkProcessor().use({ settings }).use(remarkStringify).use(remarkMdx))
87+
}, initProcessor)
8288
.freeze()
8389
}

packages/eslint-plugin-mdx/src/rules/remark.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,21 @@ export const remark: Rule.RuleModule = {
2323
const filename = context.getFilename()
2424
const extname = path.extname(filename)
2525
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,
3229
)
30+
const isMarkdown = MARKDOWN_EXTENSIONS.concat(
31+
options.markdownExtensions || [],
32+
).includes(extname)
3333
return {
3434
Program(node) {
3535
/* istanbul ignore if */
36-
if (!extensions.has(extname)) {
36+
if (!isMdx && !isMarkdown) {
3737
return
3838
}
3939
const sourceText = sourceCode.getText(node)
40-
const remarkProcessor = getRemarkProcessor(filename)
40+
const remarkProcessor = getRemarkProcessor(filename, isMdx)
4141
const file = vfile({
4242
path: filename,
4343
contents: sourceText,

0 commit comments

Comments
 (0)