# 启用自适应内容

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

### 启用自适应内容

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

前往您的 [站点设置](https://gitbook-v2-q67etdj25-gitbook.vercel.app/url/gitbook.com/docs/documentation/zh/publishing-documentation/site-settings)，并启用 **自适应内容** 来自你网站的受众设置。一旦启用，你将获得一个生成的“访客令牌签名密钥”，继续进行自适应内容设置时需要用到它。

<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": "访客是否为测试版用户。"
    }
  },
  "additionalProperties": false
}
```

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

{% tabs %}
{% tab title="字符串" %}
读取作为字符串传入的声明。

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

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

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

{% hint style="warning" %}
动态字符串（未使用 enum 键定义的字符串）仅接受用于 [行内表达式](https://gitbook-v2-q67etdj25-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": "访客是否为测试版用户。"
    },
  },
  "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 和功能标志传递声明，则必须在你的访客 schema 中将声明设置为

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

```json
{
  "type": "object",
  "properties": {
    "isBetaUser": {
      "type": "boolean",
      "description": "访客是否为测试版用户。"
    },
    // 添加未签名声明
    "unsigned": {
      "type": "object",
      "description": "网站访客的未签名声明。",
      "properties": {
        "language": {
          "type": "string",
          "description": "访客的语言",
          // 可选的枚举属性
          "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>
