mirror of
https://github.com/hanzo-docs/docs.git
synced 2026-10-26 06:31:49 +00:00
Core: Support indexing description field on documents
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'fumadocs-core': patch
|
||||
---
|
||||
|
||||
Support indexing description field on documents
|
||||
@@ -32,6 +32,7 @@ export async function updateSearchIndexes(
|
||||
documents: indexes.map((docs) => ({
|
||||
_id: docs.id,
|
||||
title: docs.title,
|
||||
description: docs.description,
|
||||
url: docs.url,
|
||||
structured: docs.structuredData,
|
||||
tag: docs.url.split('/')[2],
|
||||
|
||||
@@ -8,6 +8,8 @@ interface DocumentRecord {
|
||||
_id: string;
|
||||
|
||||
title: string;
|
||||
description?: string;
|
||||
|
||||
/**
|
||||
* URL to the page
|
||||
*/
|
||||
@@ -63,73 +65,61 @@ export async function setIndexSettings(index: SearchIndex): Promise<void> {
|
||||
});
|
||||
}
|
||||
|
||||
interface Section {
|
||||
/**
|
||||
* Heading content
|
||||
*/
|
||||
section?: string;
|
||||
|
||||
/**
|
||||
* The anchor id
|
||||
*/
|
||||
section_id?: string;
|
||||
content: string;
|
||||
}
|
||||
|
||||
function getSections(page: DocumentRecord): Section[] {
|
||||
function toIndex(page: DocumentRecord): BaseIndex[] {
|
||||
let id = 0;
|
||||
const indexes: BaseIndex[] = [];
|
||||
const scannedHeadings = new Set<string>();
|
||||
|
||||
return page.structured.contents.flatMap((p) => {
|
||||
function createIndex(
|
||||
section: string | undefined,
|
||||
sectionId: string | undefined,
|
||||
content: string,
|
||||
): BaseIndex {
|
||||
return {
|
||||
objectID: `${page._id}-${(id++).toString()}`,
|
||||
title: page.title,
|
||||
url: page.url,
|
||||
page_id: page._id,
|
||||
tag: page.tag,
|
||||
section,
|
||||
section_id: sectionId,
|
||||
content,
|
||||
...page.extra_data,
|
||||
};
|
||||
}
|
||||
|
||||
if (page.description)
|
||||
indexes.push(createIndex(undefined, undefined, page.description));
|
||||
|
||||
page.structured.contents.forEach((p) => {
|
||||
const heading = p.heading
|
||||
? page.structured.headings.find((h) => p.heading === h.id)
|
||||
: null;
|
||||
|
||||
const section = {
|
||||
section: heading?.content,
|
||||
section_id: heading?.id,
|
||||
content: p.content,
|
||||
};
|
||||
const index = createIndex(heading?.content, heading?.id, p.content);
|
||||
|
||||
if (heading && !scannedHeadings.has(heading.id)) {
|
||||
scannedHeadings.add(heading.id);
|
||||
|
||||
return [
|
||||
{
|
||||
section: heading.content,
|
||||
section_id: heading.id,
|
||||
content: heading.content,
|
||||
},
|
||||
section,
|
||||
];
|
||||
indexes.push(createIndex(heading.content, heading.id, heading.content));
|
||||
}
|
||||
|
||||
return section;
|
||||
indexes.push(index);
|
||||
});
|
||||
|
||||
return indexes;
|
||||
}
|
||||
|
||||
export async function updateDocuments(
|
||||
index: SearchIndex,
|
||||
documents: DocumentRecord[],
|
||||
): Promise<void> {
|
||||
const objects = documents.flatMap((page) => {
|
||||
return getSections(page).map(
|
||||
(section, idx) =>
|
||||
({
|
||||
objectID: `${page._id}-${idx.toString()}`,
|
||||
title: page.title,
|
||||
url: page.url,
|
||||
page_id: page._id,
|
||||
tag: page.tag,
|
||||
...section,
|
||||
...page.extra_data,
|
||||
}) satisfies BaseIndex,
|
||||
);
|
||||
});
|
||||
const objects = documents.flatMap(toIndex);
|
||||
|
||||
await index.replaceAllObjects(objects);
|
||||
}
|
||||
|
||||
export interface BaseIndex extends Section {
|
||||
export interface BaseIndex {
|
||||
objectID: string;
|
||||
title: string;
|
||||
url: string;
|
||||
@@ -139,4 +129,16 @@ export interface BaseIndex extends Section {
|
||||
* The id of page, used for distinct
|
||||
*/
|
||||
page_id: string;
|
||||
|
||||
/**
|
||||
* Heading content
|
||||
*/
|
||||
section?: string;
|
||||
|
||||
/**
|
||||
* Heading (anchor) id
|
||||
*/
|
||||
section_id?: string;
|
||||
|
||||
content: string;
|
||||
}
|
||||
|
||||
@@ -109,6 +109,7 @@ export function createI18nSearchAPI<T extends 'simple' | 'advanced'>(
|
||||
|
||||
export interface Index {
|
||||
title: string;
|
||||
description?: string;
|
||||
content: string;
|
||||
url: string;
|
||||
keywords?: string;
|
||||
@@ -132,6 +133,14 @@ export function initSearchAPI({ indexes, language }: SimpleOptions): SearchAPI {
|
||||
tokenize: 'forward',
|
||||
resolution: 9,
|
||||
},
|
||||
{
|
||||
field: 'description',
|
||||
tokenize: 'strict',
|
||||
context: {
|
||||
depth: 1,
|
||||
resolution: 9,
|
||||
},
|
||||
},
|
||||
{
|
||||
field: 'content',
|
||||
tokenize: 'strict',
|
||||
@@ -152,6 +161,7 @@ export function initSearchAPI({ indexes, language }: SimpleOptions): SearchAPI {
|
||||
for (const page of items) {
|
||||
index.add({
|
||||
title: page.title,
|
||||
description: page.description,
|
||||
url: page.url,
|
||||
content: page.content,
|
||||
keywords: page.keywords,
|
||||
@@ -181,10 +191,12 @@ export function initSearchAPI({ indexes, language }: SimpleOptions): SearchAPI {
|
||||
export interface AdvancedIndex {
|
||||
id: string;
|
||||
title: string;
|
||||
description?: string;
|
||||
|
||||
keywords?: string;
|
||||
|
||||
/**
|
||||
* Required if `tag` is enabled
|
||||
* Required if tag filter is enabled
|
||||
*/
|
||||
tag?: string;
|
||||
/**
|
||||
@@ -251,6 +263,17 @@ export function initSearchAPIAdvanced({
|
||||
url: page.url,
|
||||
});
|
||||
|
||||
if (page.description) {
|
||||
index.add({
|
||||
id: page.id + (id++).toString(),
|
||||
page_id: page.id,
|
||||
tag: page.tag,
|
||||
type: 'text',
|
||||
url: page.url,
|
||||
content: page.description,
|
||||
});
|
||||
}
|
||||
|
||||
for (const heading of data.headings) {
|
||||
index.add({
|
||||
id: page.id + (id++).toString(),
|
||||
|
||||
Reference in New Issue
Block a user