fumadocs-core
Version:
The React.js library for building a documentation website
78 lines (77 loc) • 2.3 kB
JavaScript
import { t as buildDocuments } from "../build-index-8ryBfL45.js";
import { n as recordId } from "../build-doc-DuN5O6YG.js";
// src/search/orama-cloud.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)
}));
}
/**
* Replace the documents of an index in a transaction.
*
* @returns the transaction, commit it to deploy the documents when `autoDeploy` is `false`
*/
async function sync(orama, options) {
const { autoDeploy = true } = options;
const { transaction } = orama.index.set(options.index);
await transaction.open();
await transaction.insertDocuments(options.documents.flatMap(toIndex));
if (autoDeploy) await transaction.commit();
return transaction;
}
/**
* Like `sync()`, for the index of each locale.
*
* @returns the transaction of each locale
*/
async function syncI18n(orama, { indexes, documents, ...options }) {
const transactions = {};
await Promise.all(documents.map(async ({ locale, items }) => {
transactions[locale] = await sync(orama, {
...options,
index: indexes[locale],
documents: items
});
}));
return transactions;
}
function toIndex(page) {
let id = 0;
const indexes = [];
const scannedHeadings = /* @__PURE__ */ new Set();
function createIndex(section, sectionId, content, table) {
return {
id: recordId(page.id, id++),
title: page.title,
url: page.url,
page_id: page.id,
tag: page.tag,
section,
section_id: sectionId,
content,
table,
breadcrumbs: page.breadcrumbs,
...page.extra_data
};
}
if (page.description) indexes.push(createIndex(void 0, void 0, page.description));
page.structured.contents.forEach((p) => {
const heading = p.heading ? page.structured.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;
}
export { sync, syncI18n, toDocuments };