Hexo博客网站部署配置

Hexo 是一个快速、简洁且高效的博客框架。Hexo 使用 Markdown(或其他渲染引擎)解析文章,在几秒内,即可利用靓丽的主题生成静态网页。

环境变量配置

将 Hexo 所在的目录下的 node_modules 添加到环境变量之中即可直接使用 hexo <command>

1
F:\hexosite\hexosite\node_modules\.bin

部署插件

1
npm install hexo-deployer-git --save

Github配置

配置用户名
1
git config --global user.name "yourname"
配置GitHub账户邮箱
1
git config --global user.email "youremail@example.com"
输入命令生成SSH key:
1
ssh-keygen -t rsa -C "youremail@example.com"

命令界面会显示生成的位置,例如我的是在”c/users/dell/.ssh/id_rsa.pub”。打开id_rsa.pub文件,里面的内容全部复制。在网页中点击GitHub账户头像选择”Settings”,再点击”SSH and GPG keys”,点击SSH右边的”new SSH key”,Title可以随便输入,Key中就将刚刚复制的密钥粘贴过去,点击”Add SSH key”,之后就会生成的16进制的SSH 密令。打开GitHub上的仓库,复制SSH地址链接,

Git Bash中输入关联GitHub账户:
1
ssh git@github.com

在你的博客项目文件夹中打开”_config.yml”文件,直接拉到最下面

配置项目部署环境
1
2
3
4
deploy:
type: git
repository: git@github.com:Isdear/Isdear.github.io.git
branch: master

Hexo版本升级

检查版本
1
hexo version
版本检查命令
1
2
3
4
##安装检查命令
npm install -g npm-check
##检查
npm-check
版本升级详情
1
2
3
4
##安装检查命令
npm install -g npm-upgrade
##检查
npm-upgrade

这个命令执行完之后,就会出现当前版本与可升级版本的比较,然后让你选择是否升级,一直yes就行了。Hexo版本升级

检查版本
1
hexo version
版本检查命令
1
2
3
4
##安装检查命令
npm install -g npm-check
##检查
npm-check
版本升级详情
1
2
3
4
##安装检查命令
npm install -g npm-upgrade
##检查
npm-upgrade

这个命令执行完之后,就会出现当前版本与可升级版本的比较,然后让你选择是否升级,一直yes就行了。

相对路径引用图片失败

安装hexo-asset-image后,需要对文件进行修改,修改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
'use strict';
var cheerio = require('cheerio');

// http://stackoverflow.com/questions/14480345/how-to-get-the-nth-occurrence-in-a-string
function getPosition(str, m, i) {
return str.split(m, i).join(m).length;
}

var version = String(hexo.version).split('.');
hexo.extend.filter.register('after_post_render', function(data){
var config = hexo.config;
if(config.post_asset_folder){
var link = data.permalink;
if(version.length > 0 && Number(version[0]) == 3)
var beginPos = getPosition(link, '/', 1) + 1;
else
var beginPos = getPosition(link, '/', 3) + 1;
// In hexo 3.1.1, the permalink of "about" page is like ".../about/index.html".
var endPos = link.lastIndexOf('/') + 1;
link = link.substring(beginPos, endPos);

var toprocess = ['excerpt', 'more', 'content'];
for(var i = 0; i < toprocess.length; i++){
var key = toprocess[i];

var $ = cheerio.load(data[key], {
ignoreWhitespace: false,
xmlMode: false,
lowerCaseTags: false,
decodeEntities: false
});

$('img').each(function(){
if ($(this).attr('src')){
// For windows style path, we replace '\' to '/'.
var src = $(this).attr('src').replace('\\', '/');
if(!/http[s]*.*|\/\/.*/.test(src) &&
!/^\s*\//.test(src)) {
// For "about" page, the first part of "src" can't be removed.
// In addition, to support multi-level local directory.
var linkArray = link.split('/').filter(function(elem){
return elem != '';
});
var srcArray = src.split('/').filter(function(elem){
return elem != '' && elem != '.';
});
if(srcArray.length > 1)
srcArray.shift();
src = srcArray.join('/');
$(this).attr('src', config.root + link + src);
console.info&&console.info("update link as:-->"+config.root + link + src);
}
}else{
console.info&&console.info("no src attr, skipped...");
console.info&&console.info($(this));
}
});
data[key] = $.html();
}
}
});