Skip to content

Commit 3dcb9d1

Browse files
committed
Refactor posts logic
1 parent 892cfa7 commit 3dcb9d1

File tree

1 file changed

+26
-29
lines changed
  • playground/rsc-vite-framework/app/routes/mdx-glob.$post/posts

1 file changed

+26
-29
lines changed

playground/rsc-vite-framework/app/routes/mdx-glob.$post/posts/posts.ts

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1+
import nodePath from "node:path";
2+
13
type BlogPost = {
24
Component: React.ComponentType;
5+
path: string;
36
title: string;
47
slug: string;
5-
path: string;
68
};
79

8-
async function resolvePosts(): Promise<
9-
Record<string, () => Promise<BlogPost>>
10-
> {
10+
async function resolvePosts(): Promise<{
11+
[slug: string]: () => Promise<BlogPost>;
12+
}> {
1113
const rawPosts = (await import.meta.glob("./*/*.mdx")) as Record<
1214
string,
1315
() => Promise<{
1416
default: React.ComponentType;
15-
frontmatter: { title: string };
17+
frontmatter?: unknown;
1618
}>
1719
>;
1820

@@ -23,52 +25,47 @@ async function resolvePosts(): Promise<
2325
return [
2426
slug,
2527
async (): Promise<BlogPost> => {
26-
const pathParts = path.split("/");
27-
const directoryName = pathParts[pathParts.length - 2];
28-
29-
if (directoryName !== slug) {
30-
throw new Error(
31-
`Invalid post structure: directory name "${directoryName}" does not match slug "${slug}" in path "${path}"`,
32-
);
33-
}
34-
3528
const post = await loadPost();
3629

30+
let title: string;
3731
if (
38-
!post?.frontmatter ||
39-
typeof post.frontmatter !== "object" ||
40-
!("title" in post.frontmatter) ||
41-
typeof post.frontmatter.title !== "string"
32+
post.frontmatter &&
33+
typeof post.frontmatter === "object" &&
34+
"title" in post.frontmatter &&
35+
typeof post.frontmatter.title === "string"
4236
) {
43-
throw new Error(`Invalid frontmatter for ${path}`);
37+
title = post.frontmatter.title;
38+
} else {
39+
const prettyPath = nodePath.relative(
40+
process.cwd(),
41+
nodePath.resolve(import.meta.dirname, path),
42+
);
43+
console.error(
44+
`Invalid frontmatter for ${prettyPath}: Missing title`,
45+
);
46+
title = "Untitled Post";
4447
}
4548

4649
return {
4750
Component: post.default,
48-
title: post.frontmatter.title,
49-
slug,
5051
path: `/mdx-glob/${slug}`,
52+
title,
53+
slug,
5154
};
5255
},
5356
];
5457
}),
5558
);
5659
}
5760

58-
export async function getPost(slug: string): Promise<BlogPost | undefined> {
61+
export async function getPost(slug: string): Promise<BlogPost | null> {
5962
const posts = await resolvePosts();
6063
const loadPost = posts[slug];
61-
62-
if (!loadPost) {
63-
return undefined;
64-
}
65-
66-
return await loadPost();
64+
return loadPost ? await loadPost() : null;
6765
}
6866

6967
export async function getPosts(): Promise<Record<string, BlogPost>> {
7068
const posts = await resolvePosts();
71-
7269
return Object.fromEntries(
7370
await Promise.all(
7471
Object.entries(posts).map(async ([slug, loadPost]) => {

0 commit comments

Comments
 (0)