-
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
getStaticPathFromContentType add a standardized way to get static path. But the function throw an obscure error when used in TS files, See this issue -> withastro/astro#5552
- Loading branch information
1 parent
f1bcd4e
commit 79e1361
Showing
14 changed files
with
150 additions
and
49 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 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 |
---|---|---|
@@ -1,25 +1,16 @@ | ||
--- | ||
import type { Course, CourseInstance } from "../data/courses" | ||
import ContentLayout from "../layouts/ContentLayout.astro" | ||
import { getSlugFromPath } from "../utils" | ||
import type { CourseInstance } from "$data" | ||
import { getStaticPathFromContentType } from "./glob.astro" | ||
import ContentLayout from "$layouts/ContentLayout.astro" | ||
interface Props { | ||
course: CourseInstance | ||
content: CourseInstance | ||
} | ||
const { course } = Astro.props | ||
const { content } = Astro.props | ||
export async function getStaticPaths() { | ||
const courses = await Astro.glob<Course>("../../content/**/index.md") | ||
return courses.map((course) => ({ | ||
params: { | ||
course: getSlugFromPath(course.file, "Course"), | ||
}, | ||
props: { | ||
course: { ...course }, | ||
}, | ||
})) | ||
return await getStaticPathFromContentType(Astro, "Course") | ||
} | ||
--- | ||
|
||
<ContentLayout content={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,54 @@ | ||
--- | ||
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") | ||
return contents.map((content) => ({ | ||
params: { | ||
course: getSlugFromFilePath(content.file, contentType), | ||
}, | ||
props: { | ||
content: { ...content }, | ||
}, | ||
})) | ||
case "Chapter": | ||
contents = await Astro.glob<Chapter>(`../../content/**/**/chapter.md`) | ||
return contents.map((content) => ({ | ||
params: { | ||
chapter: getSlugFromFilePath(content.file, contentType), | ||
}, | ||
props: { | ||
content: { ...content }, | ||
}, | ||
})) | ||
case "Page": | ||
contents = await Astro.glob<Chapter>("../../content/**/**/*.md") | ||
default: | ||
throw new Error( | ||
`[getStaticPathFromContentType] 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
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,37 @@ | ||
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 extractSlugFromFilePath(path, "index.md") | ||
case "Chapter": | ||
return extractSlugFromFilePath(path, "chapter.md") | ||
default: | ||
throw new Error( | ||
`[getSlugFromFilePath] contentType not yet implemented: ${contentType}` | ||
) | ||
} | ||
} | ||
|
||
/** | ||
* Private function, utils used by getSlugFromFilePath to substring path | ||
* @param path What's the path to substring ? | ||
* @param end What's the end of the subtring ? | ||
* @returns | ||
*/ | ||
function extractSlugFromFilePath(path: string, end: string) { | ||
return path.substring( | ||
// there is probably a smarter way | ||
path.indexOf("content/") + 8, | ||
path.indexOf(`/${end}`) | ||
) | ||
} |
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"] | ||
} | ||
} | ||
} |