ESLint Parser/Plugin for MDX, helps you lint all ES syntaxes. Linting
codeblocks can be enabled withmdx/code-blockssetting too! Work perfectly witheslint-plugin-import,eslint-plugin-prettieror any other eslint plugins. And also can be integrated with remark-lint plugins to lint markdown syntaxes.
TOC
- VSCode Extension
- Packages
- Install
- Notice
- Usage
- Parser Options
- Parser API
- Rules
- Prettier Integration
- Sponsors and Backers
- Changelog
- License
VSCode Extension
VSCode MDX: Integrates with VSCode ESLint, syntaxes highlighting and error reporting.
Packages
This repository is a monorepo managed by changesets what means we actually publish several packages to npm from same codebase, including:
| Package | Description | Version |
|---|---|---|
eslint-mdx |
ESLint Parser for MDX | |
eslint-plugin-mdx |
ESLint Plugin, Configuration and Rules for MDX |
Install
# yarn
yarn add -D eslint-plugin-mdx
# npm
npm i -D eslint-plugin-mdx
Notice
If you're using multi languages,
js/jsx/ts/tsx/vue, etc for example, you'd better to always useoverrides(Classic Config) orfiles(Flag Config) feature of ESLint, because configs may be overridden by following configs.See #251 for more details.
If you're using
{/* eslint-disable-line mdx/remark */}withprettier, this won't work becauseprettierwill add a blank line after the comment, which makes it invalid. You can use{/* eslint-disable mdx/remark */}paired with{/* eslint-enable mdx/remark */}instead:{/* eslint-disable mdx/remark */} # Heading {/* eslint-enable mdx/remark */}
Usage
Classic Config
.eslintrc file:
{
"extends": ["plugin:mdx/recommended"],
// optional, if you want to lint code blocks at the same time
"settings": {
"mdx/code-blocks": true,
// optional, if you want to disable language mapper, set it to `false`
// if you want to override the default language mapper inside, you can provide your own
"mdx/language-mapper": {},
// optional, same as the `parserOptions.ignoreRemarkConfig`, you have to specify it twice unfortunately
"mdx/ignore-remark-config": true,
// optional, same as the `parserOptions.remarkConfigPath`, you have to specify it twice unfortunately
"mdx/remark-config-path": "path/to/your/remarkrc",
},
}
Flat Config
eslint.config.js file:
import * as mdx from 'eslint-plugin-mdx'
export default [
{
...mdx.flat,
// optional, if you want to lint code blocks at the same
processor: mdx.createRemarkProcessor({
lintCodeBlocks: true,
// optional, if you want to disable language mapper, set it to `false`
// if you want to override the default language mapper inside, you can provide your own
languageMapper: {},
// optional, same as the `parserOptions.ignoreRemarkConfig`, you have to specify it twice unfortunately
ignoreRemarkConfig: true,
// optional, same as the `parserOptions.remarkConfigPath`, you have to specify it twice unfortunately
remarkConfigPath: 'path/to/your/remarkrc',
}),
},
{
...mdx.flatCodeBlocks,
rules: {
...mdx.flatCodeBlocks.rules,
// if you want to override some rules for code blocks
'no-var': 'error',
'prefer-const': 'error',
},
},
]
Then, make sure ESLint knows to run on .md or .mdx files:
eslint . --ext js,md,mdx
Parser Options
extensions(string | string[]):eslint-mdxwill only resolve.mdxfiles by default, if you want to resolve other extensions as like.mdx, you can use this option.markdownExtensions(string | string[]):eslint-mdxwill only treat.mdfiles 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.ignoreRemarkConfig(boolean): Ignore theremarkconfiguration defined in the project.remarkConfigPath(string): Specify the path to theremarkconfiguration file, could be relative toCWDor absolute path.
Parser API
eslint-mdx returns an ESTree Program. In addition to standard ESTree and JSX nodes, it emits custom nodes for Markdown content and MDX expressions.
Markdown content nodes
Markdown content uses the MD prefix, while MDX-specific nodes use MDX. The prefix avoids collisions with ESTree/JSX nodes. All nodes are exposed in Program.body or as children of other nodes and are traversed through the parser visitor keys.
| ESTree node | mdast node | Notes |
|---|---|---|
MDBlockquote |
blockquote |
|
MDBreak |
break |
|
MDCode |
code |
|
MDDefinition |
definition |
|
MDDelete |
delete |
remark-gfm |
MDEmphasis |
emphasis |
|
MDFootnoteDefinition |
footnoteDefinition |
remark-gfm |
MDFootnoteReference |
footnoteReference |
remark-gfm |
MDHeading |
heading |
|
MDImage |
image |
|
MDImageReference |
imageReference |
|
MDInlineCode |
inlineCode |
|
MDInlineMath |
inlineMath |
remark-math |
MDLink |
link |
|
MDLinkReference |
linkReference |
|
MDList |
list |
|
MDListItem |
listItem |
|
MDMath |
math |
remark-math |
MDParagraph |
paragraph |
|
MDStrong |
strong |
|
MDTable |
table |
remark-gfm |
MDTableCell |
tableCell |
remark-gfm |
MDTableRow |
tableRow |
remark-gfm |
MDText |
text |
|
MDThematicBreak |
thematicBreak |
|
MDToml |
toml |
remark-frontmatter |
MDYaml |
yaml |
remark-frontmatter |
For example:
# Heading
Paragraph with **strong**, _emphasis_, `inlineCode`, [link](https://example.com), and .
> Blockquote
- list item
```js
const value = 1
```
MDXCodeandMDXHeadingwere renamed toMDCodeandMDHeading; the old names are no longer emitted.
MDX expression nodes
MDX expressions use the MDX prefix and expose their ESTree expression:
| ESTree node | mdast node |
|---|---|
MDXFlowExpression |
mdxFlowExpression |
MDXTextExpression |
mdxTextExpression |
When these expressions are used as JSX children, they are wrapped in JSXExpressionContainer, which also carries the original expression text as value. Empty or comment-only expressions use JSXEmptyExpression.
For example:
{value}
> {value}
The value is {value}.
Typings
import type { BaseNode, Expression } from 'estree'
import type {
JSXElement,
JSXEmptyExpression,
JSXExpressionContainer,
JSXFragment,
JSXSpreadChild,
JSXText,
} from 'estree-jsx'
export interface MDBaseNode extends BaseNode {
raw: string
}
export interface MDCode extends MDBaseNode {
type: 'MDCode'
value: string
lang?: string | null
meta?: string | null
}
export type HeadingDepth = 1 | 2 | 3 | 4 | 5 | 6
export interface MDHeading extends MDBaseNode {
type: 'MDHeading'
depth: HeadingDepth
children: MDContentNode[]
}
export interface MDXFlowExpression extends BaseNode {
type: 'MDXFlowExpression'
expression: Expression | JSXEmptyExpression
value?: string
}
export interface MDXTextExpression extends BaseNode {
type: 'MDXTextExpression'
expression: Expression | JSXEmptyExpression
value?: string
}
export interface MDText extends MDBaseNode {
type: 'MDText'
value: string
}
export interface MDParagraph extends MDBaseNode {
type: 'MDParagraph'
children: MDContentNode[]
}
export interface MDBlockquote extends MDBaseNode {
type: 'MDBlockquote'
children: MDContentNode[]
}
export interface MDList extends MDBaseNode {
type: 'MDList'
ordered?: boolean | null
start?: number | null
spread?: boolean | null
children: MDListItem[]
}
export interface MDListItem extends MDBaseNode {
type: 'MDListItem'
checked?: boolean | null
spread?: boolean | null
children: MDContentNode[]
}
export interface MDEmphasis extends MDBaseNode {
type: 'MDEmphasis'
children: MDContentNode[]
}
export interface MDStrong extends MDBaseNode {
type: 'MDStrong'
children: MDContentNode[]
}
export interface MDInlineCode extends MDBaseNode {
type: 'MDInlineCode'
value: string
}
export interface MDLink extends MDBaseNode {
type: 'MDLink'
url: string
title?: string | null
children: MDContentNode[]
}
export interface MDImage extends MDBaseNode {
type: 'MDImage'
url: string
title?: string | null
alt?: string | null
}
export interface MDBreak extends MDBaseNode {
type: 'MDBreak'
}
export interface MDThematicBreak extends MDBaseNode {
type: 'MDThematicBreak'
}
export interface MDDefinition extends MDBaseNode {
type: 'MDDefinition'
identifier: string
label?: string | null
url: string
title?: string | null
}
export interface MDDelete extends MDBaseNode {
type: 'MDDelete'
children: MDContentNode[]
}
export interface MDFootnoteDefinition extends MDBaseNode {
type: 'MDFootnoteDefinition'
identifier: string
label?: string | null
children: MDContentNode[]
}
export interface MDFootnoteReference extends MDBaseNode {
type: 'MDFootnoteReference'
identifier: string
label?: string | null
}
export interface MDImageReference extends MDBaseNode {
type: 'MDImageReference'
identifier: string
label?: string | null
referenceType: 'collapsed' | 'full' | 'shortcut'
alt?: string | null
}
export interface MDInlineMath extends MDBaseNode {
type: 'MDInlineMath'
value: string
}
export interface MDLinkReference extends MDBaseNode {
type: 'MDLinkReference'
identifier: string
label?: string | null
referenceType: 'collapsed' | 'full' | 'shortcut'
children: MDContentNode[]
}
export interface MDMath extends MDBaseNode {
type: 'MDMath'
value: string
meta?: string | null
}
export interface MDTable extends MDBaseNode {
type: 'MDTable'
align?: Array<'center' | 'left' | 'right' | null> | null
children: MDTableRow[]
}
export interface MDTableCell extends MDBaseNode {
type: 'MDTableCell'
children: MDContentNode[]
}
export interface MDTableRow extends MDBaseNode {
type: 'MDTableRow'
children: MDTableCell[]
}
export interface MDToml extends MDBaseNode {
type: 'MDToml'
value: string
}
export interface MDYaml extends MDBaseNode {
type: 'MDYaml'
value: string
}
export type MDContentNode =
| JSXElement
| JSXExpressionContainer
| JSXFragment
| JSXSpreadChild
| JSXText
| MDBlockquote
| MDBreak
| MDCode
| MDDefinition
| MDDelete
| MDEmphasis
| MDFootnoteDefinition
| MDFootnoteReference
| MDHeading
| MDImage
| MDImageReference
| MDInlineCode
| MDInlineMath
| MDLink
| MDLinkReference
| MDList
| MDListItem
| MDMath
| MDParagraph
| MDStrong
| MDTable
| MDTableCell
| MDTableRow
| MDText
| MDThematicBreak
| MDToml
| MDXFlowExpression
| MDXTextExpression
| MDYaml
Rules
Native remark-lint rules
Recommendation: enable the native remark-lint rules with
nativeconfig. They run inside ESLint, avoid the remark fallback path, and provide native fixes where available. Keepmdx/remarkfor remark plugins that have no native equivalent.
const classic = {
extends: ['plugin:mdx/base', 'plugin:mdx/native'],
}
const flat = [mdx.configs.flat, mdx.configs.native]
See Native remark-lint rules for the mapped rules and option behavior.
Native rules read their options from the remark config by default. An ESLint positional option overrides it. Optionless native rules need a remark config entry by default; pass { enabled: true } to force-activate one from ESLint without it.
mdx/remark
Deprecated: stringify-based autofixes from
mdx/remarkwill be removed in a future major release. Enablenativefor targeted fixes; reporting remains supported. The warning is emitted once when a fallback fix is computed. SetdeprecationWarnings: falsein themdx/remarkoptions to silence it.
possible fixable depends on your remark plugins:
Integration with remark-lint plugins, it will read remark's configuration automatically via unified-engine. But .remarkignore will not be respected, you should use .eslintignore instead.
If you want to disable or change severity of some related rules, it won't work by setting rules in eslint config like 'remark-lint-no-duplicate-headings': 0, you should change your remark config instead like following:
{
"plugins": [
"@1stg/remark-config",
// change to error severity, notice `[]` is required
["lint-no-duplicate-headings", [2]],
// disable following plugin
[
"lint-no-multiple-toplevel-headings",
[0], // or false
],
],
}
Prettier Integration
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.
{
"plugins": [
"preset-lint-consistent",
"preset-lint-recommended",
"preset-lint-markdown-style-guide",
"preset-prettier"
]
}
Sponsors and Backers
Sponsors
| 1stG | RxTS | UnRS | UnTS |
|---|---|---|---|
Backers
| 1stG | RxTS | UnRS | UnTS |
|---|---|---|---|
Changelog
Detailed changes for each release are documented in CHANGELOG.md.