Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

eslint-plugin-markdown

eslint2.1mMIT5.1.0TypeScript support: definitely-typed

An ESLint plugin to lint JavaScript in Markdown code fences.

eslint, eslintplugin, markdown, lint, linter

readme

eslint-plugin-markdown

npm Version Downloads Build Status

Lint JS, JSX, TypeScript, and more inside Markdown.

A JS code snippet in a Markdown editor has red squiggly underlines. A tooltip explains the problem.

Usage

Installing

Install the plugin alongside ESLint v8 or greater:

npm install --save-dev eslint eslint-plugin-markdown

Configuring

In your eslint.config.js file, import eslint-plugin-markdown and include the recommended config to enable the Markdown processor on all .md files:

// eslint.config.js
import markdown from "eslint-plugin-markdown";

export default [
    ...markdown.configs.recommended

    // your other configs here
];

If you are still using the deprecated .eslintrc.js file format for ESLint, you can extend the plugin:markdown/recommended-legacy config to enable the Markdown processor on all .md files:

// .eslintrc.js
module.exports = {
    extends: "plugin:markdown/recommended-legacy"
};

Advanced Configuration

You can manually include the Markdown processor by setting the processor option in your configuration file for all .md 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, except for the following:

  • javascript and ecmascript are mapped to js
  • typescript is mapped to ts
  • markdown is mapped to md

For example, ```js code blocks in README.md would match README.md/*.js and ```typescript in CONTRIBUTING.md would match CONTRIBUTING.md/*.ts.

You can use glob patterns for these virtual filenames to customize configuration for code blocks without affecting regular code. For more information on configuring processors, refer to the ESLint documentation.

Here's an example:

// eslint.config.js
import markdown from "eslint-plugin-markdown";

export default [
    {
        // 1. Add the plugin
        plugins: {
            markdown
        }
    },
    {
        // 2. Enable the Markdown processor for all .md files.
        files: ["**/*.md"],
        processor: "markdown/markdown"
    },
    {
        // 3. Optionally, customize the configuration ESLint uses for ```js
        // fenced code blocks inside .md files.
        files: ["**/*.md/*.js"],
        // ...
        rules: {
            // ...
        }
    }

    // your other configs here
];

In the deprecated .eslintrc.js format:

// .eslintrc.js
module.exports = {
    // 1. Add the plugin.
    plugins: ["markdown"],
    overrides: [
        {
            // 2. Enable the Markdown processor for all .md files.
            files: ["**/*.md"],
            processor: "markdown/markdown"
        },
        {
            // 3. Optionally, customize the configuration ESLint uses for ```js
            // fenced code blocks inside .md files.
            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 markdown.configs.recommended config disables these rules in Markdown files:

Use glob patterns to disable more rules just for Markdown code blocks:

// / eslint.config.js
import markdown from "eslint-plugin-markdown";

export default [
    {
        plugins: {
            markdown
        }
    },
    {
        files: ["**/*.md"],
        processor: "markdown/markdown"
    },
    {
        // 1. Target ```js code blocks in .md files.
        files: ["**/*.md/*.js"],
        rules: {
            // 2. Disable other rules.
            "no-console": "off",
            "import/no-unresolved": "off"
        }
    }

    // your other configs here
];

And in the deprecated .eslintrc.js format:

// .eslintrc.js
module.exports = {
    plugins: ["markdown"],
    overrides: [
        {
            files: ["**/*.md"],
            processor: "markdown/markdown"
        },
        {
            // 1. Target ```js code blocks in .md files.
            files: ["**/*.md/*.js"],
            rules: {
                // 2. Disable other rules.
                "no-console": "off",
                "import/no-unresolved": "off"
            }
        }
    ]
};

Strict Mode

"use strict" directives in every code block would be annoying. The markdown.configs.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 markdown.configs.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.

Running

If you are using an eslint.config.js file, then you can run ESLint as usual and it will pick up file patterns in your config file. The --ext option is not available when using flat config.

If you are using an .eslintrc.* file, then you can run ESLint as usual and it will pick up file extensions specified in overrides[].files patterns in config files.

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 file patterns in the `eslint.config.js`.
// Or `overrides[].files` in `eslintrc.*`.
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 file patterns in your config file, it will be ignored.

Configuration Comments

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 alert global variable, disables the no-alert rule, and configures the quotes rule to prefer single quotes:

<!-- global alert -->
<!-- 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 `eslint.config.js`, the first code block will have no error from `no-alert`:

<!-- global alert -->
<!-- eslint-disable no-alert -->

```js
alert("Hello, world!");
```

But the next code block will have an error from `no-alert`:

<!-- global alert -->

```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.

changelog

Changelog

5.1.0 (2024-07-05)

Features

5.0.0 (2024-05-01)

⚠ BREAKING CHANGES

  • map known code block languages to respective file extensions (#246)

Features

  • map known code block languages to respective file extensions (#246) (096cff4)

Bug Fixes

  • check upper bounds of message line numbers for code blocks (#247) (00adccb)

Chores

  • run tests in Node.js 22, with ESLint 9 (#250) (085e6d5)
  • switch to eslint v9, eslint-config-eslint v10 (#251) (a76cdf5)

4.0.1 (2024-03-06)

Chores

  • update changelog with note that v4.0.0 was not published to npm (#241) (e80139d)

4.0.0 (2024-03-01)

⚠️ This release was not published to npm due to technical problems.

⚠ BREAKING CHANGES

  • drop eslint < 8 & Node.js < 18 (#238)
  • Switch to flat config (#232)

Features

Documentation

  • fix expected errors in react example (#237) (a758163)

Chores

  • add .vscode to .gitignore (#236) (cbb8e3a)
  • bump setup-node and checkout actions to v4 in release workflow (#239) (3fd99ad)
  • remove unused @eslint/eslintrc in test examples (#234) (4e2e160)
  • run tests in Node.js 21 (#225) (4d9f36f)
  • standardize npm script names (#223) (6bdff60)
  • use latest typescript-eslint in examples (#235) (313959b)

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)