Python
Python パッケージから API リファレンスを生成します。
Python のサポートは実験的な機能です。
fumadocs-python は、Python パッケージのソースコードと docstring からドキュメントを生成し、モジュールとクラスごとにページを作成します。Python コマンドでパッケージの情報を JSON ファイルに出力し、それをコンテンツソースとして読み込むか、MDX ファイルに変換して利用します。
セットアップ
npm install fumadocs-python shikiPython コマンドで JSON ファイルを生成します。
# パッケージの Python 環境にインストールします
pip install ./node_modules/fumadocs-python
# httpx.json を書き出します。`--dir` で出力ディレクトリを変更できます
fumapy-generate httpx
# Google スタイルではなく、NumPy または Sphinx スタイルで docstring のセクションを解析します
fumapy-generate httpx --docstring-style numpyコンポーネントのスタイルを追加します:
@import 'tailwindcss';
@import 'fumadocs-ui/css/neutral.css';
@import 'fumadocs-ui/css/preset.css';
@import 'fumadocs-python/preset.css';コンテンツソース
JSON ファイルからソースを作成し、loader() に渡します。
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()],
});ページをコンパイルしてレンダリングします。
import { source } from '@/lib/source';
import { notFound } from 'next/navigation';
import { DocsBody, DocsPage, DocsTitle } from 'fumadocs-ui/layouts/docs/page';
export default async function Page({ params }: PageProps<'/docs/[[...slug]]'>) {
const page = source.getPage((await params).slug);
if (!page) 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>
);
}コンパイラオプション
docstring は Markdown なので、remark や rehype のプラグインを適用できます:
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() を組み合わせて使用します。
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?
最終更新日
