-
-
Save brunosabot/c5faec177d6a35826d65a3c759ee6458 to your computer and use it in GitHub Desktop.
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
import fs from "fs"; | |
import path from "path"; | |
const POSTS_PATH = path.join(process.cwd(), "posts"); | |
function loadGreyMatterPost(params) { | |
if ( | |
params === undefined || | |
params.year === undefined || | |
params.slug === undefined | |
) { | |
throw new Error("Not Found"); | |
} | |
const posts = fs | |
.readdirSync(POSTS_PATH) | |
.filter((path) => /\.mdx?$/.test(path)) | |
.map((path) => fs.readFileSync(POSTS_PATH + "/" + path)) | |
.map((source) => matter(source)); | |
const post = posts.find( | |
(post) => post.data.path === `/posts/${params.year}/${params.slug}` | |
); | |
if (post === undefined) { | |
throw new Error("Not Found"); | |
} | |
return [post, posts]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment