1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// This file is part of helpers4.
// Copyright (C) 2025 baxyz
// SPDX-License-Identifier: LGPL-3.0-or-later
/// A fenced code block for `code`, tagged with `language`.
///
/// The fence is at least three backticks and one longer than the longest run of backticks in the
/// code, so code that itself contains a fence stays inside the block. The result ends with a
/// newline and the code keeps its own line breaks (a single trailing newline is not doubled).
/// Whitespace and backticks are removed from `language`, which must be a single word (`rust`,
/// `sh`, `json`, or empty for none).
///
/// # Arguments
///
/// - `code` - The code to show.
/// - `language` - The language tag for syntax highlighting, or `""`.
///
/// # Returns
///
/// The fenced block, ending with a newline.
///
/// # Examples
///
/// ```
/// use helpers4::markdown::code_block;
///
/// assert_eq!(code_block("let x = 1;", "rust"), "```rust\nlet x = 1;\n```\n");
/// assert_eq!(code_block("```\nnested\n```", ""), "````\n```\nnested\n```\n````\n");
/// ```