• Stars
    star
    405
  • Rank 106,656 (Top 3 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Lint JavaScript code blocks in Markdown documents

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 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:

// .eslintrc.js
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.

// .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 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: [
        // ...
        {
            // 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 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.

// .eslintrc.js for eslint-plugin-markdown v1
module.exports = {
    plugins: ["markdown"],
    overrides: [
        {
            files: ["**/*.md"],
            // In v1, configuration for fenced code blocks went inside an
            // `overrides` entry with a .md pattern, for example:
            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.

// eslintrc.js for eslint-plugin-markdown v2
module.exports = {
    plugins: ["markdown"],
    overrides: [
        {
            // In v2, explicitly apply eslint-plugin-markdown's `markdown`
            // processor on any Markdown files you want to lint.
            files: ["**/*.md"],
            processor: "markdown/markdown"
        },
        {
            // In v2, configuration for fenced code blocks is separate from the
            // containing Markdown file. Each code block has a virtual filename
            // appended to the Markdown file's path.
            files: ["**/*.md/*.js"],
            // Configuration for fenced code blocks goes with the override for
            // the code block's virtual filename, for example:
            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:

// eslintrc.js for v2 mimicking v1 behavior
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.")
```

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

More Repositories

1

eslint

Find and fix problems in your JavaScript code.
JavaScript
25,026
star
2

js

Monorepo for the JS language tools.
JavaScript
2,283
star
3

typescript-eslint-parser

An ESLint custom parser which leverages TypeScript ESTree to allow for ESLint to lint TypeScript source code.
JavaScript
916
star
4

config-inspector

A visual tool for inspecting and understanding your ESLint flat configs.
Vue
697
star
5

doctrine

JSDoc parser
JavaScript
456
star
6

generator-eslint

A Yeoman generator to help with ESLint development
JavaScript
230
star
7

rewrite

Monorepo for the new version of ESLint
JavaScript
156
star
8

rfcs

Repo for managing Requests For Comments (RFCs) for the ESLint project
129
star
9

eslint-scope

eslint-scope: ECMAScript scope analyzer
JavaScript
125
star
10

eslintrc

The legacy ESLintRC config file format for ESLint
JavaScript
118
star
11

archive-website

The ESLint website
JavaScript
96
star
12

create-config

Utility to create ESLint config files
JavaScript
76
star
13

eslint.org

ESLint website
JavaScript
75
star
14

eslint-github-bot

Plugin-based GitHub bot for ESLint
JavaScript
65
star
15

eslint-cli

The local eslint executor.
JavaScript
57
star
16

eslint-jp

Repository for creating and triaging issues in Japanese
47
star
17

eslint-visitor-keys

Constants and utilities about visitor keys to traverse AST.
JavaScript
42
star
18

json

JSON language plugin for ESLint
JavaScript
38
star
19

cn.eslint.org

Chinese ESLint documentation
JavaScript
27
star
20

eslint-transforms

Codemods for the ESLint ecosystem
JavaScript
25
star
21

eslint-release

The ESLint release tool
JavaScript
24
star
22

tsc-meetings

Technical Steering Committee meeting notes, proposals, and related information
JavaScript
20
star
23

zh-hans.docs.eslint.org

Simplified Chinese website
JavaScript
15
star
24

playground

ESLint online playground
JavaScript
11
star
25

eslint-canary

ESLint regression build
JavaScript
10
star
26

css

CSS language plugin for ESLint
10
star
27

code-explorer

TypeScript
9
star
28

eslint-tester

(Deprecated) A testing utility for ESLint
JavaScript
9
star
29

github-action

A GitHub action to run ESLint on pull requests
JavaScript
5
star
30

.github

Community health files for ESLint
1
star