Skip to content

Instantly share code, notes, and snippets.

@brunosabot
Last active February 27, 2022 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunosabot/c5faec177d6a35826d65a3c759ee6458 to your computer and use it in GitHub Desktop.
Save brunosabot/c5faec177d6a35826d65a3c759ee6458 to your computer and use it in GitHub Desktop.
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