Typesense
搭配 Fumadocs UI 使用 Typesense。
此整合由社群維護。
設定
Fumadocs CLI 可以幫您完成設定,包括建立搜尋對話方塊、static.json 路由,以及在每次建置後同步搜尋索引的指令碼:
npx @fumadocs/cli feature search --provider typesense-
整合 Typesense Search。
-
建立搜尋對話方塊元件。
components/search.tsx 'use client'; import { SearchDialog, SearchDialogClose, SearchDialogContent, SearchDialogFooter, SearchDialogHeader, SearchDialogIcon, SearchDialogInput, SearchDialogList, SearchDialogOverlay, type SharedProps, } from 'fumadocs-ui/components/dialog/search'; import { useI18n } from 'fumadocs-ui/contexts/i18n'; import { Client } from 'typesense'; import { useTypesenseSearch } from 'typesense-fumadocs-adapter/client'; const client = new Client({ nodes: [{ url: 'YOUR_TYPESENSE_SERVER_URL' }], apiKey: 'YOUR_TYPESENSE_SEARCH_ONLY_API_KEY', }); export default function TypesenseSearchDialog(props: SharedProps) { const { locale } = useI18n(); // 選用 const { search, setSearch, query } = useTypesenseSearch({ typesenseCollectionName: 'docs', locale, legacy: false, // 選用,若 fumadocs-ui 版本低於 16.6.0,請設為 true client, }); return ( <SearchDialog search={search} onSearchChange={setSearch} isLoading={query.isLoading} {...props} > <SearchDialogOverlay /> <SearchDialogContent> <SearchDialogHeader> <SearchDialogIcon /> <SearchDialogInput /> <SearchDialogClose /> </SearchDialogHeader> <SearchDialogList items={query.data !== 'empty' ? query.data : null} /> <SearchDialogFooter> <div className="w-full text-right text-xs text-fd-muted-foreground"> <a href="https://typesense.org" rel="noreferrer noopener" target="_blank"> Search powered by Typesense </a> </div> </SearchDialogFooter> </SearchDialogContent> </SearchDialog> ); }
替換搜尋對話方塊
請在根路由中,將您的搜尋對話方塊傳入 <RootProvider />:
import { createRootRoute, HeadContent, Outlet, Scripts } from '@tanstack/react-router';
import { RootProvider } from 'fumadocs-ui/provider/tanstack';
import SearchDialog from '@/components/search';
export const Route = createRootRoute({
component: RootComponent,
});
function RootComponent() {
return (
<html suppressHydrationWarning>
<head>
<HeadContent />
</head>
<body className="flex flex-col min-h-screen">
<RootProvider search={{ SearchDialog }}>
<Outlet />
</RootProvider>
<Scripts />
</body>
</html>
);
}標籤篩選
您也可以視需要加入依標籤篩選結果的 UI。請在搜尋伺服器上設定標籤篩選,並加入以下內容:
'use client';
import {
SearchDialog,
SearchDialogContent,
SearchDialogFooter,
SearchDialogOverlay,
type SharedProps,
TagsList,
TagsListItem,
} from 'fumadocs-ui/components/dialog/search';
import { useState } from 'react';
import { useTypesenseSearch } from 'typesense-fumadocs-adapter/client';
export default function TypesenseSearchDialog(props: SharedProps) {
const [tag, setTag] = useState<string | undefined>();
const { search, setSearch, query } = useTypesenseSearch({
tag,
// 其他選項……
});
return (
<SearchDialog>
<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?
最後更新於
