将博客从WordPress搬家至Hexo全流程

因为我的服务器最近到期了,我又不想在续费国内的机器,但是我的域名是购买了10年,不舍得放弃,所以边萌生了托管的念头,考虑到方便性和速度,我选择了老东家腾讯云,下面我来介绍一下搬迁的全过程吧。

准备

  1. 一台网络畅通的电脑(我使用的是win10,教程也是在win10下进行的)。
  2. 超文本编辑器(我使用的是sublime text 3),wordpress导出的文件(xml结尾)。
  3. 一颗聪明的头脑。

正文

WordPress导出

首先我们先把原服务器的资料都保存好,然后在WordPress导出器里面导出全部文件。

Hexo配置

  1. 首先我们先配置Hexo,它和一般即用即装的软件不一样,它需要你去手动安装好各个组件才能使用

    • 第一步我们得安装Node.js,Node.js中文站,下载windows最新版安装即可。

    • 然后我们需要安装git,和上面一样,git最新版下载

    • 把两个都安装好之后就可以直接安装Hexo了。在任意位置右键点击Git Bash Here,进入命令行页面。

      输入以下命令:

      1
      2
      3
      4
      5
      6
      npm install
      npm install -g hexo-cli
      npm install hexo-server --save
      hexo init blog
      cd blog
      hexo server

      这样的话一个基本的hexo博客就创建好了,在浏览器输入http://localhost:4000就能看见博客内容。

      但是为了更符合自己的心意,肯定不能局限于此啊,所以我们得在加工一下。

  2. 安装主题

输入以下代码:

1
2
3
4
cd blog
git clone https://github.com/tufu9441/maupassant-hexo.git themes/maupassant
npm install hexo-renderer-pug --save
npm install hexo-renderer-sass --save
  1. 主题修改配置
    修改blog目录下的 _config.yml : theme: maupassant

注:若npm install hexo-renderer-sass安装时报错,可能是国内网络问题,请尝试使用代理或者切换至淘宝NPM镜像安装。

  1. WordPress迁移

现在我们将WordPress的文章迁移到Hexo,如果你不需要迁移,跳过即可。

首先,安装hexo-migrator-wordpress插件。

1
2
3
hexo init migrate
npm install hexo-migrator-wordpress --save
npm install https://github.com/hexojs/hexo-migrator-wordpress.git --save

你需要把博客导出文件xml的数据先过滤下,然后修改下hexo-migrator-wordpress的源码。
我是这么改的。

第一步:

1
perl -pi -e 's,<title>(.*)</title>,<title><![CDATA[$1]]></title>,' wordpress.2018-03-06.xml

第二步:

修改hexo-migrator-wordpress的index.js。在node_modules/hexo-migrator-wordpress/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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
var xml2js = require('xml2js'),
async = require('async'),
tomd = require('to-markdown').toMarkdown,
request = require('request'),
file = require('fs');

var captialize = function(str){
return str[0].toUpperCase() + str.substring(1);
};
function replaceTwoBrace(str){
str = str.replace(/{{/g, '{ {');
return str;
};
function replaceHTMLEntity(str){
str = str.replace(/amp;/g, '');
str = str.replace(/&lt;/g, '<');
str = str.replace(/&gt;/g, '>');
str = str.replace(/&quot;/g, '"');
str = str.replace(/&#92;/g, '\\');
str = str.replace(/&#48;/g, '0');
return str;
};
function replaceCodeTag(str){
str = str.replace(/(<pre class=).*?>/g, '<pre>');
str = str.replace(/\<pre\>/gi, '```\r\n');
str = str.replace(/\<\/pre\>/gi, '```');
//str = str.replace(/<pre>(.*)<\/pre>/ig, '```$1```');
//console.log(str);
// str = str.replace(/\[python\]/gi, '```');
// str = str.replace(/\[\/python\]/gi, '```');
// str = str.replace(/\[java\]/gi, '```');
// str = str.replace(/\[\/java\]/gi, '```');
// str = str.replace(/\[php\]/gi, '```');
// str = str.replace(/\[\/php\]/gi, '```');
// str = str.replace(/\[c\]/gi, '```');
// str = str.replace(/\[\/c\]/gi, '```');
return str;
};
hexo.extend.migrator.register('wordpress', function(args, callback){
var source = args._.shift();

if (!source){
var help = [
'Usage: hexo migrate wordpress <source>',
'',
'For more help, you can check the docs: http://hexo.io/docs/migration.html'
];

console.log(help.join('\n'));
return callback();
}

var log = hexo.log,
post = hexo.post;

log.i('Analyzing %s...', source);

async.waterfall([
function(next){
// URL regular expression from: http://blog.mattheworiordan.com/post/13174566389/url-regular-expression-for-links-with-or-without-the
if (source.match(/((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[.\!\/\\w]*))?)/)){
request(source, function(err, res, body){
if (err) throw err;
if (res.statusCode == 200) next(null, body);
});
} else {
file.readFile(source, next);
}
},
function(content, next){
xml2js.parseString(content, next);
},
function(xml, next){
var count = 0;

async.each(xml.rss.channel[0].item, function(item, next){
if (!item['wp:post_type']){
return next();
}
//console.log(item.title[0]);
var title = item.title[0],
id = item['wp:post_id'][0],
date = item['wp:post_date'][0],
slug = item['wp:post_name'][0],
content = item['content:encoded'][0],
comment = item['wp:comment_status'][0],
status = item['wp:status'][0],
type = item['wp:post_type'][0],
categories = [],
tags = [];

if (!title && !slug) return next();
if (type !== 'post' && type !== 'page') return next();
if (typeof content !== 'string') content = '';
content = replaceTwoBrace(content);
content = replaceHTMLEntity(content);
content = replaceCodeTag(content);
content = tomd(content).replace(/\r\n/g, '\n');
count++;

if (item.category){
item.category.forEach(function(category, next){
var name = category._;

switch (category.$.domain){
case 'category':
categories.push(name);
break;

case 'post_tag':
tags.push(name);
break;
}
});
}

var data = {
title: title || slug,
id: +id,
date: date,
content: content,
layout: status === 'draft' ? 'draft' : 'post',
};

if (type === 'page') data.layout = 'page';
if (slug) data.slug = slug.toString();
if (comment === 'closed') data.comment = false;
if (categories.length && type === 'post') data.categories = categories;
if (tags.length && type === 'post') data.tags = tags;
log.i('%s found: %s', captialize(type), title);
data.title = data.title.toString();
data.date = data.date.toString();
//console.log(data.slug);
//console.log(data.date);
post.create(data, next);
}, function(err){
if (err) return next(err);

log.i('%d posts migrated.', count);
});
}
], callback);
});

最后再导入wp的数据。

1
hexo migrate wordpress 博客文件路径
  1. 添加RSS
1
npm install hexo-generator-feed

在博客文件根目录下_config.yml文件中添加如下内容:

1
2
3
4
5
6
7
8
# Extensions
plugins:
hexo-generator-feed
#Feed Atom
feed:
type: atom
path: atom.xml
limit: 20

生成rss

1
hexo g
  1. 配置oss

我们需要安装hexo-deployer-cos插件,如果你已经在准备中安装了,那么就不用安装了。否则按照下面的命令安装。

1
npm install hexo-deployer-cos --save

接下面我们配置hexo的配置文件,首先打开根目录的_config.yml配置文件,将原来的deploy替换为下面的内容:

1
2
3
4
5
6
7
deploy: 
type: cos
appId: yourAPPID ##此处自己修改
secretId: yourSecretId ##此处自己修改
secretKey: yourSecretKey ##此处自己修改
bucket: yourBucketName-yourAPPID ##此处自己修改
region: yourRegion ##此处自己修改

在腾讯云oss里面设置好储存桶,获取api,设置好解析

略…(PS:累了,写不动了)

现在你已经完成了最后的设置,最后一步就是需要部署项目到COS了,输入我们熟悉的部署命令:

1
hexo g -d

正常的话就已经完成了

尾记

差不多就结束了,有问题可以在关于里面找我的联系方式。