> For the complete documentation index, see [llms.txt](https://gitbook.com/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gitbook.com/docs/documentation/zh/chuang-jian-nei-rong/blocks/code-block.md).

# 代码块

向页面添加代码块，以包含示例代码、配置、代码片段等

你可以使用代码块在你的 GitBook 页面中添加代码。

添加代码块时，您可以选择 [设置语法](#set-syntax...), [显示行号](#with-line-numbers), [显示标题](#with-caption)，并 [自动换行](#wrap-code)。您还可以轻松 [将代码块的内容复制到剪贴板](#copying-the-code)，以便在其他地方使用。

代码块可用于：

* 共享配置
* 添加代码片段
* 共享代码文件
* 展示命令行工具的使用示例
* 展示如何调用 API 端点
* 以及更多！

### 添加代码块

1. 要添加代码块，请将光标放在空行上，然后输入 `/`.
2. 在快速插入菜单中，选择 **代码块**.
3. GitBook 会插入该块并将光标置于其中，方便你粘贴或输入代码。

### 代码块示例

{% code title="index.js" overflow="wrap" lineNumbers="true" %}

```javascript
‌import * as React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

ReactDOM.render(<App />, window.document.getElementById('root'));
```

{% endcode %}

你也可以将代码块与 [选项卡块](https://gitbook.com/docs/documentation/zh/chuang-jian-nei-rong/blocks/tabs.md) 结合使用，以便用多种不同语言展示同一个代码示例：

{% tabs %}
{% tab title="JavaScript" %}

```javascript
let greeting = function (name) {
  console.log(`Hello, ${name}!`);
};
greeting("Anna");
```

{% endtab %}

{% tab title="Ruby" %}

```ruby
greeting = lambda {|name| puts "Hello, #{name}!"}
greeting.("Anna")
```

{% endtab %}

{% tab title="Elixir" %}

```elixir
greeting = fn name -> IO.puts("Hello, #{name}!") end
greeting.("Anna")
```

{% endtab %}
{% endtabs %}

### 代码块选项 <a href="#options" id="options"></a>

点击代码块旁边的 **选项菜单** <picture><source srcset="broken://files/QLUQj6waZRiK6FpSqrt6" media="(prefers-color-scheme: dark)"><img src="https://2111890564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNkEGS7hzeqa35sMXQZ4X%2Fuploads%2Fgit-blob-23f3e8f05326b0d239d32d28bba99c1d46f204f7%2F25_01_10_options_light.svg?alt=media" alt="The Options menu icon in GitBook"></picture> 或代码块本身的 **操作菜单** <picture><source srcset="broken://files/YjlF3Z9KMYv9aQiFzZKD" media="(prefers-color-scheme: dark)"><img src="https://2111890564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNkEGS7hzeqa35sMXQZ4X%2Fuploads%2Fgit-blob-aabbca522107ef561981bdfe5af94605b31e0920%2F25_01_10_actions_icon_light.svg?alt=media" alt="The Actions menu icon in GitBook"></picture> 即可看到一系列可设置的选项。

#### 设置语法… <a href="#set-syntax" id="set-syntax"></a>

你可以将代码块的语法设置为任一受支持的语言，这也会启用该语言的语法高亮。

{% hint style="info" %}
我们使用 [Prism](https://github.com/PrismJS/prism) 进行语法高亮。你可以使用 [Test Drive Prism](https://prismjs.com/test.html#language=markup) 查看 Prism 支持哪些语言。如果你发现 GitBook 与 Prism 之间存在不一致，可能是因为我们的版本落后了一两个版本。我们很快就会跟上！
{% endhint %}

{% code title="filename.txt" %}

```
// 一些代码
```

{% endcode %}

#### 显示行号 <a href="#with-line-numbers" id="with-line-numbers"></a>

这将切换代码行号的显示与隐藏。

当代码表示整个文件的内容，或者当你有包含很多行的长代码块时，显示行号会很有用。对于代码片段、命令行或终端表达式的使用说明以及类似场景，隐藏行号会更有用。

#### 显示标题

这将切换显示位于块顶部、代码行上方的标题。

说明文字通常是文件名，就像 [上面的示例](#example-of-a-code-block)一样，但你也可以将它用作标题、描述或任何你想要的内容。

#### 自动换行

这将切换代码自动换行，较长的代码行会换行显示，以便在页面上一次性全部看到。

当代码较长时，自动换行很有用，可以避免读者为了阅读代码而来回滚动。如果你开启 **自动换行代码** ，也可以考虑显示行号——这样更便于阅读代码，也更容易看出新行从哪里开始。

#### 可展开

这将切换显示完整代码（当开关关闭时）或折叠的代码窗口，用户可以将其展开（当开关开启时）。

折叠视图默认显示 10 行代码，并提供一个按钮用于展开以显示完整代码块。如果代码少于 10 行，则会显示全部内容。

### 代码块操作

除了上面的选项外，你还可以更改代码块显示的语言，并立即复制你的代码。

#### 复制代码 <a href="#copying-the-code" id="copying-the-code"></a>

将鼠标悬停在代码块上，会出现一些图标。点击中间的图标即可将代码块内容复制到剪贴板。

### Markdown 表示

````markdown
{% code title="index.js" overflow="wrap" lineNumbers="true" %}

```javascript
‌import * as React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

ReactDOM.render(<App />, window.document.getElementById('root'));
```

{% endcode %}
````


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://gitbook.com/docs/documentation/zh/chuang-jian-nei-rong/blocks/code-block.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
