UNPKG

fumadocs-core

Version:

The React.js library for building a documentation website

54 lines (53 loc) • 1.83 kB
import { n as useSearch } from "../../use-search-eSAjMJVB.js"; import { n as search, r as toSearchIndex, t as createDocument } from "../../utils-BQW8csGG.js"; import { n as join, t as BASE_PATH } from "../../url-BF81yL4m.js"; import { useCallback } from "react"; // src/search/client/flexsearch-static.ts function initDocument(data) { const document = createDocument(); for (const [k, v] of Object.entries(data)) document.import(k, v); return toSearchIndex(document); } const cacheMap = /* @__PURE__ */ new Map(); async function searchFlexsearchStatic({ from = join(BASE_PATH, "/api/search"), locale = "", tag }, query) { let dbs = cacheMap.get(from); if (!dbs) cacheMap.set(from, dbs = init(from)); const db = (await dbs).get(locale); if (!db) return []; return search(db, query, tag); } function flexsearchStaticClient(options = {}) { return { deps: [ options.from, options.locale, String(options.tag) ], search: (query) => searchFlexsearchStatic(options, query) }; } async function init(from) { const res = await fetch(from); if (!res.ok) throw new Error(`failed to fetch exported search indexes from ${from}, make sure the search database is exported and available for client.`); const data = await res.json(); const dbs = /* @__PURE__ */ new Map(); if (data.type === "i18n") { for (const [locale, map] of Object.entries(data.raw)) dbs.set(locale, initDocument(map)); return dbs; } else dbs.set("", initDocument(data.raw)); return dbs; } function useFlexsearchStatic({ from, locale, tag, ...rest } = {}) { const tags = String(tag ?? ""); const run = useCallback((query) => searchFlexsearchStatic({ from, locale, tag: tags ? tags.split(",") : void 0 }, query), [ from, locale, tags ]); return useSearch(run, rest); } export { flexsearchStaticClient, useFlexsearchStatic };