Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion crates/mdbook-html/front-end/js/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,19 @@ aria-label="Show hidden lines"></button>';
text: function(trigger) {
hideTooltip(trigger);
const playground = trigger.closest('pre');
return playground_text(playground, false);
let text = playground_text(playground, false);

// Often console code blocks begin with a dollar or hash prompt and have
// example output. Copying and pasting this into a terminal will fail.
// Instead, strip the prompts and skip the output lines.
const codeBlock = playground.querySelector('code');
if (codeBlock && codeBlock.classList.contains('language-console')) {
text = text.split('\n')
.filter(line => line.startsWith('$ ') || line.startsWith('# '))
.map(line => line.substring(2))
.join('\n');
}
return text;
},
});

Expand Down
45 changes: 45 additions & 0 deletions guide/src/format/mdbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,51 @@ nothidden():
```
~~~

## Console code blocks

Console code blocks (annotated with `console`) provide special copy behavior to make it easier to copy commands into your terminal.
Console examples often include shell prompts (`$ ` or `# `) followed by commands, along with the output from running those commands.

When you click the copy button (<i class="fa fa-copy"></i>) on a console code block, mdBook will:

- Only copy lines that begin with `$ ` or `# ` (the actual commands)
- Strip the prompt prefix from those lines
- Skip lines without prompts (command output)

This allows you to paste commands directly into your terminal without manually removing prompts or cleaning up output. Example:

~~~markdown
```console
$ echo "Hello, World!"
Hello, World!
$ ls -la
total 64
drwxr-xr-x 5 user staff 160 Jan 1 12:00 .
```
~~~

The code block above will render with both commands and output visible:

```console
$ echo "Hello, World!"
Hello, World!
$ ls -la
total 64
drwxr-xr-x 5 user staff 160 Jan 1 12:00 .
```

However, when copied, it will only include the commands without the prompts:

```text
echo "Hello, World!"
ls -la
```

This makes it easy to copy and paste multi-line commands directly into your terminal.

See also the other supported languages with
[syntax highlighting](/format/theme/syntax-highlighting.html).

## Rust playground

Rust language code blocks will automatically get a play button (<i class="fas fa-play"></i>) which will execute the code and display the output just below the code block.
Expand Down
1 change: 1 addition & 0 deletions guide/src/format/theme/syntax-highlighting.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ your own `highlight.js` file:
- bash
- c
- coffeescript
- console
- cpp
- csharp
- css
Expand Down