> 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/publish/adaptive-content/enabling-adaptive-content.md). Agents can also search and read this documentation through its [MCP server](https://gitbook.com/docs/~gitbook/mcp).

# 启用自适应内容

选择一种身份验证方法将用户数据传递给 GitBook。

要开始为你的读者自定义文档体验，你需要启用自适应内容，并决定如何将访客数据传递给 GitBook。这样你的网站内容就能根据查看者动态适配。

### 启用自适应内容

在你能够将用户数据传递给 GitBook 之前，你需要将你的网站配置为使用自适应内容。

前往你的 [站点设置](https://gitbook.com/docs/documentation/zh/manage-your-site/site-settings.md)，并在 **自适应内容** 站点受众设置中启用该功能。启用后，系统会生成一个“访客令牌签名密钥”，你需要此密钥才能继续配置自适应内容。

<figure><img src="https://2111890564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNkEGS7hzeqa35sMXQZ4X%2Fuploads%2Fgit-blob-96a4a97549012fd729ecfe3578922177aa40fa89%2F26_01_06_enable_adaptive_content%402x.png?alt=media" alt="A GitBook screenshot showing the enable adaptive content toggle"><figcaption><p>在你网站的设置中启用自适应内容</p></figcaption></figure>

### 设置你的访客模式

在启用自适应内容后，你需要为你期望 GitBook 在用户访问你的网站时接收的声明类型定义一个模式。

访客模式应反映这些声明在发送到 GitBook 时的结构。

例如，如果你预期访问者在你的产品中可能是测试版用户，你可以设置如下类似的访客模式：

```json
{
  "type": "object",
  "properties": {
    "isBetaUser": {
      "type": "boolean",
      "description": "访问者是否为测试版用户。"
    }
  },
  "additionalProperties": false
}
```

这也有助于你在 [条件编辑器](https://gitbook.com/docs/documentation/zh/publish/adaptive-content/adapting-your-content.md#working-with-the-condition-editor)中配置声明时使用自动补全。访客架构仅支持以下类型：

{% tabs %}
{% tab title="字符串" %}
以字符串形式读取传入的声明。

GitBook 接受动态字符串，这意味着你可以动态传递字符串数据——例如用户姓名、开发者令牌等更多内容。

字符串也可以包含一个 **可选的枚举** 键，从而将 GitBook 接收的数据限制为其预定义值之一。

```json
{
  "type": "object",
  "properties": {
    "language": {
          "type": "string",
          "description": "访客的语言",
          // 可选的 enum 属性
          "enum": [
            "en",
            "fr",
            "it"
          ]
  },
  "additionalProperties": false
}
```

{% hint style="warning" %}
动态字符串（未定义枚举键的字符串）仅可用于 [内联表达式](https://gitbook.com/docs/documentation/zh/chuang-jian-nei-rong/variables-and-expressions.md#use-variables-in-your-content)。用于控制元素（页面、分区、内容块）可见性的条件表达式，仅适用于定义了枚举键的字符串。
{% endhint %}
{% endtab %}

{% tab title="布尔值" %}
以布尔值形式读取传入的声明。

```json
{
  "type": "object",
  "properties": {
    "isBetaUser": {
      "type": "boolean",
      "description": "访问者是否为测试版用户。"
    },
  },
  "additionalProperties": false
}
```

{% endtab %}

{% tab title="对象" %}
将声明嵌套在对象中以对相似值进行分组。

```json
{
  // 顶层声明
  "type": "object",
  "properties": {
    // 嵌套声明
    "access": {
      "type": "object",
      "description": "用户对产品功能的访问权限",
      "properties": {
        "isAlphaUser": {
          "type": "boolean",
          "description": "访问者是否为 Alpha 用户。"
        },
        "isBetaUser": {
          "type": "boolean",
          "description": "访问者是否为测试版用户。"
        },
      },
      "additionalProperties": false
    }
  },
  "additionalProperties": false
}
```

{% endtab %}
{% endtabs %}

### 设置未签名声明

未签名声明是一种特殊类型的声明，用于标识可能未经客户端应用签名的传入声明。如果你通过 URL 参数、未签名 Cookie 和功能标志传递声明，则必须在访客架构中将这些声明设置为 `unsigned` 。

如果你打算处理未签名声明，则需要在模式中将你预期的声明作为“unsigned”属性，与已签名声明并列声明。

```json
{
  "type": "object",
  "properties": {
    "isBetaUser": {
      "type": "boolean",
      "description": "访问者是否为测试版用户。"
    },
    // 添加未签名声明
    "unsigned": {
      "type": "object",
      "description": "网站访客的未签名声明。",
      "properties": {
        "language": {
          "type": "string",
          "description": "访客的语言",
          // 可选的 enum 属性
          "enum": [
            "en",
            "fr",
            "it"
          ]
        }
      },
      "additionalProperties": false
    }
  },
  "additionalProperties": false
}
```

### 通过 Content API 使用访客声明

当服务器需要针对特定访客评估已发布内容时，请使用 Content API。

在架构中定义访客声明，然后使用以下密钥为访客 JSON Web Token (JWT) 签名： **访客令牌签名密钥** （位于受众设置中）。只有在架构中声明的声明才能用于控制自适应内容条件。

受信任的服务器将签名后的访客 JWT 和目标已发布 URL 交换为短期有效的 Content API 令牌。使用返回的 `Bearer` 令牌，以该访客的上下文搜索内容、列出页面并检索页面。

实现此流程之前，请先了解内容检索的行为：

* 单页请求中使用 `evaluated=true` 会针对访客解析条件内容块和内联表达式。否则，API 将返回未经处理的创作内容。
* 搜索结果和页面列表会省略访客无权查看的内容。直接请求无权访问的页面时，会返回 `404`.
* 如果访客 JWT 签名无效，GitBook 会将其视为匿名访客来评估内容，不会信任所提供的声明。

{% hint style="warning" %}
仅在受信任的服务器上创建并交换签名后的访客 JWT。切勿在浏览器代码或日志中暴露访客令牌签名密钥或 Content API 令牌。
{% endhint %}

### 将访客数据传递给 GitBook

GitBook 提供了不同方式来传递访客数据，以适配你网站的内容。在定义模式后，你需要决定如何将访客数据传递给 GitBook。

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th><th></th><th data-hidden data-card-target data-type="content-ref"></th></tr></thead><tbody><tr><td><i class="fa-cookie">:cookie:</i></td><td><strong>Cookie</strong></td><td>通过公共或签名 cookie 将访客数据传递到你的文档中。</td><td><a href="https://gitbook.com/docs/documentation/zh/publish/adaptive-content/enabling-adaptive-content/cookies.md">Cookie</a></td></tr><tr><td><i class="fa-link">:link:</i></td><td><strong>URL</strong></td><td>通过 URL 查询参数将访客数据传递到你的文档中。</td><td><a href="https://gitbook.com/docs/documentation/zh/publish/adaptive-content/enabling-adaptive-content/url.md">URL</a></td></tr><tr><td><i class="fa-flag">:flag:</i></td><td><strong>功能标志</strong></td><td>通过功能标志提供商将访客数据传递到你的文档中。</td><td><a href="https://gitbook.com/docs/documentation/zh/publish/adaptive-content/enabling-adaptive-content/feature-flags.md">功能标志</a></td></tr><tr><td><i class="fa-lock">:lock:</i></td><td><strong>已认证访问</strong></td><td>通过身份验证提供商将访客数据传递到你的文档中。</td><td><a href="https://gitbook.com/docs/documentation/zh/publish/adaptive-content/enabling-adaptive-content/authenticated-access.md">已认证访问</a></td></tr></tbody></table>


---

# 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/publish/adaptive-content/enabling-adaptive-content.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.
