What is eslint-mdx?
The eslint-mdx package is an ESLint plugin that provides linting support for MDX (Markdown with JSX) files. It helps ensure code quality and consistency in MDX files by leveraging ESLint's capabilities.
What are eslint-mdx's main functionalities?
Linting MDX Files
This configuration allows ESLint to lint MDX files by extending the recommended settings provided by the eslint-mdx plugin.
module.exports = {
"overrides": [
{
"files": ["*.mdx"],
"extends": ["plugin:mdx/recommended"]
}
]
};
Custom MDX Rules
This configuration demonstrates how to add custom rules for MDX files. In this example, the rule 'mdx/no-jsx-html-comments' is set to 'error' to disallow HTML comments within JSX.
module.exports = {
"overrides": [
{
"files": ["*.mdx"],
"extends": ["plugin:mdx/recommended"],
"rules": {
"mdx/no-jsx-html-comments": "error"
}
}
]
};
Integrating with Prettier
This configuration shows how to integrate Prettier with eslint-mdx to ensure consistent formatting in MDX files. The 'prettier/prettier' rule is configured to use the 'mdx' parser.
module.exports = {
"overrides": [
{
"files": ["*.mdx"],
"extends": ["plugin:mdx/recommended", "prettier"],
"rules": {
"prettier/prettier": ["error", { "parser": "mdx" }]
}
}
]
};
Other packages similar to eslint-mdx
eslint-plugin-markdown
eslint-plugin-markdown is an ESLint plugin that allows linting of JavaScript code blocks within Markdown files. Unlike eslint-mdx, which is specifically designed for MDX files, eslint-plugin-markdown focuses on JavaScript code within standard Markdown files.
remark-lint
remark-lint is a plugin for remark, a Markdown processor, that provides linting capabilities for Markdown files. While eslint-mdx leverages ESLint for linting MDX files, remark-lint is part of the remark ecosystem and focuses on Markdown linting with its own set of rules and plugins.
eslint-plugin-react
eslint-plugin-react is an ESLint plugin that provides linting rules for React applications. Although it is not specifically designed for MDX files, it can be used in conjunction with eslint-mdx to ensure React code within MDX files adheres to best practices.
ESLint Parser/Plugin for MDX, helps you lint all ES syntaxes excluding code
block of course.
Work perfectly with eslint-plugin-import
, eslint-plugin-prettier
or any other eslint plugins.
VSCode Extension
VSCode MDX: Integrates with VSCode ESLint, syntaxes highlighting and error reporting.
Packages
This repository is a monorepo managed by Lerna what means we actually publish several packages to npm from same codebase, including:
Install
yarn add -D @rxts/eslint-plugin-mdx
npm i -D @rxts/eslint-plugin-mdx
Usage
-
In your ESLint config file:
-
If you're using eslint >= 6.0.0
, add:
{
"extends": ["plugin:@rxts/mdx/recommended"],
"overrides": [
{
"files": ["*.mdx"],
"extends": ["plugin:@rxts/mdx/overrides"]
}
]
}
-
If you're using eslint@^5.0.0
, you need to enable this parser/plugin manually, because eslint@5
does not support extends
for overrides
property in its configuration:
{
"extends": ["plugin:@rxts/mdx/recommended"],
"overrides": [
{
"files": ["*.mdx"],
"globals": {
"React": false
},
"rules": {
"lines-between-class-members": 0,
"react/react-in-jsx-scope": 0
}
}
]
}
-
Make sure ESLint knows to run on .mdx
files:
eslint . --ext js,mdx
Parser Options
-
parser
(string | ParserConfig | ParserFn
): Custom parser for ES syntax is supported, although @typescript-eslint/parser
or babel-eslint
will be detected automatically what means you actually do not need to do this:
{
"extends": ["plugin:@rxts/mdx/recommended"],
"parserOptions": {
"parser": "babel-eslint"
}
}
-
extensions
(string | string[]
): eslint-mdx
will only resolve .mdx
files by default, files with other extensions will be resolved by the parser
option. If you want to resolve other extensions as like .mdx
, you can use this option.
Rules
HTML style comments in jsx block is invalid, this rule will help you to fix it by transforming it to JSX style comments.
@rxts/mdx/no-unescaped-entities
Inline JSX like Inline <Component />
is supported by MDX, but rule react/no-unescaped-entities
from eslint-plugin-react is incompatible with it, @rxts/mdx/no-unescaped-entities
is the replacement.
@rxts/mdx/no-unused-expressions
MDX can render jsx
block automatically without exporting them, but ESLint will report no-unused-expressions
issue which could be unexpected, this rule is a replacement of it, so make sure that you've turned off the original no-unused-expressions
rule.
Limitation
This parser/plugin can only handle ES syntaxes for you, markdown related syntaxes will just be ignored, you can use markdownlint or remark-lint to lint that part.
I have a very preliminary idea to integrate with remark-lint.
Changelog
Detailed changes for each release are documented in CHANGELOG.md.
License
MIT © JounQin@1stG