eslint-plugin-markdown
Lint JS, JSX, TypeScript, and more inside Markdown.
Usage
Installing
Install the plugin alongside ESLint v6 or greater:
npm install --save-dev eslint eslint-plugin-markdown
Configuring
Extending the plugin:markdown/recommended
config will enable the Markdown processor on all .md
files:
module.exports = {
extends: "plugin:markdown/recommended"
};
Advanced Configuration
Add the plugin to your .eslintrc
and use the processor
option in an overrides
entry to enable the plugin's markdown/markdown
processor on Markdown files.
Each fenced code block inside a Markdown document has a virtual filename appended to the Markdown file's path.
The virtual filename's extension will match the fenced code block's syntax tag, so for example, ```js
code blocks in README.md
would match README.md/*.js
.
overrides
glob patterns for these virtual filenames can customize configuration for code blocks without affecting regular code.
For more information on configuring processors, refer to the ESLint documentation.
module.exports = {
plugins: ["markdown"],
overrides: [
{
files: ["**/*.md"],
processor: "markdown/markdown"
},
{
files: ["**/*.md/*.js"],
rules: {
}
}
]
};
Frequently-Disabled Rules
Some rules that catch mistakes in regular code are less helpful in documentation.
For example, no-undef
would flag variables that are declared outside of a code snippet because they aren't relevant to the example.
The plugin:markdown/recommended
config disables these rules in Markdown files:
Use overrides
glob patterns to disable more rules just for Markdown code blocks:
module.exports = {
overrides: [
{
files: ["**/*.md/*.js"],
rules: {
"no-console": "off",
"import/no-unresolved": "off"
}
}
]
};
Strict Mode
"use strict"
directives in every code block would be annoying.
The plugin:markdown/recommended
config enables the impliedStrict
parser option and disables the strict
rule in Markdown files.
This opts into strict mode parsing without repeated "use strict"
directives.
Unsatisfiable Rules
Markdown code blocks are not real files, so ESLint's file-format rules do not apply.
The plugin:markdown/recommended
config disables these rules in Markdown files:
eol-last
: The Markdown parser trims trailing newlines from code blocks.unicode-bom
: Markdown code blocks do not have Unicode Byte Order Marks.
Migrating from eslint-plugin-markdown
v1
eslint-plugin-markdown
v1 used an older version of ESLint's processor API.
The Markdown processor automatically ran on .md
, .mkdn
, .mdown
, and .markdown
files, and it only extracted fenced code blocks marked with js
, javascript
, jsx
, or node
syntax.
Configuration specifically for fenced code blocks went inside an overrides
entry with a files
pattern matching the containing Markdown document's filename that applied to all fenced code blocks inside the file.
module.exports = {
plugins: ["markdown"],
overrides: [
{
files: ["**/*.md"],
parserOptions: {
ecmaFeatures: {
impliedStrict: true
}
},
rules: {
"no-console": "off"
}
}
]
};
RFC3 designed a new processor API to remove these limitations, and the new API was implemented as part of ESLint v6.
eslint-plugin-markdown
v2 uses this new API.
$ npm install --save-dev eslint@latest eslint-plugin-markdown@latest
All of the Markdown file extensions that were previously hard-coded are now fully configurable in .eslintrc.js
.
Use the new processor
option to apply the markdown/markdown
processor on any Markdown documents matching a files
pattern.
Each fenced code block inside a Markdown document has a virtual filename appended to the Markdown file's path.
The virtual filename's extension will match the fenced code block's syntax tag, so for example, ```js
code blocks in README.md
would match README.md/*.js
.
module.exports = {
plugins: ["markdown"],
overrides: [
{
files: ["**/*.md"],
processor: "markdown/markdown"
},
{
files: ["**/*.md/*.js"],
parserOptions: {
ecmaFeatures: {
impliedStrict: true
}
},
rules: {
"no-console": "off"
}
}
]
};
If you need to precisely mimic the behavior of v1 with the hard-coded Markdown extensions and fenced code block syntaxes, you can use those as glob patterns in overrides[].files
:
module.exports = {
plugins: ["markdown"],
overrides: [
{
files: ["**/*.{md,mkdn,mdown,markdown}"],
processor: "markdown/markdown"
},
{
files: ["**/*.{md,mkdn,mdown,markdown}/*.{js,javascript,jsx,node}"]
}
]
};
Running
ESLint v7
You can run ESLint as usual and do not need to use the --ext
option.
ESLint v7 automatically lints file extensions specified in overrides[].files
patterns in config files.
ESLint v6
Use the --ext
option to include .js
and .md
extensions in ESLint's file search:
eslint --ext js,md .
Autofixing
With this plugin, ESLint's --fix
option can automatically fix some issues in your Markdown fenced code blocks.
To enable this, pass the --fix
flag when you run ESLint:
eslint --fix .
What Gets Linted?
With this plugin, ESLint will lint fenced code blocks in your Markdown documents:
```js
// This gets linted
var answer = 6 * 7;
console.log(answer);
```
Here is some regular Markdown text that will be ignored.
```js
// This also gets linted
/* eslint quotes: [2, "double"] */
function hello() {
console.log("Hello, world!");
}
hello();
```
```jsx
// This can be linted too if you add `.jsx` files to `overrides` in ESLint v7
// or pass `--ext jsx` in ESLint v6.
var div = <div className="jsx"></div>;
```
Blocks that don't specify a syntax are ignored:
```
This is plain text and doesn't get linted.
```
Unless a fenced code block's syntax appears as a file extension in overrides[].files
in ESLint v7, it will be ignored.
If using ESLint v6, you must also include the extension with the --ext
option.
```python
print("This doesn't get linted either.")
```
The processor will convert HTML comments immediately preceding a code block into JavaScript block comments and insert them at the beginning of the source code that it passes to ESLint.
This permits configuring ESLint via configuration comments while keeping the configuration comments themselves hidden when the markdown is rendered.
Comment bodies are passed through unmodified, so the plugin supports any configuration comments supported by ESLint itself.
This example enables the browser
environment, disables the no-alert
rule, and configures the quotes
rule to prefer single quotes:
<!-- eslint-env browser -->
<!-- eslint-disable no-alert -->
<!-- eslint quotes: ["error", "single"] -->
```js
alert('Hello, world!');
```
Each code block in a file is linted separately, so configuration comments apply only to the code block that immediately follows.
Assuming `no-alert` is enabled in `.eslintrc`, the first code block will have no error from `no-alert`:
<!-- eslint-env browser -->
<!-- eslint-disable no-alert -->
```js
alert("Hello, world!");
```
But the next code block will have an error from `no-alert`:
<!-- eslint-env browser -->
```js
alert("Hello, world!");
```
Skipping Blocks
Sometimes it can be useful to have code blocks marked with js
even though they don't contain valid JavaScript syntax, such as commented JSON blobs that need js
syntax highlighting.
Standard eslint-disable
comments only silence rule reporting, but ESLint still reports any syntax errors it finds.
In cases where a code block should not even be parsed, insert a non-standard <!-- eslint-skip -->
comment before the block, and this plugin will hide the following block from ESLint.
Neither rule nor syntax errors will be reported.
There are comments in this JSON, so we use `js` syntax for better
highlighting. Skip the block to prevent warnings about invalid syntax.
<!-- eslint-skip -->
```js
{
// This code block is hidden from ESLint.
"hello": "world"
}
```
```js
console.log("This code block is linted normally.");
```
Editor Integrations
VSCode
vscode-eslint
has built-in support for the Markdown processor.
Atom
The linter-eslint
package allows for linting within the Atom IDE.
In order to see eslint-plugin-markdown
work its magic within Markdown code blocks in your Atom editor, you can go to linter-eslint
's settings and within "List of scopes to run ESLint on...", add the cursor scope "source.gfm".
However, this reports a problem when viewing Markdown which does not have configuration, so you may wish to use the cursor scope "source.embedded.js", but note that eslint-plugin-markdown
configuration comments and skip directives won't work in this context.
Contributing
$ git clone https://github.com/eslint/eslint-plugin-markdown.git
$ cd eslint-plugin-markdown
$ npm install
$ npm test
This project follows the ESLint contribution guidelines.
3.0.1 (2023-07-15)
Chores
v3.0.0 - July 16, 2022
558ae3c
chore: add node v18 (#205) (Amaresh S M)071fa66
feat!: drop node v8 and v10 (#203) (Amaresh S M)f186730
ci: update github actions (#207) (Deepshika S)6570c82
ci: Work around npm behavior changes to fix CI on main (#206) (Brandon Mills)87c2b53
docs: update badges (#202) (Milos Djermanovic)2fd5b89
chore: add tests for ESLint 8 (#195) (Michaël De Boey)8db0978
chore: Check for package.json in examples (#200) (Brandon Mills)b695396
test: test with ESLint
instead of CLIEngine
when available (#198) (Michaël De Boey)e1ddcc5
ci: use node v16
(#199) (Nitin Kumar)8f590fc
chore: update devDependencies
(#197) (Michaël De Boey)3667566
chore: test all supported ESLint versions (#196) (Michaël De Boey)ecae4fe
Chore: ignore pnpm-lock.yaml (#193) (Nitin Kumar)ffdb245
Chore: use actions/setup-node@v2
in CI (#192) (Nitin Kumar)
v2.2.1 - September 11, 2021
3a40160
Fix: message.line
could be undefined
(#191) (JounQin)
v2.2.0 - May 26, 2021
32203f6
Update: Replace Markdown parser (fixes #125, fixes #186) (#188) (Brandon Mills)
v2.1.0 - April 25, 2021
f1e153b
Update: Upgrade remark-parse to v7 (fixes #77, fixes #78) (#175) (Brandon Mills)
v2.0.1 - April 5, 2021
d23d5f7
Fix: use blocksCache instead of single blocks instance (fixes #181) (#183) (JounQin)a09a645
Chore: add yarn.lock and package-lock.json into .gitignore (#184) (JounQin)1280ac1
Docs: improve jsdoc, better for typings (#182) (JounQin)79be776
Fix: More reliable comment attachment (fixes #76) (#177) (Brandon Mills)
v2.0.0 - February 14, 2021
53dc0e5
Docs: Remove prerelease README notes (#173) (Brandon Mills)140adf4
2.0.0-rc.2 (ESLint Jenkins)15d7aa6
Build: changelog update for 2.0.0-rc.2 (ESLint Jenkins)f6a3fad
Fix: overrides pattern for virtual filenames in recommended config (#169) (Milos Djermanovic)390d508
2.0.0-rc.1 (ESLint Jenkins)e05d6eb
Build: changelog update for 2.0.0-rc.1 (ESLint Jenkins)1dd7089
Fix: npm prepare script on Windows (refs #166) (#168) (Brandon Mills)23ac2b9
Fix: Ignore words in info string after syntax (fixes #166) (#167) (Brandon Mills)8f729d3
Chore: Switch to main for primary branch (fixes #161) (#165) (Brandon Mills)d30c50f
Chore: Automatically install example dependencies (#164) (Brandon Mills)2749b4d
2.0.0-rc.0 (ESLint Jenkins)922a00e
Build: changelog update for 2.0.0-rc.0 (ESLint Jenkins)d94c22f
Build: Install example test dependencies in Jenkins (#160) (Brandon Mills)7f26cb9
Docs: Reference recommended config disabled rules (#159) (Brandon Mills)bf7648f
Docs: Add TypeScript example (#155) (Brandon Mills)d80be9e
New: Add rules to recommended config (#157) (Nikolay Stoynov)fc4d7aa
Chore: run CI in Node 14.x (#158) (Kai Cataldo)f2d4923
Docs: Add React example (#152) (Brandon Mills)eb66833
New: Add recommended config (fixes #151) (#153) (Brandon Mills)0311640
Fix: Don't require message end locations (fixes #112) (#154) (Brandon Mills)2bc9352
2.0.0-alpha.0 (ESLint Jenkins)c0ba401
Build: changelog update for 2.0.0-alpha.0 (ESLint Jenkins)51e48c6
Docs: Revamp documentation for v2 (#149) (Brandon Mills)b221391
Docs: Dogfood plugin by linting readme (#145) (Brandon Mills)7423610
Docs: Explain use of --ext option in ESLint v7 (#146) (Brandon Mills)0d4dbe8
Breaking: Implement new processor API (fixes #138) (#144) (Brandon Mills)7eeafb8
Chore: Update ESLint config and plugins (#143) (Brandon Mills)f483343
Breaking: Require ESLint v6 (#142) (Brandon Mills)9aa1fdc
Chore: Use ES2018 object spread syntax (#141) (Brandon Mills)f584cc6
Build: Remove Travis (#140) (Brandon Mills)35f9a11
Breaking: Drop support for Node.js v6 (refs #138) (#137) (Brandon Mills)6f02ef5
Chore: Add npm version and build status badges (#139) (Brandon Mills)
v2.0.0-rc.2 - January 30, 2021
f6a3fad
Fix: overrides pattern for virtual filenames in recommended config (#169) (Milos Djermanovic)
v2.0.0-rc.1 - December 20, 2020
1dd7089
Fix: npm prepare script on Windows (refs #166) (#168) (Brandon Mills)23ac2b9
Fix: Ignore words in info string after syntax (fixes #166) (#167) (Brandon Mills)8f729d3
Chore: Switch to main for primary branch (fixes #161) (#165) (Brandon Mills)d30c50f
Chore: Automatically install example dependencies (#164) (Brandon Mills)
v2.0.0-rc.0 - August 19, 2020
d94c22f
Build: Install example test dependencies in Jenkins (#160) (Brandon Mills)7f26cb9
Docs: Reference recommended config disabled rules (#159) (Brandon Mills)bf7648f
Docs: Add TypeScript example (#155) (Brandon Mills)d80be9e
New: Add rules to recommended config (#157) (Nikolay Stoynov)fc4d7aa
Chore: run CI in Node 14.x (#158) (Kai Cataldo)f2d4923
Docs: Add React example (#152) (Brandon Mills)eb66833
New: Add recommended config (fixes #151) (#153) (Brandon Mills)0311640
Fix: Don't require message end locations (fixes #112) (#154) (Brandon Mills)
v2.0.0-alpha.0 - April 12, 2020
51e48c6
Docs: Revamp documentation for v2 (#149) (Brandon Mills)b221391
Docs: Dogfood plugin by linting readme (#145) (Brandon Mills)7423610
Docs: Explain use of --ext option in ESLint v7 (#146) (Brandon Mills)0d4dbe8
Breaking: Implement new processor API (fixes #138) (#144) (Brandon Mills)7eeafb8
Chore: Update ESLint config and plugins (#143) (Brandon Mills)f483343
Breaking: Require ESLint v6 (#142) (Brandon Mills)9aa1fdc
Chore: Use ES2018 object spread syntax (#141) (Brandon Mills)f584cc6
Build: Remove Travis (#140) (Brandon Mills)35f9a11
Breaking: Drop support for Node.js v6 (refs #138) (#137) (Brandon Mills)6f02ef5
Chore: Add npm version and build status badges (#139) (Brandon Mills)
v1.0.2 - February 24, 2020
52e0984
Upgrade: Update devDeps and change istanbul -> nyc (#130) (Brett Zamir)d52988f
Chore: Remove call to lint absent Makefile.js
(#129) (Brett Zamir)5640ea6
Fix: Apply base indent to multiple line breaks (fixes #127) (#128) (Brett Zamir)
v1.0.1 - October 21, 2019
fb0b5a3
Fix: Indent multiline fixes (fixes #120) (#124) (Brandon Mills)07c9017
Chore: Use GitHub Actions (#123) (Brandon Mills)b5bf014
Chore: Add Node 12 to Travis (#122) (Brandon Mills)dc90961
Fix: Support autofix at the very start of blocks (fixes #117) (#119) (Simon Lydell)2de2490
Docs: Syntax highlight Markdown (#116) (Brett Zamir)fdacf0c
Chore: Upgrade to eslint-config-eslint@5.0.1 (#110) (Brandon Mills)
v1.0.0 - January 2, 2019
2a8482e
Fix: overrides
general docs and Atom linter-eslint tips (fixes #109) (#111) (Brett Zamir)
v1.0.0-rc.1 - November 5, 2018
- a2f4492 Fix: Allowing eslint-plugin-prettier to work (fixes #101) (#107) (simlu)
v1.0.0-rc.0 - October 27, 2018
- 8fe9a0e New: Enable autofix with --fix (fixes #58) (#97) (Bohdan Khodakivskyi)
- a5d0cce Fix: Ignore anything after space in code fence's language (fixes #98) (#99) (Francisco Ryan Tolmasky I)
- 6fd340d Upgrade: eslint-release@1.0.0 (#100) (Teddy Katz)
- dff8e9c Fix: Emit correct endLine numbers (#88) (Paul Murray)
- 83f00d0 Docs: Suggest disabling strict in .md files (fixes #94) (#95) (Brandon Mills)
- 3b4ff95 Build: Test against Node v10 (#96) (Brandon Mills)
- 6777977 Breaking: required node version 6+ (#89) (薛定谔的猫)
- 5582fce Docs: Updating CLA link (#93) (Pablo Nevares)
- 24070e6 Build: Upgrade to eslint-release@0.11.1 (#92) (Brandon Mills)
- 6cfd1f0 Docs: Add unicode-bom to list of unsatisfiable rules (#91) (Brandon Mills)
v1.0.0-beta.8 - April 8, 2018
- a1544c2 Chore: Add .npmrc to disable creating package-lock.json (#90) (Brandon Mills)
- 47ad3f9 Chore: Replace global comment integration test with unit test (refs #81) (#85) (Brandon Mills)
- e34acc6 Fix: Add unicode-bom to unsatisfiable rules (refs #75) (#84) (Brandon Mills)
- 7c19f8b Fix: Support globals (fixes #79) (#81) (Anders D. Johnson)
v1.0.0-beta.7 - July 2, 2017
- f8ba18a New: Custom eslint-skip HTML comment skips blocks (fixes #69) (#73) (Brandon Mills)
- 249904f Chore: Add test for code fences without blank lines (#72) (Brandon Mills)
- 3abc569 Chore: Un-disable strict and eol-last in repository (#71) (Brandon Mills)
- 132ea5b Chore: Add test ensuring config comments do not fall through (#70) (Brandon Mills)
v1.0.0-beta.6 - April 29, 2017
- c5e9d67 Build: Explicitly specify package.json files (#67) (Brandon Mills)
v1.0.0-beta.5 - April 29, 2017
- 7bd0f6e Build: Install eslint-release (#66) (Brandon Mills)
- 48122eb Build: Dogfood plugin without npm link (#65) (Brandon Mills)
- cc7deea Chore: Increase code coverage (#64) (Brandon Mills)
- 29f2f05 Build: Use eslint-release (#63) (Brandon Mills)
- d2f2962 Upgrade: remark (#62) (Titus)