UNPKG

fumadocs-core

Version:

The React.js library for building a documentation website

61 lines (60 loc) • 2 kB
import { r as createBreadcrumbs, t as buildDocuments } from "../build-index-8ryBfL45.js"; // src/search/meilisearch.ts /** * Build the search documents of every page in a source. */ async function toDocuments(source, options = {}) { const loader = typeof source === "function" ? await source() : source; const getBreadcrumbs = createBreadcrumbs(loader); return buildDocuments(loader, (index, page) => ({ title: index.title, description: index.description, breadcrumbs: getBreadcrumbs(page), url: index.url, structured: index.structuredData, tag: options.tag?.(page), locale: page.locale })); } /** * Configure the index and replace its documents, existing documents stay searchable until the new ones are indexed. * * @param client - Meilisearch client with an admin API key */ async function sync(client, options) { const index = client.index(options.indexName); const documents = []; for (const { description, structured, ...page } of options.documents) { const add = (type, content, heading) => documents.push({ ...page, id: documents.length, type, content, heading }); add("page", page.title); if (description && !structured.contents.some((item) => item.content === description)) add("text", description); for (const heading of structured.headings) add("heading", heading.content, heading.id); for (const item of structured.contents) documents.push({ ...page, id: documents.length, type: "text", ...item }); } await run(index.updateSettings({ searchableAttributes: ["content"], filterableAttributes: [ "id", "tag", "locale" ] })); await run(index.addDocuments(documents, { primaryKey: "id" })); await run(index.deleteDocuments({ filter: `id >= ${documents.length}` })); } async function run(task) { const { status, error } = await task.waitTask({ timeout: 0 }); if (status !== "succeeded") throw new Error(error ? error.message : `Meilisearch task ${status}`, { cause: error }); } export { sync, toDocuments };