Skip to content

Instantly share code, notes, and snippets.

@arnonate
Created October 5, 2020 19:30
Show Gist options
  • Save arnonate/2b344c3bdf86890517076433382ec182 to your computer and use it in GitHub Desktop.
Save arnonate/2b344c3bdf86890517076433382ec182 to your computer and use it in GitHub Desktop.
get post data from .md in Next getStaticProps
import fs from "fs";
import path from "path";
import matter from "gray-matter";
import remark from "remark";
import html from "remark-html";
export type Product = {
id: string;
description: string;
price: number;
title: string;
thumbnail: string;
};
const productsDirectory = path.join(process.cwd(), "content/products");
export async function getProductsData() {
const fileNames = fs.readdirSync(productsDirectory);
const allPostsData = fileNames
.filter((filename) => filename !== ".DS_Store")
.map(async (fileName) => {
const id = fileName.replace(/\.md$/, "");
const fullPath = path.join(productsDirectory, fileName);
const fileContents = fs.readFileSync(fullPath, "utf8");
const matterResult = matter(fileContents);
let description = await remark().use(html).process(matterResult.content);
const product: Product = {
id,
description: description.toString(),
price: matterResult.data.price,
title: matterResult.data.title,
thumbnail: matterResult.data.thumbnail,
};
return product;
});
return Promise.all(allPostsData).then((values) => values);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment