UNPKG

fumadocs-core

Version:

The React.js library for building a documentation website

92 lines (91 loc) • 2.42 kB
import { t as buildDocuments } from "../build-index-8ryBfL45.js"; import { n as recordId } from "../build-doc-DuN5O6YG.js"; // src/search/algolia.ts /** * Build the search indexes of every page in a source. */ function toDocuments(source, options = {}) { return buildDocuments(source, (index, page) => ({ _id: index.id, title: index.title, description: index.description, url: index.url, structured: index.structuredData, tag: options.tag?.(page) })); } /** * Update index settings and replace all objects * * @param client - Algolia Admin Client * @param options - Index Options */ async function sync(client, options) { const { indexName = "document", documents } = options; await setIndexSettings(client, indexName); await updateDocuments(client, indexName, documents); } async function setIndexSettings(client, indexName) { await client.setSettings({ indexName, indexSettings: { attributeForDistinct: "page_id", attributesToRetrieve: [ "title", "section", "content", "url", "section_id", "breadcrumbs", "table" ], searchableAttributes: [ "title", "section", "content" ], attributesToSnippet: [], attributesForFaceting: ["tag"] } }); } function toIndex(page) { let id = 0; const indexes = []; const scannedHeadings = /* @__PURE__ */ new Set(); function createIndex(section, sectionId, content, table) { return { objectID: recordId(page._id, id++), breadcrumbs: page.breadcrumbs, title: page.title, url: page.url, page_id: page._id, tag: page.tag, section, section_id: sectionId, content, table, ...page.extra_data }; } if (page.description) indexes.push(createIndex(void 0, void 0, page.description)); const { headings, contents } = page.structured; for (const p of contents) { const heading = p.heading ? headings.find((h) => p.heading === h.id) : null; const index = createIndex(heading?.content, p.heading, p.content, p.table); if (heading && !scannedHeadings.has(heading.id)) { scannedHeadings.add(heading.id); indexes.push(createIndex(heading.content, heading.id, heading.content)); } indexes.push(index); } return indexes; } async function updateDocuments(client, indexName, documents) { const objects = documents.flatMap(toIndex); await client.replaceAllObjects({ indexName, objects }); } export { setIndexSettings, sync, toDocuments, updateDocuments };