> 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/ja-gitbook-documentation/kontentsuwosuru/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/ja-gitbook-documentation/kontentsuwosuru/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://4217681718-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://4217681718-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で違いが見られる場合、こちらのバージョンが1、2世代遅れている可能性があります。まもなく追いつく予定です。
{% endhint %}

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

```
// Some code
```

{% 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/ja-gitbook-documentation/kontentsuwosuru/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.
