Skip to content

Commit 1178e77

Browse files
committed
mdsvex working without prerender
1 parent 186cbcb commit 1178e77

File tree

4 files changed

+42
-18
lines changed

4 files changed

+42
-18
lines changed

apps/codingcatdev/src/lib/server/content.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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
1212
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';
1316

1417
// While developing locally this allows you to see pages without setting up firebase.
1518
export const allowLocal = env.PREVIEW === "false" ? false : import.meta.env.DEV;
1619

1720
export 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+
4049
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);
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'),

apps/codingcatdev/src/routes/(content-single)/(non-course)/+layout.server.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import { fileURLToPath } from 'url';
21
import { error } from '@sveltejs/kit';
3-
import { filterContent, getContentTypeDirectory, listContent, parseContentType } from '$lib/server/content';
2+
import { filterContent, getContentTypeDirectory, getContentTypePath, listContent } from '$lib/server/content';
43
import { ContentType, type Content, type Course } from '$lib/types';
54

65
export const load = (async (params) => {
76
try {
8-
const path = fileURLToPath(new URL(`./${params.url.pathname}/+page.md`, import.meta.url));
9-
const md = await parseContentType<Content>(path);
7+
const splitPath = params.url.pathname.split('/');
8+
const contentType = splitPath.at(1)
9+
const path = splitPath.at(2)
10+
11+
if (!contentType || !path) throw error(404);
12+
const md = await getContentTypePath<Content>(contentType as ContentType, path);
1013
if (!md) throw error(404);
1114

1215
const contentItems = await filterContent({ contentItems: [md] })

apps/codingcatdev/src/routes/(content-single)/course/+layout.server.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { fileURLToPath } from 'url';
21
import { error } from '@sveltejs/kit';
3-
import { filterContent, getContentTypeDirectory, listContent, parseContentType } from '$lib/server/content';
2+
import { filterContent, getContentTypeDirectory, getContentTypePath, listContent } from '$lib/server/content';
43
import { ContentType, type Course, type Author } from '$lib/types';
54

65
export const load = (async (params) => {
@@ -9,8 +8,9 @@ export const load = (async (params) => {
98
const courseSlug = splitPath.at(2);
109
const lessonSlug = splitPath.at(4);
1110

12-
const path = fileURLToPath(new URL(`./${courseSlug}/+page.md`, import.meta.url));
13-
const md = await parseContentType<Course>(path);
11+
if (!courseSlug) throw error(404);
12+
const md = await getContentTypePath<Course>(ContentType.course, courseSlug);
13+
1414
if (!md) throw error(404);
1515
const contentItems = await filterContent({ contentItems: [md] })
1616
const course = contentItems?.at(0);
@@ -25,7 +25,6 @@ export const load = (async (params) => {
2525
course,
2626
authors
2727
}
28-
2928
}
3029
catch (e) {
3130
console.error(e)

apps/codingcatdev/src/routes/+layout.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ccdValidateSessionCookie, validateStripeRole } from '$lib/server/firebase';
2-
export const prerender = true;
2+
export const prerender = false;
33
export const load = (async ({ cookies }) => {
44
try {
55

0 commit comments

Comments
 (0)