Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
将点点网的BLOG导出的XML转化Markdown格式的文件
var cheerio = require('cheerio')
var fs = require('fs')
var moment = require('moment')
var upndown = require('upndown')
var text = fs.readFileSync("blogs.xml", 'utf8')
var $ = cheerio.load(text)
var posts = $('post')
posts.each(function () {
var post = $(this)
var title = post.find('Title').text()
var htmlText = post.find('Text').text()
var date = new Date(parseInt(post.find('CreateTime').text()))
var dashDate = moment(date).format('YYYY-MM-DD')
// date: 9:29 2013/7/28
var blogDate = moment(date).format('HH:mm YYYY/MM/DD')
var fileName = dashDate + '-' + title.replace(/\s/g, '_')
var und = new upndown()
var markdown = und.convert(htmlText)
var content = '---\n\
title: ' + title + '\n\
date: ' + blogDate + '\n\
layout: post\n\
---\n' + markdown
fs.writeFileSync(fileName + '.md', content, {
encoding: 'utf8',
flag: 'w'
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.