11import algosearch from 'algoliasearch' ;
22import { sync } from 'fumadocs-core/search-algolia/server' ;
3- import type { SearchIndex } from 'fumadocs-mdx' ;
3+ import type { Manifest } from 'fumadocs-mdx' ;
4+ import { createGetUrl , getSlugs , parseFilePath } from 'fumadocs-core/source' ;
5+ import path from 'node:path' ;
46
5- export async function updateSearchIndexes (
6- indexes : SearchIndex [ ] ,
7- ) : Promise < void > {
7+ export async function updateSearchIndexes ( manifest : Manifest ) : Promise < void > {
88 if ( ! process . env . ALGOLIA_API_KEY ) {
99 console . warn ( 'Algolia API Key not found, skip updating search index.' ) ;
1010 return ;
@@ -27,16 +27,29 @@ export async function updateSearchIndexes(
2727 } ,
2828 ) ;
2929
30+ const getUrl = createGetUrl ( '/docs' ) ;
31+
3032 await sync ( client , {
3133 document : process . env . NEXT_PUBLIC_ALGOLIA_INDEX ?? 'document' ,
32- documents : indexes . map ( ( docs ) => ( {
33- _id : docs . id ,
34- title : docs . title ,
35- description : docs . description ,
36- url : docs . url ,
37- structured : docs . structuredData ,
38- tag : docs . url . split ( '/' ) [ 2 ] ,
39- } ) ) ,
34+ documents : manifest . files
35+ . filter ( ( file ) => file . collection === 'docs' )
36+ . map ( ( docs ) => {
37+ const url = getUrl (
38+ getSlugs ( parseFilePath ( path . relative ( 'content/docs' , docs . path ) ) ) ,
39+ ) ;
40+
41+ if ( ! docs . data . structuredData )
42+ throw new Error ( '`structuredData` is required' ) ;
43+
44+ return {
45+ _id : docs . path ,
46+ title : docs . data . frontmatter . title as string ,
47+ description : docs . data . frontmatter . description as string ,
48+ url,
49+ structured : docs . data . structuredData ,
50+ tag : url . split ( '/' ) [ 2 ] ,
51+ } ;
52+ } ) ,
4053 } ) ;
4154
4255 console . log ( 'search updated' ) ;
0 commit comments