fumadocs-core
Version:
The React.js library for building a documentation website
58 lines (57 loc) • 1.47 kB
JavaScript
// src/search/server/build-doc.ts
/**
* The id of a page's n-th record after its own. Page URLs never contain a raw `#`, so it can't be another page's id.
*/
function recordId(pageId, n) {
return `${pageId}#${n}`;
}
function buildDocuments(indexes) {
const docs = [];
for (const page of indexes) {
const pageTag = page.tag ?? [];
const tags = Array.isArray(pageTag) ? pageTag : [pageTag];
const data = page.structuredData;
let id = 0;
docs.push({
id: page.id,
page_id: page.id,
type: "page",
content: page.title,
breadcrumbs: page.breadcrumbs,
tags,
url: page.url,
locale: page.locale
});
const nextId = () => recordId(page.id, id++);
if (page.description && !data.contents.some((item) => item.content === page.description)) docs.push({
id: nextId(),
page_id: page.id,
tags,
type: "text",
url: page.url,
content: page.description,
locale: page.locale
});
for (const heading of data.headings) docs.push({
id: nextId(),
page_id: page.id,
type: "heading",
tags,
url: `${page.url}#${heading.id}`,
content: heading.content,
locale: page.locale
});
for (const content of data.contents) docs.push({
id: nextId(),
page_id: page.id,
tags,
type: "text",
url: content.heading ? `${page.url}#${content.heading}` : page.url,
content: content.content,
locale: page.locale,
table: content.table
});
}
return docs;
}
export { recordId as n, buildDocuments as t };