Skip to content

Commit f1bcd4e

Browse files
committed
fix(courses): better typing
1 parent 8b777bb commit f1bcd4e

File tree

7 files changed

+32
-18
lines changed

7 files changed

+32
-18
lines changed

src/data/courses.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
import type { MarkdownInstance } from "astro"
12
import type { BaseFrontmatter } from "./shared"
23

34
export interface Course extends BaseFrontmatter {
4-
type: string
5+
type: "SKILL" | "ENGINE"
56
short_title: string
67
date: string
78
author: string
8-
medal: string
9+
medal: "SILVER" | "BRONZE" | "GOLD" | "PLATINIUM"
910
medal_message: string
1011
cover_wide: string
1112
cover_tall: string
1213
}
14+
15+
export type CourseInstance = MarkdownInstance<Course>

src/layouts/AppLayout.astro

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import Header from "../components/header/Header.astro"
55
export interface Props {
66
title: string
77
}
8-
98
const { title } = Astro.props
109
---
1110

src/layouts/ContentLayout.astro

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
---
2-
import type { Course } from "../data/courses"
2+
import type { CourseInstance } from "../data/courses"
33
import AppLayout from "./AppLayout.astro"
44
5+
interface Props {
6+
content: CourseInstance
7+
}
58
const { content } = Astro.props
6-
const frontmatter: Course = content.frontmatter
79
---
810

9-
<AppLayout title={frontmatter.title}>
11+
<AppLayout title={content.frontmatter.title}>
1012
<main class="max-w-screen-md mx-auto px-4 py-2">
1113
<h2>
12-
{frontmatter.title}
14+
{content.frontmatter.title}
1315
</h2>
1416
<content.Content />
1517
</main>

src/layouts/MainLayout.astro

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
---
22
import AppLayout from "./AppLayout.astro"
33
4+
interface Props {
5+
title: string
6+
}
47
const { title } = Astro.props
58
---
69

src/pages/[course].astro

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
---
2+
import type { Course, CourseInstance } from "../data/courses"
23
import ContentLayout from "../layouts/ContentLayout.astro"
34
import { getSlugFromPath } from "../utils"
45
6+
interface Props {
7+
course: CourseInstance
8+
}
9+
const { course } = Astro.props
10+
511
export async function getStaticPaths() {
6-
const courses = await Astro.glob("../../content/**/index.md")
12+
const courses = await Astro.glob<Course>("../../content/**/index.md")
713
814
return courses.map((course) => ({
915
params: {
10-
course: getSlugFromPath(course.file, "course"),
16+
course: getSlugFromPath(course.file, "Course"),
1117
},
1218
props: {
13-
course: { ...course, frontmatter: course.frontmatter },
19+
course: { ...course },
1420
},
1521
}))
1622
}
17-
18-
const { course } = Astro.props
1923
---
2024

2125
<ContentLayout content={course} />

src/pages/index.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const courses = await Astro.glob<Course>("../../content/**/index.md")
1212
{
1313
courses.map((course) => (
1414
<li>
15-
<a href={"/" + getSlugFromPath(course.file, "course")}>
15+
<a href={"/" + getSlugFromPath(course.file, "Course")}>
1616
{course.frontmatter.title}
1717
</a>
1818
</li>

src/utils/index.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@
44
* @param contentType what's the type of content ?
55
* @returns
66
*/
7-
export function getSlugFromPath(path: String, contentType: String): String {
7+
export function getSlugFromPath(
8+
path: string,
9+
contentType: contentType
10+
): string {
811
switch (contentType) {
9-
case "course":
12+
case "Course":
1013
return path.substring(
1114
// there is probably a smarter way
1215
path.indexOf("content/") + 8,
1316
path.indexOf("/index.md")
1417
)
1518
default:
16-
throw new Error(
17-
`contentType from getSlugFromPath() is invalid: ${contentType}`
18-
)
19+
throw new Error(`contentType not yet implemented: ${contentType}`)
1920
}
2021
}
22+
23+
type contentType = "Course" | "Chapter" | "Page"

0 commit comments

Comments
 (0)