@@ -10,18 +10,22 @@ const LIMIT = 20;
1010// Force PREVIEW off by setting false in .env
1111// Will show for vercel previews unless forced to false
1212export const preview = env . PREVIEW === "false" ? false : env . VERCEL_ENV === "preview" || import . meta. env . DEV ;
13+ const prod = import . meta. env . PROD ;
14+ const prefix = prod ? '../entries/pages/' : '../../routes/' ;
15+ const suffix = prod ? '_page.md.js' : '+page.md' ;
1316
1417// While developing locally this allows you to see pages without setting up firebase.
1518export const allowLocal = env . PREVIEW === "false" ? false : import . meta. env . DEV ;
1619
1720export const getRootPath = ( contentType : ContentType , courseDir ?: string ) => {
21+
1822 // Normal Files
19- let root = fileURLToPath ( new URL ( `../../routes/ (content-single)/(non-course)/${ contentType } ` , import . meta. url ) ) ;
23+ let root = fileURLToPath ( new URL ( `${ prefix } (content-single)/(non-course)/${ contentType } ` , import . meta. url ) ) ;
2024 if ( contentType === ContentType . course ) {
21- root = fileURLToPath ( new URL ( `../../routes/ (content-single)/${ contentType } ` , import . meta. url ) ) ;
25+ root = fileURLToPath ( new URL ( `${ prefix } (content-single)/${ contentType } ` , import . meta. url ) ) ;
2226 }
2327 if ( contentType === ContentType . lesson && courseDir ) {
24- root = fileURLToPath ( new URL ( `../../routes/ (content-single)/course/${ courseDir } /${ contentType } ` , import . meta. url ) ) ;
28+ root = fileURLToPath ( new URL ( `${ prefix } (content-single)/course/${ courseDir } /${ contentType } ` , import . meta. url ) ) ;
2529 }
2630 return root ;
2731}
@@ -31,17 +35,35 @@ export const getContentTypeDirectory = async <T>(contentType: ContentType, withC
3135 const dirs = opendirSync ( getRootPath ( contentType , courseDir ) ) ;
3236 for await ( const dir of dirs ) {
3337 if ( dir . isFile ( ) ) continue ;
34- const parsed = await parseContentType < T > ( `${ getRootPath ( contentType , courseDir ) } /${ dir . name } /+page.md ` , withCode ) as T ;
38+ const parsed = await parseContentType < T > ( `${ getRootPath ( contentType , courseDir ) } /${ dir . name } /${ suffix } ` , withCode ) as T ;
3539 contentList . push ( parsed ) ;
3640 }
3741 return contentList ;
3842}
3943
44+ export const getContentTypePath = async < T > ( contentType : ContentType , path : string , withCode = true , courseDir ?: string ) => {
45+ const root = getRootPath ( contentType , courseDir ) ;
46+ return await parseContentType < T > ( `${ root } /${ path } /${ suffix } ` , withCode ) as T ;
47+ }
48+
4049export const parseContentType = ( async < T > ( path : string , withCode = true ) => {
41- const md = readFileSync ( path , 'utf8' ) ;
42- const transformed = await compile ( md ) ;
43- const frontmatter = transformed ?. data ?. fm as Content & Podcast | undefined ;
50+ // If we are in production everything has already been compiled to JavaScript
51+ // and we can just read the metadata, otherwise compile and read.
52+ // let frontmatter;
53+ // let html;
54+ // if (prod) {
55+ console . log ( 'IMPORT PATH' , path ) ;
4456
57+ const { metadata, default : Page } = await import ( path ) ;
58+ const frontmatter = metadata ;
59+ const html = Page ?. render ( ) ?. html ;
60+ // } else {
61+ // console.log('READ PATH', path);
62+ // const md = readFileSync(path, 'utf8');
63+ // const transformed = await compile(md);
64+ // html = transformed?.code;
65+ // frontmatter = transformed?.data?.fm as Content & Podcast | undefined;
66+ // }
4567 // TODO: Add more checks?
4668
4769 if ( ! frontmatter ?. type ) {
@@ -53,7 +75,7 @@ export const parseContentType = (async <T>(path: string, withCode = true) => {
5375 ...frontmatter ,
5476 cover : frontmatter ?. cover ? decodeURI ( frontmatter ?. cover ) : '' ,
5577 type : frontmatter ?. type as ContentType ,
56- html : withCode ? transformed ?. code : undefined ,
78+ html : withCode ? html : undefined ,
5779 weight : frontmatter ?. weight ? frontmatter ?. weight : 0 ,
5880 published : frontmatter ?. published ? frontmatter ?. published : ContentPublished . draft ,
5981 start : frontmatter ?. start ? new Date ( frontmatter ?. start ) : new Date ( 'Jan 01, 2000' ) ,
0 commit comments