real-jiakai / next-blog

This repo stores my weekly posts.

Home Page:https://gujiakai.top

Repository from Github https://github.comreal-jiakai/next-blogRepository from Github https://github.comreal-jiakai/next-blog

feat: 文章的frontmatter添加draft属性后的过滤展示

real-jiakai opened this issue · comments

commented

使用filter函数过滤掉草稿文章(frontmatter中draft属性为true的文章)。

export function getSortedPostsData() {
	const fileNames = fs.readdirSync(postsDirectory)
	let allPostsData = fileNames.map(fileName => {
		const id = fileName.replace(/\.mdx?$/, '')
		const fullPath = path.join(postsDirectory, fileName)
		const fileContents = fs.readFileSync(fullPath, 'utf8')
		const matterResult = matter(fileContents)
		const date = matterResult.data.date

		return {
			id,
			date,
			tags: matterResult.data.tags,
			summary: matterResult.data.summary,
			contentMarkdown: matterResult.content,
			...matterResult.data
		}
	})

	allPostsData = allPostsData.filter(post => post.draft !== true)

	return allPostsData.sort((a, b) => {
		if (a.date < b.date) {
			return 1
		} else {
			return -1
		}
	})
}