1+ const promisify = require ( 'util' ) . promisify ;
2+ const path = require ( 'path' ) ;
3+ const fs = require ( 'fs' ) ;
4+ const readdir = promisify ( fs . readdir ) ;
5+ const readFile = promisify ( fs . readFile ) ;
6+ const writeFile = promisify ( fs . writeFile ) ;
7+ const exists = promisify ( fs . exists ) ;
8+ const mkdir = promisify ( fs . mkdir ) ;
9+
10+ const contentDir = path . resolve ( '.' , 'content' ) ;
11+
12+ const getContentDir = dirPath => path . resolve ( contentDir , dirPath ) ;
13+
14+ const baseUrl = 'https://www.graphql-java.com' ;
15+
16+ const generateUrl = ( section , fileName ) =>
17+ [ baseUrl , section , fileName ] . join ( '/' ) . replace ( '.md' , '' ) ;
18+
19+ const extractTitle = content => {
20+ const regex = / t i t l e .* \" ( .* ) \" / ;
21+
22+ const match = regex . exec ( content ) ;
23+
24+ if ( match ) {
25+ return match [ 1 ] ;
26+ }
27+
28+ return '' ;
29+ } ;
30+
31+ const removeMetadata = content =>
32+ content . replace ( / [ \- \+ ] { 3 } .* [ \- \+ ] { 3 } / s, '' ) ;
33+
34+ const truncate = content => {
35+ const MAX_CONTENT_LENGTH = 1000 ;
36+ return content
37+ . substring ( 0 , MAX_CONTENT_LENGTH ) ;
38+ } ;
39+
40+ const indexBlog = async ( ) => {
41+ const indexedBlogs = await indexDir ( 'blog' )
42+
43+ return indexedBlogs . map ( indexedBlog => ( {
44+ ...indexedBlog ,
45+ category : 'Blogs'
46+ } ) ) ;
47+ }
48+
49+ const indexDocs = async ( ) => {
50+ const docContentDir = getContentDir ( 'documentation' ) ;
51+
52+ const docDirs = await readdir ( docContentDir )
53+
54+ const promises = docDirs . map ( docDir => {
55+ if ( fs . statSync ( path . resolve ( docContentDir , docDir ) ) . isDirectory ( ) ) {
56+ return indexDir ( `documentation/${ docDir } ` ) ;
57+ }
58+ } ) ;
59+
60+ const indexedDirs = await Promise . all ( promises ) ;
61+
62+ return indexedDirs
63+ . filter ( Boolean )
64+ . reduce ( ( acc , indexedDocs ) => acc . concat ( indexedDocs ) )
65+ . map ( indexedDoc => ( {
66+ ...indexedDoc ,
67+ category : 'Documentation'
68+ } ) ) ;
69+ }
70+
71+ const execute = async ( ) => {
72+ const OUTPUT_DIR = path . resolve ( '.' , 'dist' ) ;
73+
74+ const indexedBlog = await indexBlog ( ) ;
75+ const indexedDocs = await indexDocs ( ) ;
76+ const indexedAll = indexedBlog . concat ( indexedDocs ) ;
77+
78+ if ( ! ( await exists ( OUTPUT_DIR ) ) ) {
79+ await mkdir ( OUTPUT_DIR ) ;
80+ }
81+
82+ await writeFile ( path . resolve ( OUTPUT_DIR , 'algolia-index.json' ) , JSON . stringify ( indexedAll ) ) ;
83+ }
84+
85+ const indexDir = async dirPath => {
86+ const contentDir = getContentDir ( dirPath )
87+ const fileNames = await readdir ( contentDir ) ;
88+
89+ const filePromises = fileNames
90+ . map ( name => ( {
91+ name,
92+ path : path . resolve ( contentDir , name ) ,
93+ url : generateUrl ( dirPath , name )
94+ } ) )
95+ . map ( async fileObj => ( {
96+ ...fileObj ,
97+ rawContent : await readFile ( fileObj . path , 'utf8' )
98+ }
99+ ) ) ;
100+
101+ let fileObjects = await Promise . all ( filePromises ) ;
102+
103+ return fileObjects . map ( ( { name, path, url, rawContent } ) => ( {
104+ name,
105+ path,
106+ url,
107+ title : extractTitle ( rawContent ) ,
108+ content : truncate ( removeMetadata ( rawContent ) )
109+ } ) ) ;
110+ }
111+
112+ execute ( ) ;
0 commit comments