Svtter
9/4/2017 - 8:16 AM

导出hexo文章

导出hexo文章

const parse = require('hexo-front-matter').parse;
const fs = require('fs');
const path = require('path');
const outputFolder = 'json';
const dir = '.';

if (!fs.existsSync(outputFolder)) {
    fs.mkdir('./json', 0777, err => {
        if (err) throw err;
    })
}

// 解析hexo-markdown
let parseHexo = function (fileName) {
    let text = fs.readFileSync(fileName, 'utf8');
    let parsed = parse(text);
    const jscontent = JSON.stringify(parsed);
    let newName = fileName.slice(0, -3);
    fs.writeFileSync('json/' + newName + '.json', jscontent, 'utf8');
    console.log(fileName + ' 转换成功!')
}

// 遍历目录
let files = fs.readdirSync(dir);
files.forEach(function (filename) {
    let fullname = path.join(dir, filename);
    let stats = fs.statSync(fullname);
    if (stats.isDirectory()) return;
    parseHexo(fullname);
});