This commit is contained in:
ppst
2023-04-06 21:31:57 +08:00
commit d10412ccd8
4 changed files with 335 additions and 0 deletions

43
src/index.js Normal file
View File

@@ -0,0 +1,43 @@
const globby = require('globby')
const matter = require('gray-matter')
const fs = require('fs-extra')
const path = require('path')
module.exports = { getPosts:async()=>{
let paths = await getPostMDFilePaths()
let posts = await Promise.all(
paths.map(async (item) => {
const content = await fs.readFile(item, 'utf-8')
const { data } = matter(content)
const description = data.title?data.title:'这是一个简短描述,但是这篇随笔没有描述。'
data.description = data.description?data.description:description
data.date = _convertDate(data.date)
data.category = data.category?data.category:data.tags[0]
return {
frontMatter: data,
regularPath: `/${item.replace('.md', '.html')}`
}
})
)
posts.sort(_compareDate)
return posts
} }
function _convertDate(date = new Date().toString()) {
const json_date = new Date(date).toJSON()
return json_date.split('T')[0]
}
function _compareDate(obj1, obj2) {
return obj1.frontMatter.date < obj2.frontMatter.date ? 1 : -1
}
async function getPostMDFilePaths() {
let paths = await globby(['**.md'], {
ignore: ['node_modules', 'README.md']
})
return paths.filter((item) => item.includes('posts/'))
}