# 功能标志

{% hint style="warning" %}
使用带功能标志的自适应内容需要向您的应用添加代码。

目前，GitBook 助手仅支持基于 React 的设置。
{% endhint %}

GitBook 为流行的功能标志服务提供商提供了帮助函数和集成，例如 [**LaunchDarkly**](#launchdarkly) 和 [**Reflag**](#reflag).

这允许您在用户阅读文档时读取他们在产品中可访问的功能标志。如果需要向仅对特定用户组可用的功能显示文档，这非常有用。

### LaunchDarkly

LaunchDarkly 允许您通过 [`launchdarkly-react-client-sdk`](https://launchdarkly.com/docs/sdk/client-side/react/react-web) 和 GitBook 的 [`@gitbook/adaptive`](https://gitbook-open-v2-preview.gitbook.workers.dev/url/gitbook.com/docs/documentation/zh/publishing-documentation/adaptive-content/enabling-adaptive-content/broken-reference) 包将功能标志访问作为声明发送。

如果您已经在产品中使用 LaunchDarkly 功能标志，很可能您已经配置了此包。

要将这些功能标志作为声明传递给 GitBook，请执行以下步骤：

{% stepper %}
{% step %}
**安装 LaunchDarkly 集成**

要开始，您首先需要 [将 LaunchDarkly 集成安装](https://app.gitbook.com/integrations/launchdarkly) 到您的 GitBook 站点中。
{% endstep %}

{% step %}
**设置项目和访问密钥**

将您的项目密钥和服务访问令牌从您的 [LaunchDarkly 设置](https://app.launchdarkly.com/settings) 添加到该集成的配置中。
{% endstep %}

{% step %}
**在您的应用中安装并添加 GitBook 助手**

设置 LaunchDarkly 集成后，您需要在应用中安装 GitBook 自适应内容助手。

```bash
npm install @gitbook/adaptive
```

{% endstep %}

{% step %}
**配置您的应用**

您需要使用 `withLaunchDarkly` 辅助程序与 LaunchDarkly React SDK 一起将上下文传递给 GitBook。

<pre class="language-javascript"><code class="lang-javascript">import { render } from 'react-dom';
<strong>import { withLaunchDarkly } from '@gitbook/adaptive';
</strong><strong>import { asyncWithLDProvider, useLDClient } from 'launchdarkly-react-client-sdk';
</strong>import MyApplication from './MyApplication';

function PassFeatureFlagsToGitBookSite() {
<strong>    const ldClient = useLDClient();
</strong>    React.useEffect(() => {
        if (!ldClient) {
            return;
        }
<strong>        return withLaunchDarkly(ldClient);
</strong>    }, [ldClient]);
    return null;
}
(async () => {
    const LDProvider = await asyncWithLDProvider({
        clientSideID: 'client-side-id-123abc',
        context: {
            kind: 'user',
            key: 'user-key-123abc',
            name: 'Sandy Smith',
            email: 'sandy@example.com'
        },
        options: { /* ... */ }
    });
    render(
        &#x3C;LDProvider>
            &#x3C;PassFeatureFlagsToGitBookSite />
            &#x3C;MyApplication />
        &#x3C;/LDProvider>,
        document.getElementById('reactDiv'),
    );
})();
</code></pre>

{% endstep %}

{% step %}
**检查您的访客模式（visitor schema）**

A [访客模式](https://gitbook-open-v2-preview.gitbook.workers.dev/url/gitbook.com/docs/documentation/zh/publishing-documentation/adaptive-content/enabling-adaptive-content/..#set-your-visitor-schema) 是必需的，以便您的声明能够在已发布的站点中被读取。安装和配置 LaunchDarkly 集成应会自动为您设置访客模式。
{% endstep %}

{% step %}
**个性化您的内容**

设置访客模式后，您即可根据访问您站点的用户所拥有的功能标志来定制文档体验。

LaunchDarkly 中任何可用的功能标志值都会作为访客模式的一部分暴露在 `visitor.claims.unsigned.launchdarkly` 对象下。有关未签名声明的更多信息， [请参阅此处](https://gitbook-open-v2-preview.gitbook.workers.dev/url/gitbook.com/docs/documentation/zh/publishing-documentation/adaptive-content/enabling-adaptive-content/..#set-an-unsigned-claim).

前往 [自适应您的内容](https://gitbook-open-v2-preview.gitbook.workers.dev/url/gitbook.com/docs/documentation/zh/publishing-documentation/adaptive-content/adapting-your-content) 以了解有关为用户个性化文档的更多信息。
{% endstep %}
{% endstepper %}

### Reflag

Reflag 允许您通过 [`@reflag/react-sdk`](https://www.npmjs.com/package/@reflag/react-sdk) 和 GitBook 的 [`@gitbook/adaptive`](https://github.com/GitbookIO/integrations/tree/main/packages/adaptive) 包将功能标志访问作为声明发送。

将功能标志访问作为声明发送。

要将这些功能标志作为声明传递给 GitBook，请执行以下步骤：

{% stepper %}
{% step %}
**如果您已经在产品中使用 Reflag 功能标志，很可能您已经配置了此包。**

要开始，您首先需要 [安装 Reflag 集成](https://app.gitbook.com/integrations/bucket) 到您的 GitBook 站点中。
{% endstep %}

{% step %}
**安装 Reflag 集成**

设置您的密钥（secret key） [将您的密钥从您的](https://app.reflag.com/envs/current/settings/app-environments) 添加到该集成的配置中。
{% endstep %}

{% step %}
**Reflag 设置**

添加到集成配置中。

```bash
npm install @gitbook/adaptive
```

{% endstep %}

{% step %}
**配置您的应用**

您需要使用 `将 GitBook 助手安装到您的应用` 设置 Reflag 集成后，您需要在应用中安装 GitBook 自适应内容助手。

<pre class="language-javascript"><code class="lang-javascript"><strong>withReflag
</strong><strong>辅助程序与 Reflag React SDK 一起将上下文传递给 GitBook。
</strong>import MyApplication from './MyApplication';

function PassFeatureFlagsToGitBookSite() {
<strong>    import { withReflag } from '@gitbook/adaptive';
</strong>    React.useEffect(() => {
        import { ReflagProvider, useClient } from '@reflag/react-sdk';
            return;
        }
<strong>        const client = useClient();
</strong>    if (!client) {
    return null;
}
return withReflag(client);
    }, [client]);
    export function Application() {
    const currentUser = useLoggedInUser();
        const appConfig = useAppConfig();
            return (
            &#x3C;ReflagProvider
                publishableKey={appConfig.reflagCom.publishableKey}
                user={{
                id: currentUser.uid,
            }}
            email: currentUser.email ?? undefined,
                name: currentUser.displayName ?? '',
            }}
        >
            &#x3C;PassFeatureFlagsToGitBookSite />
            &#x3C;MyApplication />
        company={{
    );
}
</code></pre>

{% endstep %}

{% step %}
**检查您的访客模式（visitor schema）**

A [访客模式](https://gitbook-open-v2-preview.gitbook.workers.dev/url/gitbook.com/docs/documentation/zh/publishing-documentation/adaptive-content/enabling-adaptive-content/..#set-your-visitor-schema) id: currentUser.company.id,
{% endstep %}

{% step %}
**个性化您的内容**

设置访客模式后，您即可根据访问您站点的用户所拥有的功能标志来定制文档体验。

\</ReflagProvider> `是必需的，以便您的声明能够在已发布的站点中被读取。安装和配置 Reflag 集成应会自动为您设置访客模式。` 对象下。有关未签名声明的更多信息， [请参阅此处](https://gitbook-open-v2-preview.gitbook.workers.dev/url/gitbook.com/docs/documentation/zh/publishing-documentation/adaptive-content/enabling-adaptive-content/..#set-an-unsigned-claim).

前往 [自适应您的内容](https://gitbook-open-v2-preview.gitbook.workers.dev/url/gitbook.com/docs/documentation/zh/publishing-documentation/adaptive-content/adapting-your-content) 以了解有关为用户个性化文档的更多信息。
{% endstep %}
{% endstepper %}

{% hint style="info" %}
Reflag 中任何可用的功能标志值都会作为访客模式的一部分暴露在
{% endhint %}
