diff --git a/crates/mdbook-html/front-end/js/book.js b/crates/mdbook-html/front-end/js/book.js index 62d7c4cc87..6910894938 100644 --- a/crates/mdbook-html/front-end/js/book.js +++ b/crates/mdbook-html/front-end/js/book.js @@ -755,7 +755,19 @@ aria-label="Show hidden lines">'; 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; }, }); diff --git a/guide/src/format/mdbook.md b/guide/src/format/mdbook.md index ff63eb43a9..b60f26cc9f 100644 --- a/guide/src/format/mdbook.md +++ b/guide/src/format/mdbook.md @@ -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 () 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 () which will execute the code and display the output just below the code block. diff --git a/guide/src/format/theme/syntax-highlighting.md b/guide/src/format/theme/syntax-highlighting.md index 069c568957..55c11e1ccb 100644 --- a/guide/src/format/theme/syntax-highlighting.md +++ b/guide/src/format/theme/syntax-highlighting.md @@ -24,6 +24,7 @@ your own `highlight.js` file: - bash - c - coffeescript +- console - cpp - csharp - css