# 启用自适应内容

要开始为读者自定义文档体验，你需要启用自适应内容，并决定访客数据如何传递到 GitBook。这样你的网站内容就可以根据访问者动态调整。

### 启用自适应内容

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

前往你的 [站点设置](https://gitbook-v2-5hpihs24d-gitbook.vercel.app/url/gitbook.com/docs/documentation/zh/docs-site/site-settings)，并启用 **自适应内容** ，在你网站的受众设置中。启用后，你会得到一个生成的“Visitor token signing key（访客令牌签名密钥）”，你需要它才能继续进行自适应内容设置。

<figure><img src="https://2111890564-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNkEGS7hzeqa35sMXQZ4X%2Fuploads%2F5EeWAo5Ij6CKrp69uMl5%2F26_01_06_enable_adaptive_content%402x.png?alt=media&#x26;token=4a1e8558-4b91-4f8b-8581-63d570a2c330" alt="A GitBook screenshot showing the enable adaptive content toggle"><figcaption><p>在你网站的设置中启用自适应内容</p></figcaption></figure>

### 设置访客 schema

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

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

例如，如果你预计访客可能是你产品中的测试版用户，你可以设置一个类似如下的访客 schema：

```json
{
  "type": "object",
  "properties": {
    "isBetaUser": {
      "type": "boolean",
      "description": "访客是否为 Beta 用户。"
    }
  },
  "additionalProperties": false
}
```

这也将帮助你在配置以下内容中的声明时使用自动补全 [条件编辑器](https://gitbook-v2-5hpihs24d-gitbook.vercel.app/url/gitbook.com/docs/documentation/zh/zhan-dian-fang-wen/adapting-your-content#working-with-the-condition-editor)。访客 schema 仅支持以下类型：

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

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

字符串还可以包含一个 **可选的 enum** 键，它允许你将 GitBook 接收的数据限制为其设定值之一。

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

{% hint style="warning" %}
动态字符串（未使用 enum 键定义的字符串）仅被接受用于 [行内表达式](https://gitbook-v2-5hpihs24d-gitbook.vercel.app/url/gitbook.com/docs/documentation/zh/creating-content/variables-and-expressions#use-variables-in-your-content)。元素（页面、章节、区块）可见性的条件表达式仅适用于使用 enum 键定义的字符串。
{% endhint %}
{% endtab %}

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

```json
{
  "type": "object",
  "properties": {
    "isBetaUser": {
      "type": "boolean",
      "description": "访客是否为 Beta 用户。"
    },
  },
  "additionalProperties": false
}
```

{% endtab %}

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

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

{% endtab %}
{% endtabs %}

### 设置未签名声明

未签名声明是一种特定类型的声明，用于识别可能未由客户端应用签名传入的声明。如果你通过 URL 参数、未签名 cookie 和功能标志传递声明，就必须在访客 schema 中将声明设置为 `未签名的` if you are passing claims through URL parameters, unsigned cookies, and feature flags.

如果你打算使用未签名声明，你需要在 schema 中与已签名声明并列，在一个“unsigned”属性下声明你预期接收的声明。

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

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

GitBook 提供了多种方式将访客数据传递进去，以便调整你网站的内容。定义好 schema 后，你需要决定希望如何将访客数据传递给 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="enabling-adaptive-content/cookies">cookies</a></td></tr><tr><td><i class="fa-link">:link:</i></td><td><strong>URL</strong></td><td>通过 URL 查询参数将访客数据传递到你的文档中。</td><td><a href="enabling-adaptive-content/url">url</a></td></tr><tr><td><i class="fa-flag">:flag:</i></td><td><strong>功能标志</strong></td><td>通过功能标志提供方将访客数据传递到你的文档中。</td><td><a href="enabling-adaptive-content/feature-flags">feature-flags</a></td></tr><tr><td><i class="fa-lock">:lock:</i></td><td><strong>身份验证访问</strong></td><td>通过身份验证提供方将访客数据传递到你的文档中。</td><td><a href="enabling-adaptive-content/authenticated-access">authenticated-access</a></td></tr></tbody></table>
