1
+// Use direct imports from collections to avoid top-level await in lib/source.ts.
2
+import { existsSync, readdirSync, readFileSync } from 'node:fs';
3
+import { join, relative } from 'node:path';
4
+import { docs, reference, cookbooks, toolkits } from 'fumadocs-mdx:collections/server';
5
+import { loader, multiple } from 'fumadocs-core/source';
6
+import { lucideIconsPlugin } from 'fumadocs-core/source/lucide-icons';
7
+import { openapiSource, openapiPlugin } from 'fumadocs-openapi/server';
8
+import type { AdvancedIndex } from 'fumadocs-core/search/server';
9
+import type { DocumentRecord } from 'fumadocs-core/search/algolia';
10
+import { openapi } from '@/lib/openapi';
11
+import { getAllToolkitsSync } from '@/lib/toolkit-data';
12
+
13
+// Create loaders directly here to avoid the problematic lib/source.ts import.
14
+const docsSource = loader({
15
+ baseUrl: '/docs',
16
+ source: docs.toFumadocsSource(),
17
+ plugins: [lucideIconsPlugin()],
18
+});
19
+
20
+const cookbooksSource = loader({
21
+ baseUrl: '/cookbooks',
22
+ source: cookbooks.toFumadocsSource(),
23
+ plugins: [lucideIconsPlugin()],
24
+});
25
+
26
+const toolkitsSource = loader({
27
+ baseUrl: '/toolkits',
28
+ source: toolkits.toFumadocsSource(),
29
+ plugins: [lucideIconsPlugin()],
30
+});
31
+
32
+// Dynamic toolkit entries from toolkits.json.
33
+const mdxToolkitSlugs = new Set(
34
+ toolkitsSource.getPages().map((page) => page.slugs.join('/')),
35
+);
36
+
37
+const dynamicToolkitIndexes = getAllToolkitsSync()
38
+ .filter((toolkit) => !mdxToolkitSlugs.has(toolkit.slug))
39
+ .map((toolkit) => ({
40
+ id: `/toolkits/${toolkit.slug}`,
41
+ title: toolkit.name,
42
+ description: toolkit.description,
43
+ url: `/toolkits/${toolkit.slug}`,
44
+ structuredData: { headings: [], contents: [] },
45
+ keywords: [toolkit.slug, toolkit.category].filter(Boolean) as string[],
46
+ } satisfies SearchIndex));
47
+
48
+function getFrontmatterValue(frontmatter: string, key: string): string | undefined {
49
+ const match = frontmatter.match(new RegExp(`^${key}:\\s*(?:"([^"]*)"|'([^']*)'|(.+))$`, 'm'));
50
+ const value = match?.[1] ?? match?.[2] ?? match?.[3];
51
+ return value?.trim();
52
+}
53
+
54
+function getChangelogIndexes(): SearchIndex[] {
55
+ const changelogDir = join(process.cwd(), 'content/changelog');
56
+ if (!existsSync(changelogDir)) return [];
57
+
58
+ return readdirSync(changelogDir)
59
+ .filter((file) => file.endsWith('.mdx'))
60
+ .flatMap((file) => {
61
+ const source = readFileSync(join(changelogDir, file), 'utf8');
62
+ const frontmatter = source.match(/^---\n([\s\S]*?)\n---/)?.[1];
63
+ if (!frontmatter) return [];
64
+
65
+ const date = getFrontmatterValue(frontmatter, 'date');
66
+ const title = getFrontmatterValue(frontmatter, 'title');
67
+ if (!date || !title) return [];
68
+
69
+ const url = `/docs/changelog/${date.replace(/-/g, '/')}`;
70
+
71
+ return [{
72
+ id: `${url}#${title}`,
73
+ title,
74
+ description: getFrontmatterValue(frontmatter, 'description') ?? '',
75
+ url,
76
+ structuredData: { headings: [], contents: [] },
77
+ keywords: ['changelog'],
78
+ } satisfies SearchIndex];
79
+ });
80
+}
81
+
82
+type SearchIndex = AdvancedIndex & {
83
+ keywords?: string[];
84
+};
85
+
86
+export async function getDocsSearchIndexes(): Promise<SearchIndex[]> {
87
+ // Load OpenAPI pages and build full reference source.
88
+ const openapiPages = await openapiSource(openapi, {
89
+ groupBy: 'tag',
90
+ baseDir: 'api-reference',
91
+ });
92
+
93
+ const fullReferenceSource = loader({
94
+ baseUrl: '/reference',
95
+ source: multiple({
96
+ mdx: reference.toFumadocsSource(),
97
+ openapi: openapiPages,
98
+ }),
99
+ plugins: [lucideIconsPlugin(), openapiPlugin()],
100
+ });
101
+
102
+ const mdxIndexes = [
103
+ ...docsSource.getPages(),
104
+ ...cookbooksSource.getPages(),
105
+ ...toolkitsSource.getPages(),
106
+ ...fullReferenceSource.getPages(),
107
+ ].map((page) => ({
108
+ id: page.url,
109
+ title: page.data.title ?? 'Untitled',
110
+ description: page.data.description,
111
+ url: page.url,
112
+ structuredData: page.data.structuredData,
113
+ keywords: 'keywords' in page.data ? (page.data.keywords as string[]) : undefined,
114
+ } satisfies SearchIndex));
115
+
116
+ return [...mdxIndexes, ...dynamicToolkitIndexes, ...getChangelogIndexes()];
117
+}
118
+
119
+function listContentFiles(dir: string): string[] {
120
+ if (!existsSync(dir)) return [];
121
+
122
+ return readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
123
+ const path = join(dir, entry.name);
124
+ if (entry.isDirectory()) return listContentFiles(path);
125
+ if (entry.isFile() && /\.mdx?$/.test(entry.name)) return [path];
126
+ return [];
127
+ });
128
+}
129
+
130
+function slugifyHeading(value: string): string {
131
+ return value
132
+ .toLowerCase()
133
+ .trim()
134
+ .replace(/[<>]/g, '')
135
+ .replace(/[`*_~\[\]()]/g, '')
136
+ .replace(/&[a-z]+;/g, '')
137
+ .replace(/[^\w\s-]/g, '')
138
+ .replace(/\s+/g, '-')
139
+ .replace(/-+/g, '-');
140
+}
141
+
142
+function stripMdx(value: string): string {
143
+ return value
144
+ .replace(/^---\n[\s\S]*?\n---\n?/, '')
145
+ .replace(/```[\s\S]*?```/g, '')
146
+ .replace(/[<>]/g, ' ')
147
+ .replace(/\{[^}]*\}/g, ' ')
148
+ .replace(/!\[[^\]]*\]\([^)]*\)/g, '')
149
+ .replace(/\[([^\]]+)\]\([^)]*\)/g, '$1')
150
+ .replace(/[`*_>#-]/g, ' ')
151
+ .replace(/\s+/g, ' ')
152
+ .trim();
153
+}
154
+
155
+function buildStructuredData(markdown: string) {
156
+ const body = markdown.replace(/^---\n[\s\S]*?\n---\n?/, '');
157
+ const headings: { content: string; id: string }[] = [];
158
+ const contents: { content: string; heading: string | undefined }[] = [];
159
+ const lines = body.split('\n');
160
+ let currentHeading: string | undefined;
161
+ let current: string[] = [];
162
+
163
+ function flush() {
164
+ const content = stripMdx(current.join('\n'));
165
+ if (content) contents.push({ content, heading: currentHeading });
166
+ current = [];
167
+ }
168
+
169
+ for (const line of lines) {
170
+ const match = line.match(/^(#{2,6})\s+(.+)$/);
171
+ if (match) {
172
+ flush();
173
+ const content = stripMdx(match[2] ?? '');
174
+ if (content) {
175
+ const id = slugifyHeading(content);
176
+ headings.push({ content, id });
177
+ currentHeading = id;
178
+ }
179
+ continue;
180
+ }
181
+
182
+ current.push(line);
183
+ }
184
+
185
+ flush();
186
+ if (contents.length === 0) {
187
+ const content = stripMdx(body);
188
+ if (content) contents.push({ content, heading: undefined });
189
+ }
190
+
191
+ return { headings, contents };
192
+}
193
+
194
+function urlFromContentPath(path: string): string | undefined {
195
+ const rel = relative(join(process.cwd(), 'content'), path).replace(/\\/g, '/');
196
+ const withoutExt = rel.replace(/\.mdx?$/, '');
197
+ const parts = withoutExt.split('/');
198
+ const collection = parts.shift();
199
+ if (!collection) return undefined;
200
+
201
+ if (collection === 'docs') return `/docs/${parts.join('/')}`.replace(/\/index$/, '');
202
+ if (collection === 'cookbooks') return `/cookbooks/${parts.join('/')}`.replace(/\/index$/, '');
203
+ if (collection === 'reference') return `/reference/${parts.join('/')}`.replace(/\/index$/, '');
204
+ if (collection === 'toolkits') {
205
+ if (parts[0] === 'faq') return undefined;
206
+ return `/toolkits/${parts.join('/')}`.replace(/\/index$/, '');
207
+ }
208
+ if (collection === 'changelog') return undefined;
209
+
210
+ return undefined;
211
+}
212
+
213
+function getFilesystemIndexes(): SearchIndex[] {
214
+ const contentDir = join(process.cwd(), 'content');
215
+ return listContentFiles(contentDir).flatMap((file) => {
216
+ const url = urlFromContentPath(file);
217
+ if (!url) return [];
218
+
219
+ const source = readFileSync(file, 'utf8');
220
+ const frontmatter = source.match(/^---\n([\s\S]*?)\n---/)?.[1] ?? '';
221
+ const title = getFrontmatterValue(frontmatter, 'title');
222
+ if (!title) return [];
223
+
224
+ return [{
225
+ id: url,
226
+ title,
227
+ description: getFrontmatterValue(frontmatter, 'description'),
228
+ url,
229
+ structuredData: buildStructuredData(source),
230
+ keywords: getFrontmatterValue(frontmatter, 'keywords')?.split(',').map((keyword) => keyword.trim()),
231
+ } satisfies SearchIndex];
232
+ });
233
+}
234
+
235
+async function getOpenApiSearchIndexes(): Promise<SearchIndex[]> {
236
+ const openapiPages = await openapiSource(openapi, {
237
+ groupBy: 'tag',
238
+ baseDir: 'api-reference',
239
+ });
240
+
241
+ const openapiOnlySource = loader({
242
+ baseUrl: '/reference',
243
+ source: openapiPages,
244
+ plugins: [lucideIconsPlugin(), openapiPlugin()],
245
+ });
246
+
247
+ return openapiOnlySource.getPages().map((page) => ({
248
+ id: page.url,
249
+ title: page.data.title ?? 'Untitled',
250
+ description: page.data.description,
251
+ url: page.url,
252
+ structuredData: page.data.structuredData,
253
+ } satisfies SearchIndex));
254
+}
255
+
256
+function toAlgoliaDocument(index: SearchIndex): DocumentRecord {
257
+ const searchableKeywords = index.keywords?.filter(Boolean).join(' ');
258
+ const description = [index.description, searchableKeywords].filter(Boolean).join('\n');
259
+
260
+ return {
261
+ _id: index.id,
262
+ title: index.title,
263
+ description,
264
+ breadcrumbs: index.breadcrumbs,
265
+ url: index.url,
266
+ structured: index.structuredData,
267
+ tag: Array.isArray(index.tag) ? index.tag[0] : index.tag,
268
+ } satisfies DocumentRecord;
269
+}
270
+
271
+export async function getAlgoliaSearchDocuments(): Promise<DocumentRecord[]> {
272
+ const indexes = [
273
+ ...getFilesystemIndexes(),
274
+ ...dynamicToolkitIndexes,
275
+ ...getChangelogIndexes(),
276
+ ...await getOpenApiSearchIndexes(),
277
+ ];
278
+
279
+ const deduped = new Map<string, SearchIndex>();
280
+ for (const index of indexes) deduped.set(index.id, index);
281
+
282
+ return Array.from(deduped.values()).map(toAlgoliaDocument);
283
+}