Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat : add the mdx file body on getAll and getBySlug #6

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .changeset/healthy-clouds-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
"typed-mdx": major
---
Improve mdx data retrieval and add string body retrieval
7 changes: 3 additions & 4 deletions apps/web/src/app/blog/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ export default async function BlogPostPage({
params: { slug: string };
}) {
const blogPost = await collections.blog.getBySlug(params.slug);
const author = await collections.author.getBySlug(blogPost.author);
const author = await collections.author.getBySlug(blogPost.data.author);

const Content = (await import(`@/content/${blogPost.metadata.filePath}`))
.default;

return (
<article>
<h2>{blogPost.title}</h2>
<small>by {author.name}</small>
<h2>{blogPost.data.title}</h2>
<small>by {author.data.name}</small>
<Content />
</article>
);
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export default async function Page() {

<ul>
{posts.map((post) => (
<li key={post.title}>
<li key={post.data.title}>
<Link href={`/blog/${post.metadata.slug}`}>
{post.title} {post.author && <>by {post.author}</>}
{post.data.title} {post.data.author && <>by {post.data.author}</>}
</Link>
</li>
))}
Expand Down
62 changes: 43 additions & 19 deletions packages/typed-mdx/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,22 @@ async function parseMdxFile<Z extends z.Schema>(

return frontmatter;
}
async function stringifyMDX(mdxPath: string): Promise<string> {
const filePath = path.resolve(mdxPath);
const frontmatterResult = parseFrontmatter(await fs.readFile(filePath));

if (frontmatterResult.isError()) {
throw new Error(frontmatterResult.error);
}

const frontmatter = frontmatterResult.get().content;

return frontmatter;
}

const metadataSchema = z.object({
metadata: z.object({
slug: z.string(),
filePath: z.string(),
}),
slug: z.string(),
filePath: z.string(),
});

const MDX_ENTENSION = ".mdx";
Expand All @@ -82,7 +92,13 @@ async function getAll<Z extends z.Schema>({
}: {
folder: string;
schema: Z;
}): Promise<(z.infer<Z> & z.infer<typeof metadataSchema>)[]> {
}): Promise<
{
data: z.infer<Z>;
body: string;
metadata: z.infer<typeof metadataSchema>;
}[]
> {
assertSchemaIsObject(schema);

const folderPath = `${CONTENT_FOLDER}/${folder}`;
Expand All @@ -98,8 +114,7 @@ async function getAll<Z extends z.Schema>({
const mdxFileNames = postFilePaths.filter(
(postFilePath) => path.extname(postFilePath).toLowerCase() === MDX_ENTENSION
);

const data = await Promise.all(
return await Promise.all(
mdxFileNames.map(async (mdxFileName) => {
const parsedFrontmatter = await parseMdxFile(
`${folderPath}/${mdxFileName}`,
Expand All @@ -114,14 +129,11 @@ async function getAll<Z extends z.Schema>({
slug,
filePath: `${folder}/${slug}.mdx`,
};
const body = await stringifyMDX(`${folderPath}/${mdxFileName}`);

return { metadata, ...parsedFrontmatter };
return { metadata, body, data: parsedFrontmatter };
})
);

return z
.array(schema.merge(metadataSchema))
.parse(data.filter(Boolean)) as any; // TODO FIX THIS ANY
}

async function getBySlug<Z extends z.Schema>({
Expand All @@ -132,7 +144,11 @@ async function getBySlug<Z extends z.Schema>({
folder: string;
schema: Z;
slug: string;
}): Promise<z.infer<Z> & z.infer<typeof metadataSchema>> {
}): Promise<{
data: z.infer<Z>;
body: string;
metadata: z.infer<typeof metadataSchema>;
}> {
assertSchemaIsObject(schema);

const filePath = `${CONTENT_FOLDER}/${folder}/${slug}.mdx` as const;
Expand All @@ -148,9 +164,9 @@ async function getBySlug<Z extends z.Schema>({
filePath: `${folder}/${slug}.mdx`,
};

const data = { metadata, ...parsedFrontmatter };
const body = await stringifyMDX(filePath);

return schema.merge(metadataSchema).parse(data) as any; // TODO FIX THIS ANY
return { metadata, body, data: parsedFrontmatter } as any; // TODO FIX THIS ANY
}

export function defineCollection<Z extends z.Schema>(options: {
Expand All @@ -159,11 +175,19 @@ export function defineCollection<Z extends z.Schema>(options: {
strict?: boolean;
}): {
getAll: () => Promise<
Prettify<z.infer<Z> & z.infer<typeof metadataSchema>>[]
Prettify<{
data: z.infer<Z>;
body: string;
metadata: z.infer<typeof metadataSchema>;
}>[]
>;
getBySlug: (slug: string) => Promise<
Prettify<{
data: z.infer<Z>;
body: string;
metadata: z.infer<typeof metadataSchema>;
}>
>;
getBySlug: (
slug: string
) => Promise<Prettify<z.infer<Z> & z.infer<typeof metadataSchema>>>;
schema: Z;
} {
assertSchemaIsObject(options.schema);
Expand Down
Loading