Skip to content

Commit 1c9e3fd

Browse files
authored
feat: allow to ignore remark config (#374)
1 parent 51a3ee0 commit 1c9e3fd

File tree

9 files changed

+47
-7
lines changed

9 files changed

+47
-7
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ See [#251](https://github.com/mdx-js/eslint-mdx/issues/251#issuecomment-73613922
190190

191191
3. `markdownExtensions` (`string | string[]`): `eslint-mdx` will only treat `.md` files as plain markdown by default, and will lint them via remark plugins. If you want to resolve other extensions as like `.md`, you can use this option.
192192

193+
4. `ignoreRemarkConfig` (`boolean`): Ignore the `remark` configuration defined in the project.
194+
193195
## Rules
194196

195197
### mdx/no-jsx-html-comments
@@ -223,6 +225,8 @@ If you want to disable or change severity of some related rules, it won't work b
223225
}
224226
```
225227
228+
Some plugins are ESM and eslint don't supports them. So, a workaround is to set `ignoreRemarkConfig` to `true` and execute `remark-lint` through the terminal before running eslint. For example: `remark **/*.mdx --no-stdout && eslint . --fix --ext .mdx`.
229+
226230
## Prettier Integration
227231

228232
If you're using [remark-lint][] feature with [Prettier][] both together, you can try [remark-preset-prettier][] which helps you to _turn off all rules that are unnecessary or might conflict with [Prettier][]_.

packages/eslint-mdx/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ See [#251](https://github.com/mdx-js/eslint-mdx/issues/251#issuecomment-73613922
190190

191191
3. `markdownExtensions` (`string | string[]`): `eslint-mdx` will only treat `.md` files as plain markdown by default, and will lint them via remark plugins. If you want to resolve other extensions as like `.md`, you can use this option.
192192

193+
4. `ignoreRemarkConfig` (`boolean`): Ignore the `remark` configuration defined in the project.
194+
193195
## Rules
194196

195197
### mdx/no-jsx-html-comments
@@ -223,6 +225,8 @@ If you want to disable or change severity of some related rules, it won't work b
223225
}
224226
```
225227
228+
Some plugins are ESM and eslint don't supports them. So, a workaround is to set `ignoreRemarkConfig` to `true` and execute `remark-lint` through the terminal before running eslint. For example: `remark **/*.mdx --no-stdout && eslint . --fix --ext .mdx`.
229+
226230
## Prettier Integration
227231

228232
If you're using [remark-lint][] feature with [Prettier][] both together, you can try [remark-preset-prettier][] which helps you to _turn off all rules that are unnecessary or might conflict with [Prettier][]_.

packages/eslint-mdx/src/parser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ export class Parser {
172172
const root = getRemarkProcessor(
173173
getPhysicalFilename(options.filePath),
174174
isMdx,
175+
Boolean(options.ignoreRemarkConfig),
175176
).parse(code) as Parent
176177

177178
this._ast = {

packages/eslint-mdx/src/processor.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import fs from 'fs'
22
import path from 'path'
33

44
import { cosmiconfigSync } from 'cosmiconfig'
5+
import type { CosmiconfigResult } from 'cosmiconfig/dist/types'
56
import remarkMdx from 'remark-mdx'
67
import remarkParse from 'remark-parse'
78
import remarkStringify from 'remark-stringify'
@@ -69,8 +70,12 @@ const explorer = cosmiconfigSync('remark', {
6970
// @internal - exported for testing
7071
export const processorCache = new Map<string, FrozenProcessor>()
7172

72-
// eslint-disable-next-line sonarjs/cognitive-complexity
73-
export const getRemarkProcessor = (searchFrom: string, isMdx: boolean) => {
73+
export const getRemarkProcessor = (
74+
searchFrom: string,
75+
isMdx: boolean,
76+
ignoreRemarkConfig: boolean,
77+
// eslint-disable-next-line sonarjs/cognitive-complexity
78+
) => {
7479
const initCacheKey = `${String(isMdx)}-${searchFrom}`
7580

7681
let cachedProcessor = processorCache.get(initCacheKey)
@@ -79,7 +84,9 @@ export const getRemarkProcessor = (searchFrom: string, isMdx: boolean) => {
7984
return cachedProcessor
8085
}
8186

82-
const result = explorer.search(searchFrom)
87+
const result: CosmiconfigResult = ignoreRemarkConfig
88+
? null
89+
: explorer.search(searchFrom)
8390

8491
const cacheKey = result ? `${String(isMdx)}-${result.filepath}` : ''
8592

packages/eslint-mdx/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export interface ParserOptions extends Linter.ParserOptions {
5050
markdownExtensions?: string[] | string
5151
filePath?: string
5252
parser?: ParserConfig | ParserFn | string
53+
ignoreRemarkConfig?: boolean
5354
}
5455

5556
export type Traverser = (node: Node, parent?: Parent) => void

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const lazyRemark = {
2525
fileOptions: VFileOptions,
2626
physicalFilename: string,
2727
isMdx: boolean,
28+
ignoreRemarkConfig: boolean,
2829
) => {
2930
messages: VFile['messages']
3031
content: string
@@ -65,10 +66,16 @@ export const remark: Rule.RuleModule = {
6566
return
6667
}
6768

69+
const ignoreRemarkConfig = Boolean(options.ignoreRemarkConfig)
70+
6871
const physicalFilename = getPhysicalFilename(filename)
6972

7073
const sourceText = sourceCode.getText(node)
71-
const remarkProcessor = getRemarkProcessor(physicalFilename, isMdx)
74+
const remarkProcessor = getRemarkProcessor(
75+
physicalFilename,
76+
isMdx,
77+
ignoreRemarkConfig,
78+
)
7279

7380
const fileOptions = {
7481
path: physicalFilename,
@@ -85,6 +92,7 @@ export const remark: Rule.RuleModule = {
8592
fileOptions,
8693
physicalFilename,
8794
isMdx,
95+
ignoreRemarkConfig,
8896
)
8997
file.messages = messages
9098
fixedText = content
@@ -102,6 +110,7 @@ export const remark: Rule.RuleModule = {
102110
fileOptions,
103111
physicalFilename,
104112
isMdx,
113+
ignoreRemarkConfig,
105114
)
106115
file.messages = messages
107116
fixedText = content

packages/eslint-plugin-mdx/src/worker.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,13 @@ runAsWorker(
99
fileOptions: VFileOptions,
1010
physicalFilename: string,
1111
isMdx: boolean,
12+
ignoreRemarkConfig: boolean,
1213
) => {
13-
const remarkProcessor = getRemarkProcessor(physicalFilename, isMdx)
14+
const remarkProcessor = getRemarkProcessor(
15+
physicalFilename,
16+
isMdx,
17+
ignoreRemarkConfig,
18+
)
1419
const file = vfile(fileOptions)
1520
try {
1621
await remarkProcessor.process(file)

test/parser.test.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ import {
1313

1414
const stringToNode = (text: string) =>
1515
first(
16-
(getRemarkProcessor(PLACEHOLDER_FILE_PATH, true).parse(text) as Parent)
17-
.children,
16+
(
17+
getRemarkProcessor(PLACEHOLDER_FILE_PATH, true, false).parse(
18+
text,
19+
) as Parent
20+
).children,
1821
)
1922

2023
describe('parser', () => {

test/remark.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ ruleTester.run('remark', remark, {
5555
parserOptions,
5656
filename: path.resolve(__dirname, 'fixtures/async/test.mdx'),
5757
},
58+
{
59+
code: '_emphasis_ and __strong__',
60+
parser,
61+
parserOptions: { ...parserOptions, ignoreRemarkConfig: true },
62+
filename: path.resolve(__dirname, 'fixtures/style/test.mdx'),
63+
},
5864
],
5965
invalid: [
6066
{

0 commit comments

Comments
 (0)