@@ -10,18 +10,22 @@ const LIMIT = 20;
10
10
// Force PREVIEW off by setting false in .env
11
11
// Will show for vercel previews unless forced to false
12
12
export 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' ;
13
16
14
17
// While developing locally this allows you to see pages without setting up firebase.
15
18
export const allowLocal = env . PREVIEW === "false" ? false : import . meta. env . DEV ;
16
19
17
20
export const getRootPath = ( contentType : ContentType , courseDir ?: string ) => {
21
+
18
22
// 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 ) ) ;
20
24
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 ) ) ;
22
26
}
23
27
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 ) ) ;
25
29
}
26
30
return root ;
27
31
}
@@ -31,17 +35,35 @@ export const getContentTypeDirectory = async <T>(contentType: ContentType, withC
31
35
const dirs = opendirSync ( getRootPath ( contentType , courseDir ) ) ;
32
36
for await ( const dir of dirs ) {
33
37
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 ;
35
39
contentList . push ( parsed ) ;
36
40
}
37
41
return contentList ;
38
42
}
39
43
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
+
40
49
export 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 ) ;
44
56
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
+ // }
45
67
// TODO: Add more checks?
46
68
47
69
if ( ! frontmatter ?. type ) {
@@ -53,7 +75,7 @@ export const parseContentType = (async <T>(path: string, withCode = true) => {
53
75
...frontmatter ,
54
76
cover : frontmatter ?. cover ? decodeURI ( frontmatter ?. cover ) : '' ,
55
77
type : frontmatter ?. type as ContentType ,
56
- html : withCode ? transformed ?. code : undefined ,
78
+ html : withCode ? html : undefined ,
57
79
weight : frontmatter ?. weight ? frontmatter ?. weight : 0 ,
58
80
published : frontmatter ?. published ? frontmatter ?. published : ContentPublished . draft ,
59
81
start : frontmatter ?. start ? new Date ( frontmatter ?. start ) : new Date ( 'Jan 01, 2000' ) ,
0 commit comments