Core: support additional components option of Orama search

This commit is contained in:
Fuma Nama
2024-12-10 20:30:22 +08:00
parent 010da9e8ee
commit 4dfde6b02e
7 changed files with 30 additions and 37 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'fumadocs-core': patch
---
support additional `components` option of Orama search
+9 -11
View File
@@ -17,19 +17,17 @@ export const schema = {
export async function createDBSimple({
indexes,
language,
tokenizer,
...rest
}: SimpleOptions): Promise<Orama<typeof schema>> {
const items = typeof indexes === 'function' ? await indexes() : indexes;
const db = await create({
language,
schema: {
url: 'string',
title: 'string',
description: 'string',
content: 'string',
keywords: 'string',
} as const,
});
const db = (await create({
schema,
components: {
tokenizer,
},
...rest,
})) as unknown as Orama<typeof schema>;
await insertMultiple(
db,
+3 -3
View File
@@ -25,13 +25,13 @@ export async function createDB({
}: AdvancedOptions): Promise<Orama<typeof advancedSchema>> {
const items = typeof indexes === 'function' ? await indexes() : indexes;
const db = await create({
...rest,
const db = (await create({
schema: advancedSchema,
components: {
tokenizer,
},
});
...rest,
})) as unknown as Orama<typeof advancedSchema>;
const mapTo: PartialSchemaDeep<AdvancedDocument>[] = [];
items.forEach((page) => {
@@ -14,7 +14,7 @@ import {
import { type StructuredData } from '@/mdx-plugins';
import { type LocaleMap } from '@/search/i18n-api';
function defaultToIndex(page: Page): AdvancedIndex {
function pageToIndex(page: Page): AdvancedIndex {
if (!('structuredData' in page.data)) {
throw new Error(
'Cannot find structured data from page, please define the page to index function.',
@@ -39,7 +39,7 @@ type Options = Omit<AdvancedOptions, 'language' | 'indexes'> & {
export function createFromSource<S extends LoaderOutput<LoaderConfig>>(
source: S,
pageToIndex: (page: InferPageType<S>) => AdvancedIndex = defaultToIndex,
pageToIndexFn: (page: InferPageType<S>) => AdvancedIndex = pageToIndex,
options: Options = {},
): SearchAPI {
if (source._i18n) {
@@ -49,7 +49,7 @@ export function createFromSource<S extends LoaderOutput<LoaderConfig>>(
indexes: source.getLanguages().flatMap((entry) => {
return entry.pages.map((page) => {
return {
...pageToIndex(page as InferPageType<S>),
...pageToIndexFn(page as InferPageType<S>),
locale: entry.language,
};
});
@@ -60,7 +60,7 @@ export function createFromSource<S extends LoaderOutput<LoaderConfig>>(
return createSearchAPI('advanced', {
...options,
indexes: source.getPages().map((page) => {
return pageToIndex(page as InferPageType<S>);
return pageToIndexFn(page as InferPageType<S>);
}),
});
}
+1 -1
View File
@@ -138,7 +138,7 @@ export function createI18nSearchAPI<T extends 'simple' | 'advanced'>(
data: Object.fromEntries(await Promise.all(entries)) as object,
};
},
search: async (query, searchOptions) => {
async search(query, searchOptions) {
const map = await get;
const locale = searchOptions?.locale ?? options.i18n.defaultLanguage;
@@ -27,7 +27,6 @@ export async function searchAdvanced(
params = {
...params,
term: query,
tolerance: 1,
properties: ['content', 'keywords'],
...extraParams,
where: params.where,
+8 -17
View File
@@ -1,12 +1,4 @@
import {
type Orama,
type SearchParams,
type DefaultTokenizerConfig,
type Tokenizer,
type OramaPlugin,
type SorterConfig,
save,
} from '@orama/orama';
import { type Orama, type SearchParams, save, create } from '@orama/orama';
import { type NextRequest } from 'next/server';
import type { StructuredData } from '@/mdx-plugins/remark-structure';
import type { SortedResult } from '@/server/types';
@@ -52,13 +44,12 @@ export interface SearchAPI extends SearchServer {
*/
export type Dynamic<T> = () => T[] | Promise<T[]>;
interface SharedOptions {
language?: string;
type OramaInput = Parameters<typeof create>[0];
sort?: SorterConfig;
tokenizer?: Tokenizer | DefaultTokenizerConfig;
plugins?: OramaPlugin[];
}
type SharedOptions = Pick<OramaInput, 'sort' | 'components' | 'plugins'> & {
language?: string;
tokenizer?: Required<OramaInput>['components']['tokenizer'];
};
export interface SimpleOptions extends SharedOptions {
indexes: Index[] | Dynamic<Index>;
@@ -109,7 +100,7 @@ export function initSimpleSearch(options: SimpleOptions): SearchServer {
...save(await doc),
};
},
search: async (query) => {
async search(query) {
const db = await doc;
return searchSimple(db, query, options.search);
@@ -145,7 +136,7 @@ export function initAdvancedSearch(options: AdvancedOptions): SearchServer {
...save(await get),
};
},
search: async (query, searchOptions) => {
async search(query, searchOptions) {
const db = await get;
return searchAdvanced(db, query, searchOptions?.tag, options.search);