fumadocs-core
Version:
The React.js library for building a documentation website
108 lines (107 loc) • 2.42 kB
JavaScript
import { t as removeUndefined } from "../../remove-undefined-C_sjLeQT.js";
import { n as useSearch } from "../../use-search-eSAjMJVB.js";
import { useCallback, useRef } from "react";
// src/search/client/orama-cloud.ts
async function searchOramaCloud(options, query) {
const { index = "default", client, params: extraParams, tag } = options;
const list = [];
if (index === "crawler") {
const result = await client.search({
datasources: [],
...extraParams,
term: query,
where: {
category: tag ? { eq: tag.slice(0, 1).toUpperCase() + tag.slice(1) } : void 0,
...extraParams?.where
},
limit: 10
});
if (!result) return list;
for (const hit of result.hits) {
const doc = hit.document;
list.push({
id: hit.id,
type: "page",
content: doc.title,
url: doc.path
}, {
id: "page" + hit.id,
type: "text",
content: doc.content,
url: doc.path
});
}
return list;
}
const result = await client.search({
datasources: [],
properties: [
"title",
"section",
"content"
],
...extraParams,
term: query,
limit: 20,
where: removeUndefined({
tag,
...extraParams?.where
}),
groupBy: {
properties: ["page_id"],
max_results: 7,
...extraParams?.groupBy
}
});
if (!result || !result.groups) return list;
for (const item of result.groups) {
let addedHead = false;
for (const hit of item.result) {
const doc = hit.document;
if (!addedHead) {
list.push({
id: doc.page_id,
type: "page",
content: doc.title,
breadcrumbs: doc.breadcrumbs,
url: doc.url
});
addedHead = true;
}
list.push({
id: doc.id,
content: doc.content,
type: doc.content === doc.section ? "heading" : "text",
url: doc.section_id ? `${doc.url}#${doc.section_id}` : doc.url,
table: doc.table
});
}
}
return list.length > 80 ? list.slice(0, 80) : list;
}
function oramaCloudClient(options) {
return {
deps: [
options.index,
options.client,
options.tag
],
search: (query) => searchOramaCloud(options, query)
};
}
function useOramaCloudSearch({ client, index, tag, ...rest }) {
const latest = useRef(rest);
latest.current = rest;
const run = useCallback((query) => searchOramaCloud({
...latest.current,
client,
index,
tag
}, query), [
client,
index,
tag
]);
return useSearch(run, rest);
}
export { oramaCloudClient, useOramaCloudSearch };