-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
40 lines (34 loc) · 1.3 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const _ = require('lodash')
const fse = require('fs-extra')
const yaml = require('js-yaml')
const moment = require('moment')
const fm = require('front-matter')
const entities = require('entities')
const enml2text = require('enml2text')
const debug = require('debug')('everblog-adaptor-hexo')
module.exports = async function (data, cleanMode = false) {
const configPath = process.cwd() + '/_config.yml'
fse.outputFileSync(configPath, yaml.safeDump(data.$blog))
const postsPath = process.cwd() + '/source/_posts/'
fse.emptyDirSync(postsPath)
data.notes.forEach(note => {
const defaultFrontMatter = {
title: note.title,
date: formatDate(note.created),
updated: formatDate(note.updated),
tags: note.tags
}
debug(`title: ${note.title}, content(enml): ${note.content}`)
let contentMarkdown = entities.decodeHTML(enml2text(note.content))
let data = fm.parse(contentMarkdown)
_.merge(data.attributes, defaultFrontMatter)
contentMarkdown = fm.stringify(data)
const filename = postsPath + note.title + '.md'
fse.outputFileSync(filename, contentMarkdown)
debug(`title: ${filename}, content(markdown): ${JSON.stringify(contentMarkdown)}`)
})
debug('build success!')
}
function formatDate (timestamp) {
return moment(timestamp).format('YYYY/M/DD HH:mm:ss')
}