Created
October 25, 2016 05:55
-
-
Save keelii/b6c51290e5ee0253f99a6424a7e2faeb to your computer and use it in GitHub Desktop.
transfer octopress post to hugo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var fs = require('fs') | |
var path = require('path') | |
var yaml = require('js-yaml'); | |
var files = fs.readdirSync(__dirname); | |
var dest = './dest' | |
files.forEach(convert) | |
function convert(filename) { | |
var re = /^\d{4}-\d{2}-\d{2}-/ | |
var mdRE = /\.(md|markdown)$/ | |
var needRename = re.test(filename) | |
var newFilename = needRename | |
? filename.replace(re, '').replace(mdRE, '.md') | |
: filename | |
var realFileName = path.join(dest, newFilename) | |
if (mdRE.test(filename)) { | |
var originContent = fs.readFileSync(filename).toString() | |
var yamlStart = originContent.indexOf('---') | |
var yamlEnd = originContent.indexOf('---', 3) | |
// console.log('Start: %s | End: %s', yamlStart, yamlEnd) | |
var yamlHeader = originContent.substr(yamlStart, yamlEnd) | |
try { | |
var data = yaml.load(yamlHeader) | |
} catch (err) { | |
console.log(err) | |
return false; | |
} | |
var toml = `+++ | |
title = "${data.title}" | |
isCJKLanguage = true | |
date = "${data.date}" | |
categories = ${getArrayValue(data.categories)} | |
tags = ${getArrayValue(data.tags)} | |
+++` | |
function getArrayValue(data) { return data ? JSON.stringify(data) : "[]" } | |
var result = toml + originContent.substr(yamlEnd + 3) | |
console.log('===== %s =====', newFilename) | |
console.log(toml) | |
if (!fs.existsSync(dest)) { fs.mkdirSync(dest) } | |
fs.writeFileSync(realFileName, result, 'utf-8') | |
// console.log('===== before =====') | |
// console.log(yamlHeader) | |
// console.log('===== after =====') | |
// console.log(toml) | |
// console.log('\n') | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment