Skip to content

Commit

Permalink
Merge pull request #289 from fuma-nama/dev
Browse files Browse the repository at this point in the history
Update to 10.0.3
  • Loading branch information
fuma-nama authored Mar 9, 2024
2 parents 4cb43f2 + f68ec22 commit bf2619a
Show file tree
Hide file tree
Showing 26 changed files with 419 additions and 432 deletions.
5 changes: 5 additions & 0 deletions apps/docs/content/docs/mdx/props.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { type CreateMDXOptions } from 'fumadocs-mdx/config';

export type SearchIndexOptions = NonNullable<
Exclude<CreateMDXOptions['buildSearchIndex'], boolean>
>;
5 changes: 5 additions & 0 deletions apps/docs/content/docs/mdx/search-index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@ After running `next build`, generated search indexes will be written to `.next/s
You can access them with `JSON.parse`.

```ts title="update-index.mts"
import { readFileSync } from 'node:fs';
import type { SearchIndex } from 'fumadocs-mdx';

const indexes = JSON.parse(
readFileSync('./.next/server/chunks/fumadocs_search.json').toString(),
) as SearchIndex[];
```

### Options

<AutoTypeTable path="mdx" name="SearchIndexOptions" />
1 change: 1 addition & 0 deletions apps/docs/mdx-components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { cn } from './utils/cn';
const shortcuts: Record<string, string> = {
ui: './content/docs/ui/props.ts',
headless: './content/docs/headless/props.ts',
mdx: './content/docs/mdx/props.ts',
};

export function useMDXComponents(components: MDXComponents): MDXComponents {
Expand Down
6 changes: 3 additions & 3 deletions examples/contentlayer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
},
"dependencies": {
"contentlayer": "0.3.4",
"fumadocs-contentlayer": "1.1.3",
"fumadocs-core": "10.0.2",
"fumadocs-ui": "10.0.2",
"fumadocs-contentlayer": "1.1.4",
"fumadocs-core": "10.0.3",
"fumadocs-ui": "10.0.3",
"next": "14.1.2",
"next-contentlayer": "0.3.4",
"react": "18.2.0",
Expand Down
6 changes: 3 additions & 3 deletions examples/next-mdx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"start": "next start"
},
"dependencies": {
"fumadocs-core": "10.0.2",
"fumadocs-mdx": "8.2.1",
"fumadocs-ui": "10.0.2",
"fumadocs-core": "10.0.3",
"fumadocs-mdx": "8.2.2",
"fumadocs-ui": "10.0.3",
"next": "14.1.2",
"react": "18.2.0",
"react-dom": "18.2.0"
Expand Down
7 changes: 7 additions & 0 deletions packages/contentlayer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# fumadocs-contentlayer

## 1.1.4

### Patch Changes

- Updated dependencies [6f321e5]
- [email protected]

## 1.1.3

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/contentlayer/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fumadocs-contentlayer",
"version": "1.1.3",
"version": "1.1.4",
"description": "The Contentlayer adapter for Fumadocs",
"keywords": [
"NextJs",
Expand Down
6 changes: 6 additions & 0 deletions packages/core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# next-docs-zeta

## 10.0.3

### Patch Changes

- 6f321e5: Fix type errors of flexseach

## 10.0.2

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fumadocs-core",
"version": "10.0.2",
"version": "10.0.3",
"description": "The library for building a documentation website in Next.js",
"keywords": [
"NextJs",
Expand Down Expand Up @@ -118,7 +118,7 @@
"@formatjs/intl-localematcher": "^0.5.4",
"@shikijs/rehype": "^1.1.7",
"@shikijs/transformers": "^1.1.7",
"flexsearch": "^0.7.43",
"flexsearch": "0.7.21",
"github-slugger": "^2.0.0",
"hast-util-to-estree": "^3.1.0",
"negotiator": "^0.6.3",
Expand Down
173 changes: 87 additions & 86 deletions packages/core/src/search/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import type { StructuredData } from '@/mdx-plugins/remark-structure';
import type { SortedResult } from './shared';

interface SearchAPI {
GET: (
request: NextRequest,
) => NextResponse<SortedResult[]> | Promise<NextResponse<SortedResult[]>>;
GET: (request: NextRequest) => NextResponse<SortedResult[]>;

search: (
query: string,
options?: { locale?: string; tag?: string },
) => SortedResult[];
}

interface SimpleOptions {
Expand All @@ -30,6 +33,23 @@ type ToI18n<T extends { indexes: unknown }> = Omit<
indexes: [language: string, indexes: T['indexes']][];
};

function create(search: SearchAPI['search']): SearchAPI {
return {
search,
GET(request) {
const query = request.nextUrl.searchParams.get('query');
if (!query) return NextResponse.json([]);

return NextResponse.json(
search(query, {
tag: request.nextUrl.searchParams.get('tag') ?? undefined,
locale: request.nextUrl.searchParams.get('locale') ?? undefined,
}),
);
},
};
}

export function createSearchAPI<T extends 'simple' | 'advanced'>(
type: T,
options: T extends 'simple' ? SimpleOptions : AdvancedOptions,
Expand Down Expand Up @@ -59,18 +79,15 @@ export function createI18nSearchAPI<T extends 'simple' | 'advanced'>(
);
}

return {
GET(request) {
const locale = request.nextUrl.searchParams.get('locale');
if (locale) {
const handler = map.get(locale);
return create((query, searchOptions) => {
if (searchOptions?.locale) {
const handler = map.get(searchOptions.locale);

if (handler) return handler.GET(request);
}
if (handler) return handler.search(query, searchOptions);
}

return NextResponse.json([]);
},
};
return [];
});
}

interface Index {
Expand Down Expand Up @@ -121,30 +138,21 @@ export function initSearchAPI({ indexes, language }: SimpleOptions): SearchAPI {
});
}

return {
GET(request) {
const { searchParams } = request.nextUrl;
const query = searchParams.get('query');

if (!query) return NextResponse.json([]);

const results = index.search(query, 5, {
enrich: true,
suggest: true,
});
return create((query) => {
const results = index.search(query, 5, {
enrich: true,
suggest: true,
});

if (results.length === 0) return NextResponse.json([]);
if (results.length === 0) return [];

const pages = results[0].result.map<SortedResult>((page) => ({
type: 'page',
content: page.doc.title,
id: page.doc.url,
url: page.doc.url,
}));

return NextResponse.json(pages);
},
};
return results[0].result.map<SortedResult>((page) => ({
type: 'page',
content: page.doc.title,
id: page.doc.url,
url: page.doc.url,
}));
});
}

interface AdvancedIndex {
Expand Down Expand Up @@ -230,64 +238,57 @@ export function initSearchAPIAdvanced({
}
}

return {
GET(request) {
const query = request.nextUrl.searchParams.get('query');
const paramTag = request.nextUrl.searchParams.get('tag');
return create((query, options) => {
const results = index.search(query, 5, {
enrich: true,
tag: options?.tag,
limit: 6,
});

if (!query) return NextResponse.json([]);
const map = new Map<string, SortedResult[]>();
const sortedResult: SortedResult[] = [];

const results = index.search(query, 5, {
enrich: true,
tag: paramTag ?? undefined,
limit: 6,
});
for (const item of results[0]?.result ?? []) {
if (item.doc.type === 'page') {
if (!map.has(item.doc.page_id)) {
map.set(item.doc.page_id, []);
}

const map = new Map<string, SortedResult[]>();
const sortedResult: SortedResult[] = [];
continue;
}

for (const item of results[0]?.result ?? []) {
if (item.doc.type === 'page') {
if (!map.has(item.doc.page_id)) {
map.set(item.doc.page_id, []);
}
const i: SortedResult = {
id: item.doc.id,
content: item.doc.content,
type: item.doc.type,
url: item.doc.url,
};

if (map.has(item.doc.page_id)) {
map.get(item.doc.page_id)?.push(i);
} else {
map.set(item.doc.page_id, [i]);
}
}

continue;
for (const [id, items] of map.entries()) {
const page = (
index as unknown as {
get: (id: string) => InternalIndex | null;
}
).get(id);

const i: SortedResult = {
id: item.doc.id,
content: item.doc.content,
type: item.doc.type,
url: item.doc.url,
};

if (map.has(item.doc.page_id)) {
map.get(item.doc.page_id)?.push(i);
} else {
map.set(item.doc.page_id, [i]);
}
}
if (!page) continue;

for (const [id, items] of map.entries()) {
const page = (
index as unknown as {
get: (id: string) => InternalIndex | null;
}
).get(id);

if (!page) continue;

sortedResult.push({
id: page.id,
content: page.content,
type: 'page',
url: page.url,
});
sortedResult.push(...items);
}
sortedResult.push({
id: page.id,
content: page.content,
type: 'page',
url: page.url,
});
sortedResult.push(...items);
}

return NextResponse.json(sortedResult);
},
};
return sortedResult;
});
}
Loading

0 comments on commit bf2619a

Please sign in to comment.