Skip to content

Commit 96fffcf

Browse files
committed
feat: remark-mdx is not compatible with normal markdown syntaxes
mdx-js/mdx#1042
1 parent 80686eb commit 96fffcf

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
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']
@@ -157,7 +158,7 @@ export class Parser {
157158
return this._eslintParse(code, options)
158159
}
159160

160-
const root = mdxProcessor.parse(code) as Parent
161+
const root = (isMdx ? mdxProcessor : mdProcessor).parse(code) as Parent
161162

162163
this._ast = {
163164
...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 = (
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-6
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,21 @@ export const remark: Rule.RuleModule = {
2424
const filename = context.getFilename()
2525
const extname = path.extname(filename)
2626
const sourceCode = context.getSourceCode()
27-
const extensions = DEFAULT_EXTENSIONS.concat(
28-
context.parserOptions.extensions || [],
29-
MARKDOWN_EXTENSIONS,
30-
context.parserOptions.markdownExtensions || [],
27+
const options = context.parserOptions
28+
const isMdx = DEFAULT_EXTENSIONS.concat(options.extensions || []).includes(
29+
extname,
3130
)
31+
const isMarkdown = MARKDOWN_EXTENSIONS.concat(
32+
options.markdownExtensions || [],
33+
).includes(extname)
3234
return {
3335
Program(node) {
3436
/* istanbul ignore if */
35-
if (!extensions.includes(extname)) {
37+
if (!isMdx && !isMarkdown) {
3638
return
3739
}
3840
const sourceText = sourceCode.getText(node)
39-
const remarkProcessor = getRemarkProcessor(filename)
41+
const remarkProcessor = getRemarkProcessor(filename, isMdx)
4042
const file = vfile({
4143
path: filename,
4244
contents: sourceText,

0 commit comments

Comments
 (0)