Code Rules
These rules ensure proper formatting of code blocks and inline code in markdown documents.
Rules in This Category
- MD040 - Fenced code blocks should have a language specified
- MD014 - Dollar signs used before commands without showing output
- MD031 - Fenced code blocks should be surrounded by blank lines
- MD038 - Spaces inside code span elements
- MD046 - Code block style
- MD048 - Code fence style
Why Code Rules Matter
Proper code formatting:
- Enables syntax highlighting for better readability
- Maintains consistency across code examples
- Improves copy-paste reliability
- Ensures proper rendering in different viewers
- Helps readers identify programming languages quickly
Code Block Styles
Fenced Code Blocks (Recommended)
```javascript
function example() {
return "Hello, world!";
}
```
Indented Code Blocks
function example() {
return "Hello, world!";
}
Inline Code
Use the `console.log()` function to debug.
Language Specifications
Common language tags for syntax highlighting:
Language | Tags |
---|---|
JavaScript | js , javascript |
TypeScript | ts , typescript |
Python | py , python |
Rust | rs , rust |
Shell | sh , bash , shell |
JSON | json |
YAML | yml , yaml |
Quick Configuration
# .mdbook-lint.toml
# Configure MD040 - Require language tags
[MD040]
allowed_languages = ["js", "python", "rust", "bash"]
# Configure MD046 - Code block style
[MD046]
style = "fenced" # Options: "fenced", "indented", "consistent"
# Configure MD048 - Code fence style
[MD048]
style = "backtick" # Options: "backtick", "tilde", "consistent"
Best Practices
- Always specify language: Enables syntax highlighting
- Use fenced blocks: More flexible than indented blocks
- Surround with blank lines: Improves readability
- Be consistent: Use the same style throughout
- Escape special characters: Use backslash when needed
Shell Commands
# Good - shows command without prompt
npm install mdbook-lint
# Avoid - dollar sign without output
$ npm install mdbook-lint
Related Categories
- mdBook Rules - mdBook-specific code requirements
- Whitespace Rules - General spacing rules