UNPKG

fumadocs-core

Version:

The React.js library for building a documentation website

66 lines (65 loc) • 1.57 kB
import Search from "flexsearch"; // src/search/flexsearch/utils.ts function toSearchIndex(document) { const pages = /* @__PURE__ */ new Map(); for (let key = 0, doc = document.get(key); doc; doc = document.get(++key)) if (doc.type === "page") pages.set(doc.id, doc); return { document, pages }; } async function search({ document, pages }, query, tag, limit = 60) { const arr = await document.searchAsync(query, { index: "content", limit, tag: tag ? { tags: tag } : void 0 }); const out = []; if (arr.length === 0) return out; const results = arr[0].result; const grouped = /* @__PURE__ */ new Map(); for (const key of results) { const doc = document.get(key); if (!doc) continue; let list = grouped.get(doc.page_id); if (!list) { list = []; grouped.set(doc.page_id, list); } if (doc.type !== "page") list.push(doc); } for (const [page_id, items] of grouped) { const page = pages.get(page_id); if (!page) continue; out.push({ id: page_id, type: "page", content: page.content, breadcrumbs: page.breadcrumbs, url: page.url }); for (const item of items) out.push({ id: item.id, content: item.content, breadcrumbs: item.breadcrumbs, type: item.type, url: item.url, table: item.table }); } return out; } function createDocument(options) { return new Search.Document({ tokenize: "full", ...options, document: { id: "id", index: ["content"], tag: ["tags"], store: true, ...options?.document } }); } export { search as n, toSearchIndex as r, createDocument as t };