init
This commit is contained in:
43
src/index.js
Normal file
43
src/index.js
Normal 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/'))
|
||||
}
|
Reference in New Issue
Block a user