1 | var extend = hexo.extend,
|
2 | util = hexo.util,
|
3 | file = util.file,
|
4 | sourceDir = hexo.source_dir,
|
5 | xml2js = require('xml2js'),
|
6 | parser = new xml2js.Parser(),
|
7 | request = require('request'),
|
8 | async = require('async'),
|
9 | _ = require('underscore'),
|
10 | tomd = require('to-markdown').toMarkdown;
|
11 |
|
12 | extend.migrator.register('wordpress', function(args){
|
13 | var source = args._.shift();
|
14 |
|
15 | if (!source) return console.log('\nUsage: hexo migrate wordpress <source>\n\nMore info: http://zespia.tw/hexo/docs/migrate.html\n');
|
16 |
|
17 | async.waterfall([
|
18 | function(next){
|
19 | console.log('Fetching %s.', source);
|
20 |
|
21 |
|
22 | if (source.match(/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[.\!\/\\w]*))?)/)){
|
23 | request(source, function(err, res, body){
|
24 | if (err) throw err;
|
25 | if (res.statusCode == 200) next(null, body);
|
26 | });
|
27 | } else {
|
28 | file.read(source, next);
|
29 | }
|
30 | },
|
31 | function(data, next){
|
32 | console.log('Parsing XML.');
|
33 | parser.parseString(data, next);
|
34 | },
|
35 | function(data, next){
|
36 | console.log('Analyzing.');
|
37 |
|
38 | var length = 0,
|
39 | arr = data.rss.channel[0].item;
|
40 |
|
41 | async.forEach(arr, function(item, next){
|
42 | var postTitle = item.title[0],
|
43 | postDate = item['wp:post_date'][0],
|
44 | postLink = item['wp:post_name'][0],
|
45 | postContent = item['content:encoded'][0],
|
46 | postComment = item['wp:comment_status'][0] === 'open' ? true : false;
|
47 |
|
48 | if (_.isObject(postTitle)) postTitle = '';
|
49 | if (!postLink || _.isObject(postLink)) {
|
50 | if (postTitle)
|
51 | postLink = postTitle.toLowerCase().split(' ').join('-');
|
52 | else {
|
53 |
|
54 | postLink = item['wp:post_id'][0];
|
55 | }
|
56 | }
|
57 |
|
58 | postContent = _.isObject(postContent) ? '' : tomd(postContent);
|
59 | postContent = postContent.replace(/\r\n/g, '\n');
|
60 |
|
61 | switch (item['wp:post_type'][0]){
|
62 | case 'post':
|
63 | length++;
|
64 |
|
65 | var postStatus = item['wp:status'][0] === 'publish' ? '_posts/' : '_drafts/',
|
66 | cats = item.category,
|
67 | postTag = [];
|
68 |
|
69 | _.each(cats, function(item){
|
70 | if (!_.isString(item) && item.$.domain === 'tag'){
|
71 | postTag.push(item._);
|
72 | }
|
73 | });
|
74 |
|
75 | if (postTag.length) postTag = '\n- ' + _.uniq(postTag).join('\n- ');
|
76 |
|
77 | var content = [
|
78 | 'title: ' + postTitle,
|
79 | 'date: ' + postDate,
|
80 | 'tags: ' + (postTag ? postTag : ''),
|
81 | '---'
|
82 | ];
|
83 |
|
84 | file.write(sourceDir + postStatus + decodeURIComponent(postLink) + '.md', content.join('\n') + '\n\n' + postContent, next);
|
85 | break;
|
86 |
|
87 | case 'page':
|
88 | length++;
|
89 |
|
90 | var content = [
|
91 | 'title: ' + postTitle,
|
92 | 'date: ' + postDate,
|
93 | '---'
|
94 | ];
|
95 |
|
96 | file.write(sourceDir + postLink + '/index.md', content.join('\n') + '\n\n' + postContent, next);
|
97 | break;
|
98 |
|
99 | default:
|
100 | next();
|
101 | }
|
102 | }, function(err){
|
103 | if (err) throw err;
|
104 | next(null, length);
|
105 | });
|
106 | }
|
107 | ], function(err, length){
|
108 | if (err) throw err;
|
109 | console.log('%d posts migrated.', length);
|
110 | });
|
111 | });
|