hexo 搬家

一开始hexo配置在公司电脑上,但平时在家的时候还是自己的笔记本用的比较多,所以决定在笔记本上同步一下自己的Hexo环境。

笔记本上已经装好了nodejshexo,所以这点就不用再折腾了。那么,blog的源码怎么弄到笔记本呢?什么!Copy&Paste? 不行不行,这么low的方法怎么是我这个小码农使用的呢,还是继续git吧。

github?额,虽然源码也没什么东西,但我还是想建一个private repository,毕竟自己的一亩三分地是自己私人的东西。当然,github也提供private repository,但需要money,而我这么个简单的需求的确没什么花钱的必要。所以,还是用另一个简单好用的github兄弟,gitlab。这个我之前做项目有接触过,所以,继续搞起。

下面讲讲hexo搬家遇到的一些坑.

1. git submodule problem

hexo blog源码内部有发布版的blog网站,这个发布版的网站文件夹本身也是一个git仓库(发布到本网站的 http://billsedison.github.io ),因此,在执行

1
git add .

时,这个文件夹并不会加入到外部的仓库,这是件好事,因为将发布到github的文件夹再提交到gitlab的确多此一举。

但是!!! 之前下载的blog主题yilia也是从github上clone下来的,所以themes/yilia也是一个git仓库,同理,也不会被git add .加入到外部的仓库。但是,一开始我并不知道这件事情,所以,上传到gitlab上的blog源码的themes/yilia是空的,然后在自己笔记本clone下来后,本机运行是空的页面!

解决方案
由于之前已经上传过submodule themes/yilia,因此需要先从git仓库中移除掉themes/yilia,移除方法:

1
2
3
4
5
6
7
8
9
10
11
mv themes/yilia themes/yilia_bak # backup original yilia folder
git rm themes/yilia # remove from git repository
git submodule deinit themes/yilia # optional??
git commit -m "rm yilia temporarily"
git push -u origin master # sync modification to gitlab

mv themes/yilia_bak themes/yilia # revert the name
rm -fr themes/yilia/.git # IMPORTANT!!! make it be never a git repository
git add themes/yilia # add it to git repository again
git commit -m "add yilia again"
git push -u origin master # add it back

然后,再到本机上git clone,嗯,发现东西都在了!

2. node_module problem

虽然上面用的都是Linux下的命令,但我其实是work在Windows上的有木有!我自己配了一个比较好用的Windows Console,然后把大部分Linux命令都集成进来了(不是Cygwin,也不是msys,就是原生的Windows CMD),当然这是闲话……

Windows有个该死的限制是,PATH的最大字符长度不能超过260,这是历史遗留问题,反正是不能通过改下面的宏实现的T_T

1
#define MATH_PATH 260

而nodejs安装的本地node_module里面经常是文件夹套文件夹,所以260的余粮很快就没了,而git add .也加不进超过260的路径,所以node_module文件夹是在Windows上加不到git仓库的。

好吧,这其实也是好事,因为这些node_module都可以通过npm install来安装,且装完一般也不会变,所以也没必要传到gitlab上。
只不过,以后如果把blog迁移到其它机器时,我得记得执行下面的命令:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
npm install hexo --save
npm install hexo-generator-index --save
npm install hexo-generator-archive --save
npm install hexo-generator-category --save
npm install hexo-generator-tag --save
npm install hexo-server --save
npm install hexo-deployer-git --save
npm install hexo-deployer-heroku --save
npm install hexo-deployer-rsync --save
npm install hexo-deployer-openshift --save
npm install hexo-renderer-marked@0.2 --save
npm install hexo-renderer-stylus@0.2 --save
npm install hexo-generator-feed@1 --save
npm install hexo-generator-sitemap@1 --save

npm install hexo-renderer-ejs --save
npm install hexo-renderer-stylus --save
npm install hexo-renderer-marked --save

最后的三行很重要,因为没有这三行,解析出来的页面是这样的……

1
2
3
4
5
6
<%- partial('_partial/head') %>
<%- partial('_partial/header') %>
<%- body %>
<% if (theme.sidebar && theme.sidebar !== 'bottom'){ %> <%- partial('_partial/sidebar') %> <% } %>
<%- partial('_partial/footer') %>
<%- partial('_partial/mobile-nav') %> <%- partial('_partial/after-footer') %>

折腾完上述工作后,再运行

1
hexo s

本地测试,然后,一切准备就绪~~~~

Remarks

  1. 那些npm install我想写在一个批处理.bat中一次执行完,但是却发现批处理中只会执行第一条npm install,这是什么鬼!npm install会自动关闭cmd吗?看来,还是要用python做批处理。
  2. 在Windows下,如果需要用ssh连接git,可以在本地目录下建立一个.ssh文件夹,然后里面放上如下ssh key文件,并利用config文件配置好不同git使用的不同ssh key

但是,要使用本地的.ssh,需要为windows配置HOME环境变量。方法如下

1
set HOME=.

或者一劳永逸

1
setx HOME=. # 直接永久加入到User的环境变量

至此,blog搬家完毕,后面慢慢note code, note life……