-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(routing): wip getStaticPathFromContentType & wip Chapter
- Loading branch information
1 parent
3950641
commit 2440908
Showing
16 changed files
with
137 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"name": "@example/basics", | ||
"name": "fairedesjeux", | ||
"type": "module", | ||
"version": "0.0.1", | ||
"private": true, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import type { MarkdownInstance } from "astro" | ||
import type { BaseFrontmatter } from "./shared" | ||
|
||
export interface Chapter extends BaseFrontmatter {} | ||
|
||
export type ChapterInstance = MarkdownInstance<Chapter> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from "./chapters" | ||
export * from "./courses" | ||
export * from "./shared" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
import type { Chapter, ChapterInstance } from "$data" | ||
import { getStaticPathFromContentType } from "../glob.astro" | ||
interface Props { | ||
slug: ChapterInstance | ||
} | ||
const { content } = Astro.props | ||
export async function getStaticPaths() { | ||
return await getStaticPathFromContentType(Astro, "Chapter") | ||
} | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
import type { CourseInstance } from "$data" | ||
import { getStaticPathFromContentType } from "./glob.astro" | ||
import ContentLayout from "$layouts/ContentLayout.astro" | ||
interface Props { | ||
content: CourseInstance | ||
} | ||
const { content } = Astro.props | ||
export async function getStaticPaths() { | ||
return await getStaticPathFromContentType(Astro, "Course") | ||
} | ||
--- | ||
|
||
<ContentLayout content={content} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
import type { Chapter, ContentType, Course } from "$data" | ||
import { getSlugFromFilePath } from "$utils" | ||
import type { AstroGlobal } from "astro" | ||
/** | ||
* A standardized way to get all Static Paths of a given content type. | ||
* ============================================== | ||
* Temporarily moved from `src/utils/glob.ts`, | ||
* see this issue -> https://github.com/withastro/astro/issues/5552 | ||
* @param Astro needed to use Astro.glob<t> inside a TypeScript file | ||
* @param contentType what's the type of content needed ? | ||
* @returns A promise of an array with all Static Paths | ||
*/ | ||
export async function getStaticPathFromContentType( | ||
Astro: Readonly<AstroGlobal>, | ||
contentType: ContentType | ||
) { | ||
let contents: Array<any> = [] | ||
switch (contentType) { | ||
case "Course": | ||
contents = await Astro.glob<Course>("../../content/**/index.md") | ||
break | ||
case "Chapter": | ||
contents = await Astro.glob<Chapter>("../../content/**/**/chapter.md") | ||
case "Page": | ||
contents = await Astro.glob<Chapter>("../../content/**/**/*.md") | ||
default: | ||
throw new Error( | ||
`[getStaticPathFromContentType] contentType not yet implemented: ${contentType}` | ||
) | ||
} | ||
return contents.map((content) => ({ | ||
params: { | ||
slug: getSlugFromFilePath(content.file, contentType), | ||
}, | ||
props: { | ||
content: { ...content }, | ||
}, | ||
})) | ||
} | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import type { ContentType } from "$data" | ||
|
||
/** | ||
* Get content's slug from a content file path | ||
* @param path where is the content file ? | ||
* @param contentType what's the type of content ? | ||
* @returns | ||
*/ | ||
export function getSlugFromFilePath( | ||
path: string, | ||
contentType: ContentType | ||
): string { | ||
switch (contentType) { | ||
case "Course": | ||
return path.substring( | ||
// there is probably a smarter way | ||
path.indexOf("content/") + 8, | ||
path.indexOf("/index.md") | ||
) | ||
default: | ||
throw new Error( | ||
`[getSlugFromFilePath] contentType not yet implemented: ${contentType}` | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
/** | ||
* Temporarily moved to `src/pages/glob.astro`, | ||
* see this issue -> https://github.com/withastro/astro/issues/5552 | ||
*/ | ||
export async function getStaticPathFromContentType() { | ||
throw new Error("Temporarily moved to `src/pages/glob.astro`") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,2 @@ | ||
/** | ||
* Get content's slug from a content file path | ||
* @param path where is the content file ? | ||
* @param contentType what's the type of content ? | ||
* @returns | ||
*/ | ||
export function getSlugFromPath( | ||
path: string, | ||
contentType: contentType | ||
): string { | ||
switch (contentType) { | ||
case "Course": | ||
return path.substring( | ||
// there is probably a smarter way | ||
path.indexOf("content/") + 8, | ||
path.indexOf("/index.md") | ||
) | ||
default: | ||
throw new Error(`contentType not yet implemented: ${contentType}`) | ||
} | ||
} | ||
|
||
type contentType = "Course" | "Chapter" | "Page" | ||
export * from "./files" | ||
export * from "./glob" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
{ | ||
"extends": "astro/tsconfigs/strict" | ||
} | ||
"extends": "astro/tsconfigs/strict", | ||
"compilerOptions": { | ||
"baseUrl": ".", | ||
"paths": { | ||
"$components/*": ["src/components/*"], | ||
"$data": ["src/data/index.ts"], | ||
"$layouts/*": ["src/layouts/*"], | ||
"$utils": ["src/utils/index.ts"] | ||
} | ||
} | ||
} |