@@ -202,82 +202,62 @@ const relinkReferences = (md, pages, root) => {
202
202
relinkImageReferences ( imageReferences , definitions , pages , root ) ;
203
203
} ;
204
204
205
- export const importDocs = async ( filename , item ) => {
206
- const doc = fs . readFileSync ( ` ${ filename } ` , 'utf-8' ) ;
207
-
208
- const md = remark . parse ( doc ) ;
205
+ const createPage = async ( page , filename , item , hasMulitplePages , idx ) => {
206
+ if ( hasMulitplePages ) {
207
+ createDir ( ` ${ DOCSROOT } / ${ item . destination } ` ) ;
208
+ }
209
209
210
210
const lastModifiedDate = await getLastModifiedDate (
211
211
`.${ removeRepoDomain ( item . repo ) } ${ item . file } ` ,
212
212
) ;
213
213
214
- if ( item . options . singlePage ) {
215
- relinkReferences ( md , [ md ] , `/${ item . destination } /` ) ;
214
+ const title = getTitle ( page ) ;
215
+ const slug =
216
+ idx === 0 || item . options . RootOrder === 0 ? 'index' : createSlug ( title ) ;
217
+ const menuTitle =
218
+ idx === 0 || item . options . RootOrder === 0 ? item . title : title ;
219
+ const order = idx === 0 ? item . options . RootOrder : idx ? idx : 0 ;
220
+
221
+ // check that there is just 1 h1.
222
+ // if more, keep only 1 and replace the next with an h2
223
+ const pageContent = cleanUp (
224
+ page ,
225
+ `/${ item . destination } /${ order === 0 ? '' : slug } ` ,
226
+ ) ;
216
227
217
- const title = getTitle ( md ) ;
218
-
219
- const menuTitle = item . title ;
220
- const order = item . options . RootOrder ;
221
- const slug = createSlug ( title ) ;
222
-
223
- // check that there is just 1 h1.
224
- // if more, keep only 1 and replace the next with an h2
225
-
226
- const pageContent = cleanUp (
227
- md ,
228
- `/${ item . destination } /${ order === 0 ? '' : slug } ` ,
229
- ) ;
230
-
231
- const doc = toMarkdown ( pageContent ) ;
232
-
233
- fs . writeFileSync (
234
- `${ DOCSROOT } /${ item . destination } /${ order === 0 ? 'index' : slug } .md` ,
235
- createFrontMatter (
236
- title ,
237
- menuTitle ,
238
- order ,
239
- createEditOverwrite ( item ) ,
240
- item . options . tags ,
241
- lastModifiedDate ,
242
- ) + doc ,
243
- {
244
- flag : 'w' ,
245
- } ,
246
- ) ;
228
+ const doc = toMarkdown ( pageContent ) ;
229
+
230
+ fs . writeFileSync (
231
+ `${ DOCSROOT } /${ item . destination } /${ order === 0 ? 'index' : slug } .md` ,
232
+ createFrontMatter (
233
+ title ,
234
+ menuTitle ,
235
+ order ,
236
+ createEditOverwrite ( item ) ,
237
+ item . options . tags ,
238
+ lastModifiedDate ,
239
+ ) + doc ,
240
+ {
241
+ flag : 'w' ,
242
+ } ,
243
+ ) ;
244
+ } ;
247
245
246
+ export const importDocs = async ( filename , item ) => {
247
+ const doc = fs . readFileSync ( `${ filename } ` , 'utf-8' ) ;
248
+
249
+ const md = remark . parse ( doc ) ;
250
+
251
+ if ( item . options . singlePage ) {
252
+ relinkReferences ( md , [ md ] , `/${ item . destination } /` ) ;
253
+ await createPage ( md , filename , item ) ;
248
254
return ;
249
255
}
250
256
251
257
const pages = divideIntoPages ( md ) ;
252
258
relinkReferences ( md , pages , `/${ item . destination } /` ) ;
253
259
254
- pages . forEach ( ( page , idx ) => {
255
- const title = getTitle ( page ) ;
256
- const slug = idx === 0 ? 'index' : createSlug ( title ) ;
257
- const menuTitle = idx === 0 ? item . title : title ;
258
- const order = idx === 0 ? item . options . RootOrder : idx ;
259
-
260
- // check that there is just 1 h1.
261
- // if more, keep only 1 and replace the next with an h2
262
- const pageContent = cleanUp ( page , `/${ item . destination } /${ slug } ` ) ;
263
-
264
- const doc = toMarkdown ( pageContent ) ;
265
-
266
- createDir ( `${ DOCSROOT } /${ item . destination } ` ) ;
267
-
268
- fs . writeFileSync (
269
- `${ DOCSROOT } /${ item . destination } /${ slug } .md` ,
270
- createFrontMatter (
271
- title ,
272
- menuTitle ,
273
- order ,
274
- createEditOverwrite ( item ) ,
275
- item . options . tags ,
276
- lastModifiedDate ,
277
- ) + doc ,
278
- {
279
- flag : 'w' ,
280
- } ,
281
- ) ;
260
+ pages . forEach ( async ( page , idx ) => {
261
+ await createPage ( page , filename , item , true , idx ) ;
282
262
} ) ;
283
263
} ;
0 commit comments