UNPKG

fumadocs-core

Version:

The React.js library for building a documentation website

106 lines (105 loc) • 2.4 kB
import { t as removeUndefined } from "../../remove-undefined-C_sjLeQT.js"; import { n as useSearch } from "../../use-search-eSAjMJVB.js"; import { useCallback, useRef } from "react"; // src/search/client/orama-cloud-legacy.ts async function searchOramaCloudLegacy(options, query) { const { index = "default", client, params: extraParams = {}, tag } = options; const list = []; if (index === "crawler") { const result = await client.search({ ...extraParams, term: query, where: { category: tag ? { eq: tag.slice(0, 1).toUpperCase() + tag.slice(1) } : void 0, ...extraParams.where }, limit: 10 }); if (!result) return list; for (const hit of result.hits) { const doc = hit.document; list.push({ id: hit.id, type: "page", content: doc.title, url: doc.path }, { id: "page" + hit.id, type: "text", content: doc.content, url: doc.path }); } return list; } const params = { properties: [ "title", "section", "content" ], ...extraParams, term: query, where: removeUndefined({ tag, ...extraParams.where }), groupBy: { properties: ["page_id"], maxResult: 7, ...extraParams.groupBy } }; const result = await client.search(params); if (!result || !result.groups) return list; for (const item of result.groups) { let addedHead = false; for (const hit of item.result) { const doc = hit.document; if (!addedHead) { list.push({ id: doc.page_id, type: "page", content: doc.title, breadcrumbs: doc.breadcrumbs, url: doc.url }); addedHead = true; } list.push({ id: doc.id, content: doc.content, type: doc.content === doc.section ? "heading" : "text", url: doc.section_id ? `${doc.url}#${doc.section_id}` : doc.url, table: doc.table }); } } return list; } function oramaCloudLegacyClient(options) { return { deps: [ options.index, options.client, options.tag ], search: (query) => searchOramaCloudLegacy(options, query) }; } function useOramaCloudLegacySearch({ client, index, tag, ...rest }) { const latest = useRef(rest); latest.current = rest; const run = useCallback((query) => searchOramaCloudLegacy({ ...latest.current, client, index, tag }, query), [ client, index, tag ]); return useSearch(run, rest); } export { oramaCloudLegacyClient, useOramaCloudLegacySearch };