# Fumadocs (Framework Mode, Waku): Sanity
URL: /ja/docs/waku/integrations/content/sanity
Source: https://raw.githubusercontent.com/fuma-nama/fumadocs/refs/heads/main/apps/docs/content/docs/(framework)/[framework]/integrations/content/sanity.ja.mdx

Sanity 向けの公式インテグレーションです。

## 概要 [#overview]

Fumadocs は、Sanity CMS のセットアップを簡単にする公式インテグレーションを提供しています。

> [Sanity のサンプル](https://github.com/fuma-nama/fumadocs-sanity)は GitHub で公開しています。

## セットアップ [#setup]

このガイドでは、[Sanity](https://sanity.io/docs/getting-started) の公式ドキュメントに従って、すでに設定が完了していることを前提としています。

### データ型 [step] [#data-types]

次のコマンドを実行して、コンテンツタイプを初期化します。

```npm
npx @fumadocs/cli add sanity/base
```

> または、`npx @fumadocs/cli add` を実行して、ほかの Sanity プリセットを一覧から選んでインストールすることもできます。

インストールした型を登録します。

```ts tab="schemaTypes/index.ts"
import { docsType, blockContent, callout, card, cards } from '@/lib/sanity/base';

// [!code ++]
export const schemaTypes = [blockContent, callout, card, cards, docsType];
```

```tsx tab="components/sanity.ts"
import {
  PortableText,
  type PortableTextProps,
  type PortableTextReactComponents,
} from '@portabletext/react';
// インストールしたコンポーネント
import { baseBlocks, baseComponents, baseMarks } from './base';

const components: Partial<PortableTextReactComponents> = {
  block: {
    // [!code ++]
    ...baseBlocks,
  },
  marks: {
    // [!code ++]
    ...baseMarks,
  },
  types: {
    // [!code ++]
    ...baseComponents,
  },
};

export function CustomPortableText(props: PortableTextProps) {
  return <PortableText {...props} components={{ ...components, ...props.components }} />;
}
```

### コンテンツローダー [step] [#content-loader]

インテグレーションをインストールします。

```npm
npm i @fumadocs/sanity
```

Sanity クライアントでソースを作成します:

```ts title="lib/source.ts"
import { dynamicLoader } from 'fumadocs-core/source';
import { createSanitySource } from '@fumadocs/sanity';
// Sanity クライアント
import { client } from '@/sanity/lib/client';
// 生成された型
import type { Docs } from '@/sanity/types';

const source = dynamicLoader(
  createSanitySource<Docs>({
    client,
    docType: 'docs',
  }),
  {
    baseUrl: '/docs',
  },
);

export async function getSource() {
  return source.get();
}
```

次に、`getSource()` 関数を使ってページをレンダリングします:

```tsx title="src/pages/docs/[...slugs].tsx"
import type { PageProps } from 'waku/router';
import { unstable_notFound } from 'waku/router/server';
import { DocsBody, DocsDescription, DocsPage, DocsTitle } from 'fumadocs-ui/layouts/docs/page';
import { getSource } from '@/lib/source';
import { CustomPortableText } from '@/components/sanity';

export default async function Page({ slugs }: PageProps<'/docs/[...slugs]'>) {
  const source = await getSource();
  const page = source.getPage(slugs);
  if (!page) unstable_notFound();

  const { renderToc, body } = await page.data.load();

  return (
    <DocsPage toc={renderToc({ render: (block) => <CustomPortableText value={block} /> })}>
      <DocsTitle>{page.data.title}</DocsTitle>
      <DocsDescription>{page.data.description}</DocsDescription>
      <DocsBody>
        <CustomPortableText value={body!} />
      </DocsBody>
    </DocsPage>
  );
}

export async function getConfig() {
  const source = await getSource();
  return {
    render: 'static',
    staticPaths: source.generateParams().map((item) => item.slug),
  } as const;
}
```

### 完了 [step] [#done]

Studio エディターで `docs` タイプのドキュメントを作成すると、docs ルートに表示されるようになります。