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

MDBOOK001 - Code Blocks Should Have Language Tags

Code blocks should have language tags.

This rule is triggered when code blocks don't have language tags for syntax highlighting. Proper language tags help with documentation clarity and proper rendering in mdBook.

Why This Rule Exists

mdBook uses language tags for:

  • Syntax highlighting in rendered output
  • Proper code formatting and display
  • Enabling language-specific features (like line numbers, highlighting specific lines)
  • Improving accessibility for screen readers
  • Better SEO and content understanding

Examples

❌ Incorrect (violates rule)

```
fn main() {
    println!("Hello, world!");
}
```

✅ Correct

```rust
fn main() {
    println!("Hello, world!");
}
```

Other valid examples:

```bash
cargo build --release
```

```toml
[dependencies]
serde = "1.0"
```

```json
{
  "name": "example",
  "version": "1.0.0"
}
```

Special Language Tags

mdBook supports special language tags:

  • text or plain - for plain text without highlighting
  • console - for command-line output
  • diff - for showing differences
  • ignore - for Rust code that shouldn't be tested
  • no_run - for Rust code that compiles but shouldn't run
  • should_panic - for Rust code expected to panic

Configuration

This rule has no configuration options. All code blocks should have language tags.

When to Disable

Consider disabling this rule if:

  • You have many legacy code blocks without language tags
  • You're using a custom mdBook renderer that doesn't require language tags

Rule Details

  • Rule ID: MDBOOK001
  • Category: mdBook-specific
  • Severity: Warning
  • Automatic Fix: Not available (requires manual language identification)

Why Language Tags Matter in mdBook

Syntax Highlighting

mdBook uses language tags to apply syntax highlighting via highlight.js or similar libraries:

Without language tag:

```
fn main() {
    println!("Hello, world!");
}
```

Renders as plain text with no highlighting.

With language tag:

```rust
fn main() {
    println!("Hello, world!");
}
```

Renders with proper Rust syntax highlighting.

mdBook-Specific Features

Language tags enable mdBook-specific features:

Rust Playground Integration

fn main() {
    println!("This code can be run in the Rust Playground!");
}

Hidden Lines in Rust Code

```rust
# fn main() {
println!("Only this line is shown");
# }
```

Test Annotations

```rust,ignore
// This code won't be tested
fn example() {}
```

```rust,no_run
// This code is compiled but not run
fn main() {
    loop {} // Would hang if run
}
```

```rust,should_panic
// This code is expected to panic
fn main() {
    panic!("This is expected!");
}
```

Common Language Tags

Programming Languages

LanguageTagCommon Uses
RustrustPrimary language for mdBook documentation
JavaScriptjavascript or jsWeb examples, Node.js code
Pythonpython or pyScripts, examples
Shellbash or shCommand-line examples
TOMLtomlConfiguration files
JSONjsonData structures, APIs
YAMLyaml or ymlConfiguration, CI/CD
HTMLhtmlWeb markup
CSScssStyling examples
SQLsqlDatabase queries

Special Tags

TagPurpose
text or plainPlain text without highlighting
consoleTerminal output with prompt highlighting
diffShowing differences with +/- highlighting
markdown or mdMarkdown source code

Examples by Use Case

Configuration Files

TOML (Cargo.toml):

```toml
[package]
name = "my-project"
version = "0.1.0"

[dependencies]
serde = "1.0"
```

JSON (package.json):

```json
{
  "name": "my-project",
  "version": "1.0.0",
  "dependencies": {
    "react": "^18.0.0"
  }
}
```

Command-Line Examples

Shell commands:

```bash
# Install mdbook-lint
cargo install mdbook-lint

# Run the linter
mdbook-lint check
```

Console output:

```console
$ cargo build
   Compiling my-project v0.1.0
    Finished dev [unoptimized] target(s) in 2.34s
```

Showing Changes

Diff format:

```diff
- Old line that was removed
+ New line that was added
  Unchanged line
```

Multi-language Examples

HTML with embedded CSS and JavaScript:

```html
<!DOCTYPE html>
<html>
<head>
    <style>
        body { font-family: sans-serif; }
    </style>
</head>
<body>
    <h1>Hello</h1>
    <script>
        console.log('Hello, world!');
    </script>
</body>
</html>
```

Choosing the Right Language Tag

Decision Tree

  1. Is it code? → Use appropriate language tag
  2. Is it terminal output? → Use console
  3. Is it a diff? → Use diff
  4. Is it plain text? → Use text or plain
  5. Is it data? → Use format tag (json, yaml, toml)
  6. Not sure? → Use text rather than no tag

Language Detection Tips

Look for characteristic syntax:

  • Rust: fn, let, mut, impl, ::
  • Python: def, import, : for blocks, no semicolons
  • JavaScript: function, const, =>, var
  • Shell: $, # for comments, command names
  • JSON: {, }, :, quoted keys
  • TOML: [sections], key = value, # comments

Edge Cases

Mixed Language Blocks

For templates or mixed content, choose the primary language:

```html
<!-- This is primarily HTML even though it contains CSS -->
<div style="color: red;">Content</div>
```

Unknown or Custom Languages

For unsupported languages, use text:

```text
CUSTOM_SYNTAX {
    nonstandard = syntax
}
```

File Names as Context

Sometimes include the filename for context:

```rust
// src/main.rs
fn main() {
    println!("Hello!");
}
```

Impact on mdBook Features

Search Indexing

Code blocks with language tags are better indexed for search.

Syntax Theme Support

Language tags enable proper theme application:

  • Light themes show appropriate colors
  • Dark themes adjust for readability
  • Contrast themes maintain accessibility

Copy Button

mdBook's copy button works better with properly tagged code blocks.

Line Numbers

Some themes show line numbers only for tagged code blocks:

```rust,linenos
fn main() {
    println!("Line 1");
    println!("Line 2");
}
```

Configuration

This rule has no configuration options. All code blocks should have language tags for optimal mdBook rendering.

  • MD040 - Fenced code blocks should have a language specified (standard rule)
  • MD046 - Code block style
  • MD048 - Code fence style

References