markdown-preferences/code-fence-length
enforce consistent code fence length in fenced code blocks.
- ⚙️ This rule is included in
plugin.configs.standard. - 🔧 The
--fixoption on the command line can automatically fix some of the problems reported by this rule.
📖 Rule Details
This rule enforces a consistent length for code fences (```, ~~~) in fenced code blocks. The opening and closing fence lengths must always match, regardless of configuration. This improves readability and consistency in Markdown files.
What This Rule Checks
- The opening and closing code fence lengths must always match. (Always reported if not matched)
- You can enforce a fixed fence length (e.g. always 3 backticks)
- You can specify fallback behavior if the fixed length cannot be used (e.g. due to content)
- You can override the length per language (info string)
Examples
Default (length: 3)
md
<!-- eslint markdown-preferences/code-fence-length: ['error', { "length": 3 }] -->
<!-- ✓ GOOD -->
```js
console.log("ok")
```
<!-- ✗ BAD -->
````js
console.log("bad")
````
<!-- ✗ BAD (mismatched) -->
```js
console.log("bad")
````With fallbackLength: "minimum"
md
<!-- eslint markdown-preferences/code-fence-length: ['error', { "length": 3, "fallbackLength": "minimum" }] -->
<!-- ✓ GOOD (content contains 3 backticks, so fence is 4) -->
````md
```
code
```
````
<!-- ✗ BAD (should use 4) -->
`````md
```
code
```
`````With overrides
md
<!-- eslint markdown-preferences/code-fence-length: ['error', { "length": 3, "overrides": [{ "lang": "md", "length": 4 }] }] -->
<!-- ✓ GOOD (markdown code block uses 4) -->
````md
```js
console.log("sample")
```
````
<!-- ✗ BAD (should use 4 for md) -->
```md
# Sample
```
<!-- ✓ GOOD (js code block uses 3) -->
```js
console.log("js")
```🔧 Options
json
{
"markdown-preferences/code-fence-length": [
"error",
{
"length": 3,
"fallbackLength": "minimum",
"overrides": [{ "lang": "md", "length": 4, "fallbackLength": "minimum" }]
}
]
}length(number, default:3):
Enforces a fixed code fence length (must be 3 or greater). The specified number of backticks (`) or tildes (~) will be used for code fences.fallbackLength(number|"minimum"|"as-is", default:"minimum"):
Specifies the fallback behavior if the fixedlengthcannot be used (for example, if the code block content contains a sequence of backticks/tilde of the same or greater length)."minimum": Use the minimum required length (the longest sequence in the content plus one).number: Use this number or the minimum required, whichever is greater."as-is": Allow the current length and do not report or fix.
overrides(Array<object>):
Allows you to overridelengthandfallbackLengthfor specific languages (info strings). Each item is an object with the following properties:lang(string):
The info string (language) to match for the code block (e.g.,"js","md","sh"). If the code block's info string matches this value, this override is applied instead of the global setting.length(number):
The preferred code fence length for this language (must be 3 or greater).fallbackLength(number|"minimum"|"as-is", optional):
Fallback behavior for this language. If omitted, the globalfallbackLengthis used.
📚 Further Reading
👫 Related Rules
- markdown-preferences/code-fence-spacing
- markdown-preferences/code-fence-style
- markdown-preferences/prefer-fenced-code-blocks
🚀 Version
This rule was introduced in eslint-plugin-markdown-preferences v0.20.0