Skip to content

Latest commit

 

History

History
153 lines (108 loc) · 2.61 KB

File metadata and controls

153 lines (108 loc) · 2.61 KB

MD031 - Fenced code blocks should be surrounded by blank lines

Aliases: blanks-around-fences

Tags: blank_lines, code

Fixable: Some violations can be fixed by tooling

Rule Details

This rule is triggered when fenced code blocks are either not preceded or not followed by a blank line:

Some text
```
Code block
```

```
Another code block
```
Some more text

To fix this, ensure that all fenced code blocks have a blank line both before and after (except where the block is at the beginning or end of the document):

Some text

```
Code block
```

```
Another code block
```

Some more text

Configuration

This rule supports one configuration parameter:

list_items (boolean, default: true)

Set the list_items parameter to false to disable this rule for list items. Disabling this behavior for lists can be useful if it is necessary to create a tight list containing a code fence.

Example configuration:

[linters.settings.blanks-around-fences]
list_items = false

Example with list_items = true (default):

1. First item
   ```javascript
   const x = 1;
   ```
2. Second item

This would trigger MD031 violations (missing blank lines around the code block).

Example with list_items = false:

1. First item
   ```javascript
   const x = 1;
   ```
2. Second item

This would NOT trigger MD031 violations, allowing tight lists with code blocks.

Rationale

Aside from aesthetic reasons, some parsers, including kramdown, will not parse fenced code blocks that don't have blank lines before and after them. Ensuring proper spacing around code blocks improves compatibility across different Markdown parsers and enhances readability.

Examples

Correct ✅

Some text here.

```javascript
const greeting = "Hello, World!";
console.log(greeting);
```

More text here.
# Document start

```bash
echo "This is fine at document start"
```

Some text.

```python
print("This is properly spaced")
```

# Document continues

Incorrect ❌

Some text here.
```javascript
const greeting = "Hello, World!";
console.log(greeting);
```
More text here.
Some text here.

```bash
echo "Missing blank line after"
```
More text immediately following.

List Items (with default list_items = true)

<!-- This triggers violations -->
1. First item
   ```javascript
   const x = 1;
   ```
2. Second item

<!-- This is correct -->
1. First item

   ```javascript
   const x = 1;
   ```

2. Second item