fumadocs-core
Version:
The library for building a documentation website in Next.js
59 lines (57 loc) • 1.5 kB
JavaScript
import {
createContentHighlighter
} from "./chunk-OTD7MV33.js";
import "./chunk-JSBRDJBE.js";
// src/search/client/algolia.ts
function groupResults(hits) {
const grouped = [];
const scannedUrls = /* @__PURE__ */ new Set();
for (const hit of hits) {
if (!scannedUrls.has(hit.url)) {
scannedUrls.add(hit.url);
grouped.push({
id: hit.url,
type: "page",
breadcrumbs: hit.breadcrumbs,
url: hit.url,
content: hit.title
});
}
grouped.push({
id: hit.objectID,
type: hit.content === hit.section ? "heading" : "text",
url: hit.section_id ? `${hit.url}#${hit.section_id}` : hit.url,
content: hit.content
});
}
return grouped;
}
async function searchDocs(query, { indexName, onSearch, client, locale, tag }) {
if (query.trim().length === 0) return [];
const result = onSearch ? await onSearch(query, tag, locale) : await client.searchForHits({
requests: [
{
type: "default",
indexName,
query,
distinct: 5,
hitsPerPage: 10,
filters: tag ? `tag:${tag}` : void 0
}
]
});
const highlighter = createContentHighlighter(query);
return groupResults(result.results[0].hits).flatMap((hit) => {
if (hit.type === "page") {
return {
...hit,
contentWithHighlights: hit.contentWithHighlights ?? highlighter.highlight(hit.content)
};
}
return [];
});
}
export {
groupResults,
searchDocs
};