Fumadocs rebuilt on Hamur, the original is fumadocs.dev
コンテンツへスキップ
Fumadocs

Python

Python パッケージから API リファレンスを生成します。

Python のサポートは実験的な機能です。

fumadocs-python は、Python パッケージのソースコードと docstring からドキュメントを生成し、モジュールとクラスごとにページを作成します。Python コマンドでパッケージの情報を JSON ファイルに出力し、それをコンテンツソースとして読み込むか、MDX ファイルに変換して利用します。

セットアップ

npm install fumadocs-python shiki

Python コマンドで JSON ファイルを生成します。

# パッケージの Python 環境にインストールします
pip install ./node_modules/fumadocs-python

# httpx.json を書き出します。`--dir` で出力ディレクトリを変更できます
fumapy-generate httpx

# Google スタイルではなく、NumPy または Sphinx スタイルで docstring のセクションを解析します
fumapy-generate httpx --docstring-style numpy

コンポーネントのスタイルを追加します:

Tailwind CSS
@import 'tailwindcss';
@import 'fumadocs-ui/css/neutral.css';
@import 'fumadocs-ui/css/preset.css';
@import 'fumadocs-python/preset.css';

コンテンツソース

JSON ファイルからソースを作成し、loader() に渡します。

lib/source.ts
import { loader } from 'fumadocs-core/source';
import { createPython } from 'fumadocs-python';

const python = createPython({
  file: './httpx.json',
  // `httpx._client.Client` は `/docs/httpx/_client/Client` で配信され、
  // `none` にすると `/docs/_client/Client` で配信されます
  groupBy: 'module',
});

export const source = loader(await python.staticSource(), {
  baseUrl: '/docs',
  // サイドバーのページに `module` または `class` のバッジを付けます
  plugins: [python.loaderPlugin()],
});

ページをコンパイルしてレンダリングします。

src/pages/docs/[...slugs].tsx
import type { PageProps } from 'waku/router';
import { unstable_notFound } from 'waku/router/server';
import { DocsBody, DocsPage, DocsTitle } from 'fumadocs-ui/layouts/docs/page';
import { source } from '@/lib/source';

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

  const data = await page.data.load();
  // MDX コンポーネントを渡すと、デフォルトのコンポーネントを上書きできます
  const { body, toc } = await data.render();

  return (
    <DocsPage toc={toc}>
      <DocsTitle>{page.data.title}</DocsTitle>
      <DocsBody>{body}</DocsBody>
    </DocsPage>
  );
}

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

コンパイラオプション

docstring は Markdown なので、remark や rehype のプラグインを適用できます:

lib/source.ts
import { createPython } from 'fumadocs-python';
import remarkMath from 'remark-math';
import rehypeKatex from 'rehype-katex';

const python = createPython({
  file: './httpx.json',
  // ソースコードや docstring の例など、コードブロック用の Shiki オプション
  rehypeCodeOptions: {
    themes: {
      light: 'github-light',
      dark: 'github-dark',
    },
  },
  remarkPlugins: [remarkMath],
  rehypePlugins: [rehypeKatex],
});

動的ソース

staticSource() は JSON ファイルを一度だけ読み込みます。サーバーを再起動せずに、再生成されたファイルを読み込むには、dynamicSource() と dynamicLoader() を組み合わせて使用します。

lib/source.ts
import { dynamicLoader } from 'fumadocs-core/source';

const pythonLoader = dynamicLoader(python.dynamicSource(), {
  baseUrl: '/docs',
  plugins: [python.loaderPlugin()],
});

export function getSource() {
  return pythonLoader.get();
}

source の代わりに await getSource() を使用してください。pythonLoader.invalidate() を呼び出した後は、次の getSource() の呼び出し時にファイルが再度読み込まれます。

MDX ファイル

ページを他のコンテンツと同じ場所で管理するには、スクリプトで JSON を MDX ファイルに変換します。ページ間のリンクは baseUrl から始まるため、baseUrl には出力ディレクトリの URL を設定してください。

import * as fs from 'node:fs/promises';
import * as Python from 'fumadocs-python';

const content = JSON.parse(await fs.readFile('./httpx.json', 'utf-8'));
// 削除済みモジュールのページを取り除く
await fs.rm('content/docs/httpx', { recursive: true, force: true });

await Python.write(Python.convert(content, { baseUrl: '/docs' }), 'content/docs');

How is this guide?

最終更新日