ZBSearch(預設)
以 ZBSearch 為基礎的預設搜尋引擎。
概覽
Fumadocs 預設已設定好 ZBSearch 搜尋引擎,開箱即用。
搜尋會透過伺服器上執行的 API 端點進行;若是靜態網站,則改用靜態快取的 JSON 檔案。
設定
建立搜尋對話方塊。
若您的搜尋路由採用每次請求時算繪 (需要轉接器) ,請使用此用戶端:
'use client';
import { useFetchSearch } from 'fumadocs-core/search/client';
import {
SearchDialog,
SearchDialogClose,
SearchDialogContent,
SearchDialogHeader,
SearchDialogIcon,
SearchDialogInput,
SearchDialogList,
SearchDialogOverlay,
type SharedProps,
} from 'fumadocs-ui/components/dialog/search';
import { useI18n } from 'fumadocs-ui/contexts/i18n';
export default function DefaultSearchDialog(props: SharedProps) {
const { locale } = useI18n(); // (optional) for i18n
const search = useFetchSearch({
locale,
});
return (
<SearchDialog {...search} {...props}>
<SearchDialogOverlay />
<SearchDialogContent>
<SearchDialogHeader>
<SearchDialogIcon />
<SearchDialogInput />
<SearchDialogClose />
</SearchDialogHeader>
<SearchDialogList />
</SearchDialogContent>
</SearchDialog>
);
}
替換搜尋對話方塊
您的 provider 元件已會將 src/components/search.tsx 傳入 <RootProvider />,因此只要替換該檔案即可:
import { navigate } from 'astro:transitions/client';
import type { ReactNode } from 'react';
import { RootProvider } from 'fumadocs-ui/provider/astro';
import SearchDialog from '@/components/search';
export interface ProviderProps {
pathname: string;
params: Record<string, string | undefined>;
children?: ReactNode;
}
export function Provider({ pathname, params, children }: ProviderProps) {
return (
<RootProvider
pathname={pathname}
params={params}
navigate={navigate}
search={{ SearchDialog }}
>
{children}
</RootProvider>
);
}標籤篩選
您也可以視需要加入依標籤篩選結果的 UI。請在搜尋伺服器上設定標籤篩選,並加入以下內容:
'use client';
import {
SearchDialog,
SearchDialogContent,
SearchDialogFooter,
SearchDialogOverlay,
type SharedProps,
TagsList,
TagsListItem,
} from 'fumadocs-ui/components/dialog/search';
import { useState } from 'react';
import { useFetchSearch } from 'fumadocs-core/search/client';
export default function CustomSearchDialog(props: SharedProps) {
const [tag, setTag] = useState<string | undefined>();
const search = useFetchSearch({
tag,
});
return (
<SearchDialog {...search} {...props}>
<SearchDialogOverlay />
<SearchDialogContent>
...
<SearchDialogFooter className="flex flex-row">
<TagsList tag={tag} onTagChange={setTag}>
<TagsListItem value="my-value">My Value</TagsListItem>
</TagsList>
</SearchDialogFooter>
</SearchDialogContent>
</SearchDialog>
);
}How is this guide?
最後更新於
