improve handling of draft posts

This commit is contained in:
Timothy
2021-01-16 18:36:25 +08:00
parent fdda4c2042
commit 8f646e3429
2 changed files with 19 additions and 11 deletions

View File

@ -85,17 +85,15 @@ export async function getFileBySlug(type, slug) {
export async function getAllFilesFrontMatter(type) {
const files = fs.readdirSync(path.join(root, 'data', type))
const allFrontMatter = files.reduce((allPosts, postSlug) => {
const source = fs.readFileSync(path.join(root, 'data', type, postSlug), 'utf8')
const allFrontMatter = []
files.forEach((file) => {
const source = fs.readFileSync(path.join(root, 'data', type, file), 'utf8')
const { data } = matter(source)
return [
{
...data,
slug: postSlug.replace(/\.(mdx|md)/, ''),
},
...allPosts,
]
}, [])
if (data.draft !== true) {
allFrontMatter.push({ ...data, slug: file.replace(/\.(mdx|md)/, '') })
}
})
return allFrontMatter.sort((a, b) => dateSortDesc(a.date, b.date))
}