Created
October 5, 2020 19:30
-
-
Save arnonate/2b344c3bdf86890517076433382ec182 to your computer and use it in GitHub Desktop.
get post data from .md in Next getStaticProps
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"; | |
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