跳转至

代码块以及内容选项卡

代码相关的内容。


基本用法

这里同时使用了两个内容格式:代码块内容选项卡

Content tabs with code blocks
=== "Bash"

    ``` bash
    $ printf "%s\n" "Hello world"
    ```

=== "Python"

    ``` python
    >>> print("Hello World")
    ```
$ printf "%s\n" "Hello world"
>>> print("Hello World")

展示其渲染结果,效果如上,用如下代码包裹:

<div class="result" markdown> ... </div>

使用标题、行号、高亮:

Code block with title
1
2
3
``` py title="Python"
...
```
Code block with line numbers
1
2
3
``` py linenums="1"
...
```
Code block with highlighted lines
1
2
3
``` py hl_lines="1 3"
...
```
Code block with highlighted line range
1
2
3
``` py hl_lines="1-3"
...
```

嵌入外部文件

可执行文件的源代码可以导入,后面的两个冒号紧接数字1,分别代表起始行和结束行2

单个文件导入
``` py
--8<-- "test.py::6"
--8<-- "test.py:4:"
--8<-- "test.py:4:6"
```
多个文件导入
``` py
--8<--
test.py
--8<--
```

提示

内容表格同样可以与其他内容连用,如列表元素

符号 --8<-- 可以通过添加一个 ; 的方式转义,文件也可以以同样的方式跳过,如下:

;--8<--
--8<-- "; skip.md"

  1. 两个冒号可选,如果不加冒号则导入整个文件。单个文件和多个文件的这些行为相同。 

  2. 可以省略其中一个冒号后的数字,省略前者代表从头开始,省略后者代表读到末尾。