Skip to content

Commit 32192a3

Browse files
committed
working main content
1 parent 18e3497 commit 32192a3

24 files changed

Lines changed: 438 additions & 191 deletions

File tree

‎apps/codingcatdev/src/lib/components/content/Video.svelte‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
var match = url.match(regExp);
88
return match && match[1].length == 11 ? match[1] : false;
99
};
10-
console.log(src);
1110
$: finalSrc = `https://www.youtube.com/embed/${youtubeParser(src)}`;
1211
</script>
1312

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

Lines changed: 58 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,30 @@ export const preview = env.PREVIEW === "false" ? false : env.VERCEL_ENV === "pre
1414
// While developing locally this allows you to see pages without setting up firebase.
1515
export const allowLocal = env.PREVIEW === "false" ? false : import.meta.env.DEV;
1616

17-
export const getContentTypeDirectory = async <T>(contentType: ContentType, withCode = true) => {
18-
const contentList: T[] = [];
19-
17+
export const getRootPath = (contentType: ContentType, courseDir?: string) => {
2018
// Normal Files
2119
let root = fileURLToPath(new URL(`../../routes/(content-single)/(non-course)/${contentType}`, import.meta.url));
2220
if (contentType === ContentType.course) {
2321
root = fileURLToPath(new URL(`../../routes/(content-single)/${contentType}`, import.meta.url));
2422
}
25-
const dirs = opendirSync(root);
23+
if (contentType === ContentType.lesson && courseDir) {
24+
root = fileURLToPath(new URL(`../../routes/(content-single)/course/${courseDir}/${contentType}`, import.meta.url));
25+
}
26+
return root;
27+
}
28+
29+
export const getContentTypeDirectory = async <T>(contentType: ContentType, withCode = true, courseDir?: string) => {
30+
const contentList: T[] = [];
31+
const dirs = opendirSync(getRootPath(contentType, courseDir));
2632
for await (const dir of dirs) {
27-
const parsed = await parseContentType(`${root}/${dir.name}/+page.md`, withCode) as T;
33+
if (dir.isFile()) continue;
34+
const parsed = await parseContentType<T>(`${getRootPath(contentType, courseDir)}/${dir.name}/+page.md`, withCode) as T;
2835
contentList.push(parsed);
2936
}
3037
return contentList;
3138
}
3239

33-
export const parseContentType = (async (path: string, withCode = true) => {
40+
export const parseContentType = (async <T>(path: string, withCode = true) => {
3441
const md = readFileSync(path, 'utf8');
3542
const transformed = await compile(md);
3643
const frontmatter = transformed?.data?.fm as Content & Podcast | undefined;
@@ -42,111 +49,26 @@ export const parseContentType = (async (path: string, withCode = true) => {
4249
return;
4350
}
4451

45-
return {
52+
const content = {
4653
...frontmatter,
4754
cover: frontmatter?.cover ? decodeURI(frontmatter?.cover) : '',
4855
type: frontmatter?.type as ContentType,
4956
html: withCode ? transformed?.code : undefined,
5057
weight: frontmatter?.weight ? frontmatter?.weight : 0,
5158
published: frontmatter?.published ? frontmatter?.published : ContentPublished.draft,
5259
start: frontmatter?.start ? new Date(frontmatter?.start) : new Date('Jan 01, 2000'),
60+
slug: path.split('/').at(-2)
5361
};
54-
})
55-
56-
// export const parseModules = async (modules: Record<string, () => Promise<unknown>>) => {
57-
// const contentList: Content[] = [];
58-
// for (const path in modules) {
59-
// await modules[path]().then((mod) => {
60-
// const splitPath = path.split('/');
61-
// const courseType = splitPath.at(-3);
62-
// const normalType = splitPath.at(-2);
63-
// const slug = splitPath.at(-1);
64-
// const type = courseType === 'content' ? normalType : courseType;
65-
66-
// if (courseType === 'content') {
67-
// console.log(`Precompiling: ${type}/${slug}`);
68-
// } else {
69-
// console.log(`Precompiling: ${type}/${normalType}`);
70-
// }
71-
72-
// if (!type || !slug) {
73-
// console.error('Missing name or type');
74-
// return;
75-
// }
7662

77-
// const mdsvx = mod as {
78-
// default: {
79-
// render: () => { html: string };
80-
// };
81-
// metadata: Content;
82-
// };
83-
// const { html } = mdsvx.default.render();
84-
// /**
85-
// * This needs to match the function that adds the
86-
// * same data to Firestore
87-
// */
88-
// const content = {
89-
// ...mdsvx?.metadata,
90-
// cover: mdsvx?.metadata?.cover ? decodeURI(mdsvx?.metadata?.cover) : '',
91-
// type: type as ContentType,
92-
// html,
93-
// weight: mdsvx?.metadata?.weight ? mdsvx?.metadata?.weight : 0,
94-
// published: mdsvx?.metadata?.published ? mdsvx?.metadata?.published : ContentPublished.draft,
95-
// start: mdsvx?.metadata?.start ? new Date(mdsvx?.metadata?.start) : new Date('Jan 01, 1900'),
96-
// };
97-
// contentList.push(content);
98-
// });
99-
// }
100-
// return contentList;
101-
// }
102-
103-
// export const parseLessonModules = async ({ lessonModules, courses }: { lessonModules: Record<string, () => Promise<unknown>>, courses: Course[] }) => {
104-
// for (const path in lessonModules) {
105-
// await lessonModules[path]().then((mod) => {
106-
// const splitPath = path.split('/');
107-
// const type = splitPath.at(-4);
108-
// const slug = splitPath.at(-3);
109-
// const lessonSlug = splitPath?.at(-1)?.replace(/\.[^/.]+$/, '');
110-
111-
// console.log(`Precompiling Lesson: ${type}/${slug}/lesson/${lessonSlug}`);
112-
113-
// if (!type || !slug || !lessonSlug) {
114-
// console.error('Lesson Param missing');
115-
// return;
116-
// }
117-
118-
// const mdsvx = mod as {
119-
// default: {
120-
// render: () => { html: string };
121-
// };
122-
// metadata: Lesson;
123-
// };
124-
// const { html } = mdsvx.default.render();
125-
// /**
126-
// * This needs to match the function that adds the
127-
// * same data to Firestore
128-
// */
129-
// const content = {
130-
// ...mdsvx?.metadata,
131-
// cover: mdsvx?.metadata?.cover ? decodeURI(mdsvx?.metadata?.cover) : '',
132-
// type: ContentType.lesson,
133-
// courseSlug: slug,
134-
// html,
135-
// weight: mdsvx?.metadata?.weight ? mdsvx?.metadata?.weight : 0,
136-
// published: mdsvx?.metadata?.published ? mdsvx?.metadata?.published : ContentPublished.draft,
137-
// start: mdsvx?.metadata?.start ? new Date(mdsvx?.metadata?.start) : new Date('Jan 01, 1900'),
138-
// locked: mdsvx?.metadata?.locked || false,
139-
// };
140-
141-
// courses
142-
// .filter((c) => c.slug === slug)
143-
// .map((c) => {
144-
// c?.lesson ? c.lesson.push(content) : (c['lesson'] = [content]);
145-
// });
146-
// });
147-
// }
148-
// return courses;
149-
// }
63+
if (frontmatter.type === ContentType.course) {
64+
const lesson = (await listContent<Content>({
65+
contentItems: await getContentTypeDirectory<Content>(ContentType.lesson, false, frontmatter.slug),
66+
limit: 10000
67+
})).content
68+
return { ...content, lesson } as T;
69+
}
70+
return { ...content, } as T;
71+
})
15072

15173

15274
/**
@@ -157,25 +79,15 @@ export const listContent = async <T extends Content>({
15779
contentItems,
15880
after,
15981
limit,
160-
contentFilter = (c) => c.published === ContentPublished.published
16182
}: {
16283
contentItems: T[]
16384
after?: number;
16485
limit?: number;
165-
contentFilter?: (c: Content) => boolean;
16686
}) => {
16787
const theLimit = limit || LIMIT;
16888
const theAfter = after || 0;
16989

170-
console.log(`List limit of ${theLimit}`);
171-
172-
for (const c of contentItems) {
173-
console.log(c?.title)
174-
}
175-
176-
const fullContent = contentItems
177-
.filter(preview ? () => true : contentFilter)
178-
.sort((a, b) => new Date(b.start).valueOf() - new Date(a.start).valueOf());
90+
const fullContent = await filterContent<T>({ contentItems })
17991

18092
const content = fullContent.slice(0 + theAfter, theLimit + theAfter);
18193
const total = fullContent.length;
@@ -186,6 +98,39 @@ export const listContent = async <T extends Content>({
18698
};
18799
};
188100

101+
102+
export const filterContent = async <T extends Content>({
103+
contentItems,
104+
}: {
105+
contentItems: T[];
106+
}) => {
107+
const doc = contentItems
108+
.filter(
109+
preview ?
110+
() => true
111+
:
112+
(c) => new Date(c.start) <= new Date() &&
113+
c.published === ContentPublished.published
114+
)
115+
.sort((a, b) => new Date(b.start).valueOf() - new Date(a.start).valueOf())
116+
.map((c: Course) => {
117+
return {
118+
...c,
119+
lesson: c?.lesson
120+
?.filter(
121+
preview ?
122+
() => true
123+
:
124+
(l) => new Date(l.start) <= new Date() && l.published === ContentPublished.published
125+
)
126+
.sort((a, b) => a.weight && b.weight ? a.weight - b.weight : -1)
127+
};
128+
}) as unknown as T[];
129+
130+
return [...doc];
131+
};
132+
133+
189134
/**
190135
* List all content from specified content type by author
191136
* */
@@ -236,49 +181,6 @@ export const listContentBySponsor = async ({ sponsorSlug, contentItems }:
236181
];
237182
};
238183

239-
export const getContentBySlug = async ({
240-
contentItems,
241-
slug
242-
}: {
243-
contentItems: Content[];
244-
slug: string
245-
}) => {
246-
console.debug(`Searching for slug: ${slug} in ${contentItems.length} items`);
247-
248-
const doc = contentItems
249-
.filter(
250-
preview ?
251-
(c) =>
252-
c.slug == slug
253-
:
254-
(c) =>
255-
c.slug == slug &&
256-
new Date(c.start) <= new Date() &&
257-
c.published === ContentPublished.published
258-
)
259-
.sort((a, b) => new Date(b.start).valueOf() - new Date(a.start).valueOf())
260-
.slice(0, 1)
261-
.map((c: Course) => {
262-
return {
263-
...c,
264-
lesson: c?.lesson
265-
?.filter(
266-
preview ?
267-
() => true
268-
:
269-
(l) => new Date(l.start) <= new Date() && l.published === ContentPublished.published
270-
)
271-
.sort((a, b) => a.weight && b.weight ? a.weight - b.weight : -1)
272-
};
273-
})
274-
.at(0);
275-
if (!doc) {
276-
return null;
277-
}
278-
return {
279-
...doc
280-
};
281-
};
282184

283185
/**
284186
* Get lesson by course and slug

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

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
1-
import { compile } from 'mdsvex';
2-
import { readFileSync } from 'fs';
31
import { fileURLToPath } from 'url';
42
import { error } from '@sveltejs/kit';
5-
import type { Content, Podcast } from '$lib/types';
6-
7-
export const prerender = true;
3+
import { filterContent, parseContentType } from '$lib/server/content';
4+
import type { Content } from '$lib/types';
85

96
export const load = (async (params) => {
107
try {
11-
const file = fileURLToPath(new URL(`./${params.url.pathname}/+page.md`, import.meta.url));
12-
const md = readFileSync(file, 'utf8');
13-
const transformed = await compile(md);
14-
const content = transformed?.data?.fm as Content & Podcast | undefined;
15-
const start = content?.start && new Date(content.start);
16-
if (start && start < new Date()) {
17-
return {
18-
content,
19-
course: [],
20-
tutorial: [],
21-
podcast: [],
22-
post: []
23-
}
24-
} else {
25-
throw error(404);
8+
const path = fileURLToPath(new URL(`./${params.url.pathname}/+page.md`, import.meta.url));
9+
const md = await parseContentType<Content>(path);
10+
if (!md) throw error(404);
11+
12+
const contentItems = await filterContent({ contentItems: [md] })
13+
const content = contentItems?.at(0);
14+
if (!content) throw error(404);
15+
return {
16+
content,
17+
course: [],
18+
tutorial: [],
19+
podcast: [],
20+
post: []
2621
}
22+
2723
}
2824
catch (e) {
2925
console.error(e)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { fileURLToPath } from 'url';
2+
import { error } from '@sveltejs/kit';
3+
import { filterContent, getContentTypeDirectory, listContent, parseContentType } from '$lib/server/content';
4+
import { ContentType, type Course, type Author } from '$lib/types';
5+
6+
export const load = (async (params) => {
7+
try {
8+
const splitPath = params.url.pathname.split('/');
9+
const courseSlug = splitPath.at(2);
10+
const lessonSlug = splitPath.at(4);
11+
12+
const path = fileURLToPath(new URL(`./${courseSlug}/+page.md`, import.meta.url));
13+
const md = await parseContentType<Course>(path);
14+
if (!md) throw error(404);
15+
const contentItems = await filterContent({ contentItems: [md] })
16+
const course = contentItems?.at(0);
17+
if (!course) throw error(404);
18+
19+
const authors = (await listContent<Author>({
20+
contentItems: await getContentTypeDirectory<Author>(ContentType.author)
21+
}))?.content
22+
23+
return {
24+
content: course.lesson?.find(l => l.slug === lessonSlug),
25+
course,
26+
authors
27+
}
28+
29+
}
30+
catch (e) {
31+
console.error(e)
32+
throw error(404)
33+
}
34+
})

0 commit comments

Comments
 (0)