关闭ESLint的markdown相关规则无效,lint时仍报错求助
问题
我在运行ESLint时尝试关闭两个规则:因为项目是模式库,大量README文档存在重复标题与长标题,所以需要关闭no-duplicate-headings和maximum-heading-length规则。已经在.eslintrc.json中把这两个规则设为"off",但lint对应文件时还是收到相同报错,配置和报错信息如下:
配置文件
{ "env": { "browser": true, "es2021": true }, "extends": ["eslint:recommended", "plugin:md/prettier", "plugin:mdx/recommended", "plugin:@typescript-eslint/recommended"], "parserOptions": { "ecmaFeatures": { "jsx": true }, "ecmaVersion": "latest", "sourceType": "module" }, "plugins": ["prettier", "@typescript-eslint/eslint-plugin"], "rules": { "no-duplicate-headings": "off", "maximum-heading-length": "off" }, "settings": { "mdx/code-blocks": true }, "overrides": [ { "files": ["*.mdx"], "extends": "plugin:mdx/recommended" }, { "files": ["*.md"], "extends": "plugin:md/prettier" } ] }
报错信息
191:1 error Do not use headings with similar content (139:1) (no-duplicate-headings) md/remark 197:1 error Use headings shorter than `60` (maximum-heading-length) md/remark 199:1 error Do not use headings with similar content (183:1) (no-duplicate-headings) md/remark 221:1 error Use headings shorter than `60` (maximum-heading-length) md/remark 223:1 error Do not use headings with similar content (199:1) (no-duplicate-headings) md/remark 232:1 error Do not use headings with similar content (206:1) (no-duplicate-headin
解决方案
从报错信息里的md/remark后缀能看出来,这两个规则是eslint-plugin-md插件提供的,不是ESLint核心规则。你之前直接写规则名是无效的,必须加上插件前缀md/。另外,你对.md文件的overrides块里继承了plugin:md/prettier,这个继承配置会覆盖根规则的设置,所以得把规则关闭放到这个overrides块里,确保优先级更高。
修改后的配置如下:
{ "env": { "browser": true, "es2021": true }, "extends": ["eslint:recommended", "plugin:md/prettier", "plugin:mdx/recommended", "plugin:@typescript-eslint/recommended"], "parserOptions": { "ecmaFeatures": { "jsx": true }, "ecmaVersion": "latest", "sourceType": "module" }, "plugins": ["prettier", "@typescript-eslint/eslint-plugin", "md"], "settings": { "mdx/code-blocks": true }, "overrides": [ { "files": ["*.mdx"], "extends": "plugin:mdx/recommended" }, { "files": ["*.md"], "extends": "plugin:md/prettier", "rules": { "md/no-duplicate-headings": "off", "md/maximum-heading-length": "off" } } ] }
关键改动点:
- 规则名添加
md/前缀,明确指定是eslint-plugin-md的插件规则 - 将规则关闭配置移到
.md文件对应的overrides块内,避免被继承的配置覆盖 - 插件列表里补充
md(虽然继承配置可能已经引入,但显式声明更稳妥)
内容的提问来源于stack exchange,提问作者rovac
相关产品推荐
相关产品推荐

