Notion
Fumadocs を使って Notion データベースのページを公開します。
@fumadocs/notion は、Notion データベースのページをコンテンツソースとして読み込み、各ページのブロックを Fumadocs UI でサーバー側でレンダリングします。
セットアップ
npm install @fumadocs/notion @notionhq/clientFumadocs UI のスタイルの後に、そのスタイルを追加してください。
@import 'fumadocs-ui/css/neutral.css';
@import 'fumadocs-ui/css/preset.css';
@import '@fumadocs/notion/css/preset.css';Notion データベース
Notion インテグレーションを作成し、ページを格納しているデータベースに接続します。データベースの各行が1つのページに対応し、以下のプロパティを持ちます。
- タイトルプロパティはページのタイトルです。
Slugは URL パスです。ネストされたページであればguides/installのように指定します。指定しない場合、スラッグはタイトルから生成されます。Descriptionはページの説明です。
別のプロパティ名を使用する場合は、dynamicSource() の properties オプションを設定してください。
インテグレーショントークンとデータベースのデータソース ID を環境変数に追加します。
NOTION_TOKEN=ntn_...
# データベース URL 内のデータベース ID ではない点に注意してください
NOTION_DATA_SOURCE_ID=...コンテンツソース
ソースを作成し、getSource() からローダーを取得します。
import { Client } from '@notionhq/client';
import { dynamicLoader } from 'fumadocs-core/source';
import { createNotion } from '@fumadocs/notion/source';
export const notion = createNotion({
client: new Client({ auth: process.env.NOTION_TOKEN }),
dataSourceId: process.env.NOTION_DATA_SOURCE_ID!,
});
const source = dynamicLoader(
notion.dynamicSource({
// `Published` にチェックが入っている行のみを公開します
query: {
filter: { property: 'Published', checkbox: { equals: true } },
},
}),
{ baseUrl: '/docs' },
);
export async function getSource() {
return source.get();
}ページのレンダリング
page.data.load() はページのブロックを取得します。取得したブロックは NotionRenderer でレンダリングしてください:
import type { ReactNode } from 'react';
import { DocsLayout } from 'fumadocs-ui/layouts/docs';
import { getSource } from '@/lib/source';
export default async function Layout({ children }: { children: ReactNode }) {
const source = await getSource();
return <DocsLayout tree={source.getPageTree()}>{children}</DocsLayout>;
}ファイルルート
画像や PDF など、Notion 上でホストされているファイルの URL は約 1 時間で有効期限が切れます。createNotionFileUrlResolver() は、これらのファイルの URL を、新しい URL へリダイレクトするルートに置き換えます。
import { createNotionFileHandler } from '@fumadocs/notion';
import { notion } from '@/lib/source';
export const GET = createNotionFileHandler(notion);ルートを別のパスにマウントする場合は、同じパスをリゾルバーにも渡してください。
createNotionFileUrlResolver({ pageId: page.data.id, path: '/notion/file' });再検証
ローダーはページをメモリ上に保持します。Notion で編集した後は、Webhook などからローダーを再検証してください。
export function revalidateNotion() {
return source.revalidate();
}プレビューなどでリクエストごとに最新のコンテンツを取得するには、読み込む前にキャッシュを無効化します。
export async function getSource() {
source.invalidate();
return source.get();
}ブロックのカスタマイズ
ブロックタイプのレンダリングをオーバーライドするには、components を渡します。
<NotionRenderer
blocks={blocks}
components={{
callout({ block, children, renderRichText }) {
return (
<aside className="rounded-lg bg-fd-muted p-4">
{renderRichText(block.callout.rich_text)}
{children}
</aside>
);
},
}}
// デフォルトは Shiki。ハイライトなしのプレーンなコードにするには `false`、または `(code, language) => ReactNode` を指定します
highlightCode={false}
/>制限事項
- サイト上のページになるのはデータベースの行だけであるため、サブページやその他の Notion ページへのリンクは Notion 上のページを指します。
- ファイルルートにはサーバーが必要です。静的エクスポートする場合は、ファイルを Notion 以外の場所でホストし、
getFileUrlは指定しないでください。
How is this guide?
最終更新日
