Skip to content

MD046 - Use Consistent Code Block Style

Aliases: code-block-style

What this rule does

Ensures all code blocks in your document:

  • Use the same style - either fenced (with backticks) or indented (with spaces)
  • Are properly closed when using fenced style

Why this matters

  • Readability: Mixing styles makes documents harder to scan and understand
  • Maintainability: Consistent style makes code blocks easier to edit
  • Tool compatibility: Some tools work better with one style over another
  • Professional appearance: Consistency shows attention to detail

Examples

✅ Correct (Fenced style)

Here's how to use our API:

```javascript
const api = require('our-api');
api.connect();
```

And here's the response format:

```json
{
  "status": "success",
  "data": {}
}
```

❌ Incorrect (Mixed styles)

Here's how to use our API:

```javascript
const api = require('our-api');
api.connect();
```

And here's the response format:

    {
      "status": "success",
      "data": {}
    }

🔧 Fixed (Mixed styles)

Here's how to use our API:

```javascript
const api = require('our-api');
api.connect();
```

And here's the response format:

```json
{
  "status": "success",
  "data": {}
}
```

❌ Incorrect (Unclosed code block)

```javascript
const api = require('our-api');
api.connect();

Some text after the code that was meant to be outside the block.

🔧 Fixed (Unclosed code block)

```javascript
const api = require('our-api');
api.connect();
```

Some text after the code that was meant to be outside the block.

❌ Incorrect (Missing closing fence before new block)

```javascript
const api = require('our-api');
api.connect();

```python
print("hello")
```

🔧 Fixed (Missing closing fence before new block)

```javascript
const api = require('our-api');
api.connect();
```

```python
print("hello")
```

Configuration

[MD046]
style = "consistent"  # Options: "consistent", "fenced", "indented"

Style options

Value Description
consistent All code blocks must use the same style (default)
fenced All code blocks must use fenced style (``` or ~)
indented All code blocks must use indented style (4 spaces)

Automatic fixes

When enabled, this rule will:

  • Convert all code blocks to match your configured style
  • Preserve code content and language identifiers
  • Maintain proper spacing around blocks
  • Close fenced code blocks that are missing a closing fence
  • Insert closing fences when a new code block opens before the previous one is closed

Style comparison

Fenced code blocks (recommended):

  • Support syntax highlighting with language identifiers
  • Easier to copy and paste
  • More visible boundaries

Indented code blocks:

  • Traditional Markdown style
  • No language identifier support
  • Must indent each line with 4 spaces or 1 tab

Special cases

  • Empty code blocks are ignored
  • HTML <pre> and <code> tags are not affected
  • When using "consistent", the most prevalent style is used (in case of a tie, fenced style is preferred as it's more widely supported)
  • Per CommonMark, fenced code block openers (``` or ~~~) can only have 0-3 spaces of indentation. With 4+ spaces, the fence markers become literal content in an indented code block
  • Unclosed-block detection is skipped in documents containing markdown or md code blocks, since these often include fence examples that would be misdetected

Learn more

  • MD031 - Add blank lines around code blocks
  • MD040 - Specify languages for code blocks
  • MD048 - Use consistent fence markers (backticks vs tildes)