Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

MD040 - Fenced Code Blocks Should Have a Language Specified

Severity: Warning
Category: Code
Auto-fix: Not available

Rule Description

This rule ensures that fenced code blocks specify a language for syntax highlighting. Language tags improve readability and enable proper syntax highlighting in rendered output.

Why This Rule Exists

Language specifications are important because:

  • Enables syntax highlighting in rendered markdown
  • Improves code readability and comprehension
  • Helps readers quickly identify the programming language
  • Ensures consistent code block presentation
  • Required by many documentation tools (including mdBook)

Examples

❌ Incorrect (violates rule)

```
function hello() {
    console.log("Hello, world!");
}
```

```
SELECT * FROM users WHERE active = true;
```

✅ Correct

```javascript
function hello() {
    console.log("Hello, world!");
}
```

```sql
SELECT * FROM users WHERE active = true;
```

```bash
echo "Shell commands also benefit from highlighting"
```

```text
Plain text can be explicitly marked
```

Configuration

[MD040]
allowed_languages = []  # List of allowed languages (empty = all allowed)
language_optional = false  # Whether language tag is optional (default: false)

Common Language Tags

LanguageTags
JavaScriptjs, javascript
TypeScriptts, typescript
Pythonpy, python
Rustrs, rust
Shellsh, bash, shell
JSONjson
YAMLyml, yaml
Markdownmd, markdown
Plain Texttext, txt
TOMLtoml
HTMLhtml
CSScss
SQLsql

When to Disable

Consider disabling this rule if:

  • Your markdown renderer doesn't support syntax highlighting
  • You have many code blocks where language is obvious from context
  • You're using custom code block processors that don't require language tags

Disable in Config

# .mdbook-lint.toml
disabled_rules = ["MD040"]

Disable Inline

<!-- mdbook-lint-disable MD040 -->
```
Code block without language tag
```
<!-- mdbook-lint-enable MD040 -->
  • MD046 - Code block style
  • MD048 - Code fence style
  • MDBOOK001 - Code blocks should have language tags (mdBook-specific)

References