Skip to content

consistent-code-style

🎨 Stylistic ⭐ CommonMark 🌟 GFM

🔗 Rule Source 🔗 Test Source

Enforce consistent code style.

Rule Details

This rule enforces a single, consistent style for code blocks in Markdown files. Consistent formatting makes it easier to understand a document, and mixing different code block styles can reduce readability.

A code block is defined as either an indented code block (4 spaces or 1 tab), a fenced code block with backticks (```), or a fenced code block with tildes (~~~). While Markdown allows any of these styles, this rule ensures that only one is used throughout the document.

Examples

❌ Incorrect

Examples of incorrect code for this rule:

Default

md
<!-- eslint md/consistent-code-style: 'error' -->

    code block 1

```
code block 2 ```
~~~
code block 3 ~~~
md
<!-- eslint md/consistent-code-style: 'error' -->

```
code block 1
```

code block 2
~~~
code block 3 ~~~
md
<!-- eslint md/consistent-code-style: 'error' -->

~~~
code block 1
~~~

code block 2
```
code block 3 ```

With { style: 'indent' } Option

md
<!-- eslint md/consistent-code-style: ['error', { style: 'indent' }] -->

```
code block 1 ```
~~~
code block 2 ~~~

With { style: 'fence-backtick' } Option

md
<!-- eslint md/consistent-code-style: ['error', { style: 'fence-backtick' }] -->

code block 1
~~~
code block 2 ~~~

With { style: 'fence-tilde' } Option

md
<!-- eslint md/consistent-code-style: ['error', { style: 'fence-tilde' }] -->

code block 1
```
code block 2 ```

With { blankLineAbove: 1 } Option

md
<!-- eslint md/consistent-code-style: ['error', { blankLineAbove: 1 }] -->

Paragraph
```js
console.log('missing blank line above'); ```

With { blankLineBelow: 1 } Option

md
<!-- eslint md/consistent-code-style: ['error', { blankLineBelow: 1 }] -->

```js
console.log('missing blank line below'); ``` Paragraph

✅ Correct

Examples of correct code for this rule:

Default

md
<!-- eslint md/consistent-code-style: 'error' -->

    code block 1

---

    code block 2
md
<!-- eslint md/consistent-code-style: 'error' -->

```
code block 1
```

```
code block 2
```
md
<!-- eslint md/consistent-code-style: 'error' -->

~~~
code block 1
~~~

~~~
code block 2
~~~

With { style: 'indent' } Option

md
<!-- eslint md/consistent-code-style: ['error', { style: 'indent' }] -->

    code block 1

With { style: 'fence-backtick' } Option

md
<!-- eslint md/consistent-code-style: ['error', { style: 'fence-backtick' }] -->

```
code block 1
```

With { style: 'fence-tilde' } Option

md
<!-- eslint md/consistent-code-style: ['error', { style: 'fence-tilde' }] -->

~~~
code block 1
~~~

With { blankLineAbove: 1 } Option

md
<!-- eslint md/consistent-code-style: ['error', { blankLineAbove: 1 }] -->

Paragraph

```js
console.log('blank line above');
```

With { blankLineBelow: 1 } Option

md
<!-- eslint md/consistent-code-style: ['error', { blankLineBelow: 1 }] -->

```js
console.log('blank line below');
```

Paragraph

Options

js
'md/consistent-code-style': ['error', {
  style: 'consistent',
  blankLineAbove: false,
  blankLineBelow: false,
}]

style

Type: 'consistent' | 'indent' | 'fence-backtick' | 'fence-tilde' / Default: 'consistent'

When style is set to 'consistent', the rule enforces that all code blocks in the document use the same style as the first one encountered.

You can also specify a particular style by setting style to 'indent', 'fence-backtick', or 'fence-tilde', which will enforce that all code blocks use the specified style.

blankLineAbove

Type: number | false / Default: false

Require a specific number of blank lines above each fenced code block.

Set this option to false to disable the blank line check above fenced code blocks. Set it to a positive integer to require that many blank lines before every fenced code block.

This option does not apply to indented code blocks

For compatibility with markdownlint's MD031 - Fenced code blocks should be surrounded by blank lines, this option applies only to fenced code blocks.

blankLineBelow

Type: number | false / Default: false

Require a specific number of blank lines below each fenced code block.

Set this option to false to disable the blank line check below fenced code blocks. Set it to a positive integer to require that many blank lines after every fenced code block.

This option does not apply to indented code blocks

For compatibility with markdownlint's MD031 - Fenced code blocks should be surrounded by blank lines, this option applies only to fenced code blocks.

Prior Art