11import 'core-js/full/array/from-async' ;
22
3- import Router , { RouterParamContext } from '@koa/router' ;
43import { Context , Middleware } from 'koa' ;
54import { HTTPError } from 'koajax' ;
65import { DataObject } from 'mobx-restful' ;
7- import { KoaOption , withKoa , withKoaRouter } from 'next-ssr-middleware' ;
6+ import { KoaOption , withKoa } from 'next-ssr-middleware' ;
7+ import Path from 'path' ;
88import { ProxyAgent , setGlobalDispatcher } from 'undici' ;
9- import { parse } from 'yaml' ;
9+ import YAML from 'yaml' ;
1010
1111const { HTTP_PROXY } = process . env ;
1212
@@ -46,11 +46,6 @@ export const safeAPI: Middleware<any, any> = async (context: Context, next) => {
4646export const withSafeKoa = < S , C > ( ...middlewares : Middleware < S , C > [ ] ) =>
4747 withKoa < S , C > ( { } as KoaOption , safeAPI , ...middlewares ) ;
4848
49- export const withSafeKoaRouter = < S , C extends RouterParamContext < S > > (
50- router : Router < S , C > ,
51- ...middlewares : Middleware < S , C > [ ]
52- ) => withKoaRouter < S , C > ( { } as KoaOption , router , safeAPI , ...middlewares ) ;
53-
5449export interface ArticleMeta {
5550 name : string ;
5651 path ?: string ;
@@ -68,7 +63,7 @@ export function splitFrontMatter(raw: string) {
6863 if ( ! frontMatter ) return { markdown : raw } ;
6964
7065 try {
71- const meta = parse ( frontMatter ) as DataObject ;
66+ const meta = YAML . parse ( frontMatter ) as DataObject ;
7267
7368 return { markdown, meta } ;
7469 } catch ( error ) {
@@ -90,25 +85,28 @@ export async function* pageListOf(path: string, prefix = 'pages'): AsyncGenerato
9085
9186 const isMDX = MDX_pattern . test ( name ) ;
9287
93- name = name . replace ( MDX_pattern , '' ) ;
88+ ( { name } = Path . parse ( name ) ) ;
9489 path = `${ path } /${ name } ` . replace ( new RegExp ( `^${ prefix } ` ) , '' ) ;
9590
96- if ( node . isFile ( ) && isMDX ) {
91+ if ( node . isFile ( ) ) {
9792 const article : ArticleMeta = { name, path, subs : [ ] } ;
9893
99- const file = await readFile ( ` ${ node . path } / ${ node . name } ` , 'utf-8' ) ;
100-
101- const { meta } = splitFrontMatter ( file ) ;
94+ if ( isMDX )
95+ try {
96+ const rawFile = await readFile ( ` ${ node . path } / ${ node . name } ` , { encoding : 'utf-8' } ) ;
10297
103- if ( meta ) article . meta = meta ;
98+ const { meta } = splitFrontMatter ( rawFile ) ;
10499
100+ if ( meta ) article . meta = meta ;
101+ } catch ( error ) {
102+ console . error ( `Error reading front matter for ${ node . path } /${ node . name } :` , error ) ;
103+ }
105104 yield article ;
106- }
107- if ( ! node . isDirectory ( ) ) continue ;
105+ } else if ( node . isDirectory ( ) ) {
106+ const subs = await Array . fromAsync ( pageListOf ( path , prefix ) ) ;
108107
109- const subs = await Array . fromAsync ( pageListOf ( path , prefix ) ) ;
110-
111- if ( subs . length ) yield { name, subs } ;
108+ if ( subs [ 0 ] ) yield { name, subs } ;
109+ }
112110 }
113111}
114112
0 commit comments