Fumadocs rebuilt on Hamur, the original is fumadocs.dev
跳至內容
Fumadocs

Sanity

Sanity 官方整合。

概覽

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

您可以在 GitHub 上查看我們的 Sanity 範例。

設定

本指南假設您已依照 Sanity 官方文件完成設定。

資料型別

請執行以下指令來初始化內容型別。

npx @fumadocs/cli add sanity/base

您也可以執行 npx @fumadocs/cli add,瀏覽並安裝其他 Sanity 預設組合。

註冊已安裝的類型:

import { docsType, blockContent, callout, card, cards } from '@/lib/sanity/base';

export const schemaTypes = [blockContent, callout, card, cards, docsType];

內容載入器

安裝此整合:

npm i @fumadocs/sanity

使用 Sanity 用戶端建立來源:

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() 函式算繪頁面:

---
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>

完成

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

How is this guide?

最後更新於