Skip to content

Instantly share code, notes, and snippets.

@BrunoQuaresma
Created May 29, 2021 16:39
Show Gist options
  • Save BrunoQuaresma/b3ed37f9179acc46dc132245ba0f5e0f to your computer and use it in GitHub Desktop.
Save BrunoQuaresma/b3ed37f9179acc46dc132245ba0f5e0f to your computer and use it in GitHub Desktop.
Next/mdx blog index page
export async function getStaticProps() {
const postDirectory = path.join(process.cwd(), "src/pages/blog");
const postFilenames = fs
.readdirSync(postDirectory)
.filter((file) => path.extname(file).toLowerCase() === ".mdx");
const postModules = await Promise.all(
postFilenames.map(async (p) => require(`./${p}`))
);
return {
props: {
posts: postModules.map((post) => ({
...post.meta,
date: (post.meta.date as Date).toISOString(),
})),
},
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment