Skip to content

Commit

Permalink
new:Set Orama Search for doc
Browse files Browse the repository at this point in the history
  • Loading branch information
PaloMiku committed Nov 2, 2024
1 parent e133769 commit 9b6a89e
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 29 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ jobs:
run: |
bun install
bun run build
- name: Sync search index
run: |
bun run sync-search
- name: Add CNAME
run: |
echo "mx-space.js.org" > out/CNAME
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ yarn-error.log*
# others
.env*.local
.vercel
next-env.d.ts
next-env.d.ts
.env
44 changes: 39 additions & 5 deletions app/api/search/route.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,41 @@
import { source } from '@/lib/source';
import { createFromSource } from 'fumadocs-core/search/server';
import { sync } from 'fumadocs-core/search/orama-cloud';
import * as fs from 'node:fs/promises';
import { CloudManager } from '@oramacloud/client';

// it should be cached forever
export const revalidate = false;
export const dynamic = 'force-static';

export const { staticGET: GET } = createFromSource(source);
const updateSearchIndexes = async (): Promise<void> => {
const apiKey = process.env.ORAMA_PRIVATE_API_KEY;
const indexId = 'k2hnq39jnt7u8l41bfv0ezhd';

if (!apiKey) {
console.log('未找到 Orama 私钥, 跳过索引更新');
return;
}

try {
const content = await fs.readFile('.next/server/app/static.json.body');
const records = JSON.parse(content.toString());

const manager = new CloudManager({ api_key: apiKey });

await sync(manager, {
index: indexId,
documents: records,
});

console.log(`搜索索引更新完成: ${records.length} 条记录`);
} catch (error) {
console.error('更新搜索索引时发生错误:', error);
}
}

void updateSearchIndexes();

export async function GET(request: Request) {
// ... GET处理逻辑 ...
}

export async function POST(request: Request) {
// ... POST处理逻辑 ...
}
18 changes: 18 additions & 0 deletions app/components/provider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use client';
import { RootProvider } from 'fumadocs-ui/provider';
import dynamic from 'next/dynamic';
import type { ReactNode } from 'react';

const SearchDialog = dynamic(() => import('@/app/components/search')); // lazy load

export function Provider({ children }: { children: ReactNode }) {
return (
<RootProvider
search={{
SearchDialog,
}}
>
{children}
</RootProvider>
);
}
14 changes: 14 additions & 0 deletions app/components/search.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use client';

import { OramaClient } from '@oramacloud/client';
import type { SharedProps } from 'fumadocs-ui/components/dialog/search';
import SearchDialog from 'fumadocs-ui/components/dialog/search-orama';

const client = new OramaClient({
endpoint: 'https://cloud.orama.run/v1/indexes/mxspace-no50lj',
api_key: 'HHIpRwosmxFfAs7l2gsJv5m5A3ew2PRB',
});

export default function CustomSearchDialog(props: SharedProps) {
return <SearchDialog {...props} client={client} showOrama />;
}
12 changes: 6 additions & 6 deletions app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import './global.css';
import { RootProvider } from 'fumadocs-ui/provider';
import { Provider } from './components/provider';
import { Inter } from 'next/font/google';
import type { ReactNode } from 'react';

Expand All @@ -10,16 +10,16 @@ const inter = Inter({
export default function Layout({ children }: { children: ReactNode }) {
return (
<html lang="en" className={inter.className} suppressHydrationWarning>
<body className="flex flex-col min-h-screen">
<RootProvider
<body>
<Provider
search={{
options: {
type: 'static',
},
type: 'static'
}
}}
>
{children}
</RootProvider>
</Provider>
</body>
</html>
);
Expand Down
25 changes: 25 additions & 0 deletions app/static.json/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { NextResponse } from 'next/server';
import { source } from '@/lib/source';
import type { OramaDocument } from 'fumadocs-core/search/orama-cloud';

export const revalidate = false;

export async function GET(): Promise<Response> {
const pages = source.getPages();
const results = await Promise.all(
pages.map(async (page) => {
const { structuredData } = await page.data;

return {
id: page.url,
structured: structuredData,
tag: page.slugs[0],
url: page.url,
title: page.data.title,
description: page.data.description,
} satisfies OramaDocument;
}),
);

return NextResponse.json(results);
}
Binary file modified bun.lockb
Binary file not shown.
16 changes: 0 additions & 16 deletions components/ToGitHub.tsx

This file was deleted.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
"build": "next build",
"dev": "next dev --turbo",
"start": "next start",
"postinstall": "fumadocs-mdx"
"postinstall": "fumadocs-mdx",
"sync-search": "bun run sync-index.mjs"
},
"dependencies": {
"@orama/orama": "^3.0.1",
"@orama/react-components": "^0.1.11",
"@orama/tokenizers": "^3.0.1",
"@oramacloud/client": "^1.3.19",
"copy-to-clipboard": "^3.3.3",
"dotenv": "^16.4.5",
"framer-motion": "^11.11.11",
"fumadocs-core": "14.2.0",
"fumadocs-mdx": "11.1.1",
Expand Down
47 changes: 47 additions & 0 deletions sync-index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { sync } from 'fumadocs-core/search/orama-cloud';
import * as fs from 'node:fs/promises';
import { CloudManager } from '@oramacloud/client';
import * as path from 'path';
import * as dotenv from 'dotenv';

// 加载 .env 文件
dotenv.config();

export async function updateSearchIndexes() {
const apiKey = process.env.ORAMA_PRIVATE_API_KEY;
const indexId = 'k2hnq39jnt7u8l41bfv0ezhd'; // 你的索引 ID

if (!apiKey) {
console.log('未找到 Orama 私钥, 跳过索引更新');
return;
}

try {
// 使用绝对路径
const staticJsonPath = path.join(process.cwd(), '.next/server/app/static.json.body');

try {
await fs.access(staticJsonPath);
} catch {
console.error('static.json.body 文件不存在,请先运行 next build');
process.exit(1);
}

const content = await fs.readFile(staticJsonPath);
const records = JSON.parse(content.toString());

const manager = new CloudManager({ api_key: apiKey });

await sync(manager, {
index: indexId,
documents: records,
});

console.log(`搜索索引更新完成: ${records.length} 条记录`);
} catch (error) {
console.error('更新搜索索引时发生错误:', error);
process.exit(1);
}
}

void updateSearchIndexes();

0 comments on commit 9b6a89e

Please sign in to comment.