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

Sanity 官方整合。

## 概覽 [#overview]

Fumadocs 提供官方整合，可簡化 Sanity CMS 的設定流程。

> 您可以在 GitHub 上查看我們的 [Sanity 範例](https://github.com/fuma-nama/fumadocs-sanity)。

## 設定 [#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()` 函式算繪頁面：

```astro tab="src/pages/docs/[...slug].astro"
---
import type { GetStaticPaths, InferGetStaticPropsType } from 'astro';
import Layout from '@/layouts/Layout.astro';
import { Docs } from '@/components/docs';
import { getSource } from '@/lib/source';

export const prerender = true;

export const getStaticPaths = (async () => {
  const source = await getSource();
  return source.getPages().map((page) => ({
    params: { slug: decodeURI(page.slugs.join('/')) || undefined },
    props: { page },
  }));
}) satisfies GetStaticPaths;

type Props = InferGetStaticPropsType<typeof getStaticPaths>;

const { page } = Astro.props;
const source = await getSource();
const pageTree = await source.serializePageTree(source.getPageTree());
const { title, description, _toc, body } = await page.data.load();
---

<Layout title={page.data.title}>
  <Docs
    client:load
    pathname={Astro.url.pathname}
    params={Astro.params}
    tree={pageTree}
    title={title}
    description={description}
    toc={_toc}
    body={body}
  />
</Layout>
```

```tsx tab="src/components/docs.tsx"
import { type SerializedPageTree, useFumadocsLoader } from 'fumadocs-core/source/client';
import { DocsLayout } from 'fumadocs-ui/layouts/docs';
import { DocsBody, DocsDescription, DocsPage, DocsTitle } from 'fumadocs-ui/layouts/docs/page';
import { renderToc } from '@fumadocs/sanity/client';
import type { PortableTextBlock, PortableTextProps } from '@portabletext/react';
import { Provider, type ProviderProps } from '@/components/provider';
import { CustomPortableText } from '@/components/sanity';

export interface DocsProps extends ProviderProps {
  tree: SerializedPageTree;
  title?: string;
  description?: string;
  toc: PortableTextBlock[];
  body?: PortableTextProps['value'];
}

export function Docs({ tree, title, description, toc, body, ...props }: DocsProps) {
  const data = useFumadocsLoader({ tree });

  return (
    <Provider {...props}>
      <DocsLayout tree={data.tree}>
        <DocsPage toc={renderToc({ toc, render: (block) => <CustomPortableText value={block} /> })}>
          <DocsTitle>{title}</DocsTitle>
          <DocsDescription>{description}</DocsDescription>
          <DocsBody>
            <CustomPortableText value={body!} />
          </DocsBody>
        </DocsPage>
      </DocsLayout>
    </Provider>
  );
}
```

### 完成 [step] [#done]

在 Studio 編輯器中建立 `docs` 類型的文件後，這些文件就會出現在 docs 路由中。