Supported extensions are
Install the plugin:
npm install --save-dev eslint eslint-plugin-markdown
Add it to your
Run ESLint on
eslint --ext md .
It will lint
node fenced code blocks in your Markdown documents:
Blocks that don't specify either
node syntax are ignored:
```This is plain text and doesn't get linted.``````pythonprint("This doesn't get linted either.")```
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;```
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;```But the next code block will have an error from `no-alert`:<!-- eslint-env browser -->```js;```
Sometimes it can be useful to have code blocks marked with
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 betterhighlighting. Skip the block to prevent warnings about invalid syntax.<!-- eslint-skip -->```js// This code block is hidden from ESLint."hello": "world"``````jsconsole;```
Fix issues automatically
This plugin can attempt to fix some of the issues automatically using
fix ESLint option. This option instructs ESLint to try to fix as many issues as possible. To enable this option you can add
--fix to your ESLint call, for example:
eslint --fix --ext md .
Since code blocks are not files themselves but embedded inside a Markdown document, some rules do not apply to Markdown code blocks, and messages from these rules are automatically suppressed:
Project or directory-wide overrides for code snippets
ESLint allows a configuration property
overrides which has a
property which accepts a
glob pattern, allowing you to designate files (such as all
md files) whose rules will
The following example shows the disabling of a few commonly problematic rules
for code snippets. It also points to the fact that some rules
padded-blocks) may be more appealing for disabling given that
one may wish for documentation to be more liberal in providing padding for
// .eslintrc.json// ..."overrides":"files": "**/*.md""rules":"no-undef": "off""no-unused-vars": "off""no-console": "off""padded-blocks": "off"
strict rule is technically satisfiable inside of Markdown code blocks, but writing a
"use strict" directive at the top of every code block is tedious and distracting. We recommend a glob pattern for
.md files to disable
strict and enable the
impliedStrict parser option so the code blocks still parse in strict mode:
// .eslintrc.json// ..."overrides":"files": "**/*.md""parserOptions":"ecmaFeatures":"impliedStrict": true"rules":"strict": "off"
Tips for use with Atom linter-eslint
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.
$ 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.