YULIU 2015-07-31
以前的博客是使用Jekyll托管在github上,后来用着越来越不方便,比如没有自动生成post,不能一键部署,文件结构和配置也是比较繁琐,更重要的是有时候用markdown写一篇文章,生成的静态文件很乱,应该是有些字符解析的问题。现在下决心换成了hexo。
因为hexo是基于nodejs的应用,所以要先安装nodejs才可以。我这里以Ubuntu为例,因为我自己一直在使用ubuntu。使用如下命令即可:
<span class="pln">sudo apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> install nodejs</span>
<span class="pln">sudo apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> install npm</span>
Hexo 是一个功能强大的静态网站生成系统,快速、简洁、高效。Hexo 支持 GitHub Flavored Markdown 的所有功能,甚至可以整合 Octopress 的大多数插件。可以一键部署到github,还有丰富的插件和主题,还支持热部署哦。执行如下命令进行安装。
<span class="pln">sudo npm install hexo</span><span class="pun">-</span><span class="pln">cli </span><span class="pun">-</span><span class="pln">g</span>
安装Hexo安成后,使用如下命令快速新建一个博客系统,然后运行它:
<span class="pln">hexo init blog</span>
<span class="pln">cd blog</span>
<span class="pln">npm install</span>
<span class="pln">hexo server</span>
如果npm安装失败,请使用sudo安装。运行成功后,访问 http://0.0.0.0:4000/ 就可以看到博客的样子的,对就是我现在的博客这个样子,o(∩∩)o...哈哈。
现在我们来看看Hexo 生成博客的目录结构:
<span class="pun">.</span>
<span class="pun">├──</span><span class="pln"> _config</span><span class="pun"><</span><span class="pln">span </span><span class="kwd">class</span><span class="pun">=</span><span class="str">"class"</span><span class="pun">>.</span><span class="pln">yml</span><span class="pun"></</span><span class="pln">span</span><span class="pun">></span>
<span class="pun">├──</span><span class="pln"> db</span><span class="pun"><</span><span class="pln">span </span><span class="kwd">class</span><span class="pun">=</span><span class="str">"class"</span><span class="pun">>.</span><span class="pln">json</span><span class="pun"></</span><span class="pln">span</span><span class="pun">></span>
<span class="pun">├──</span><span class="pln"> node_modules</span>
<span class="pun">├──</span><span class="kwd">package</span><span class="pun"><</span><span class="pln">span </span><span class="kwd">class</span><span class="pun">=</span><span class="str">"class"</span><span class="pun">>.</span><span class="pln">json</span><span class="pun"></</span><span class="pln">span</span><span class="pun">></span>
<span class="pun">├──</span><span class="kwd">public</span>
<span class="pun">├──</span><span class="pln"> scaffolds</span>
<span class="pun">├──</span><span class="pln"> source</span>
<span class="pun">└──</span><span class="pln"> themes</span>
其中_config.yml是配置站点的文件,public是hexo生成的静态站点文件夹,scaffolds是模板文件夹,source是存在用户资源的文件夹,themes是主题文件夹。
找到title,subtitle,author参数配置,分别配置站点的标题、副标题和作者,比如我这里是:
<span class="pln">title</span><span class="pun">:</span><span class="pun">飞雪无情的博客</span>
<span class="pln">subtitle</span><span class="pun">:</span><span class="pun">专注于</span><span class="typ">Android</span><span class="pun">、</span><span class="typ">Java</span><span class="pun">、移动互联网、项目管理、软件架构</span>
<span class="pln">description</span><span class="pun">:</span>
<span class="pln">author</span><span class="pun">:</span><span class="pun">飞雪无情</span>
<span class="pln">language</span><span class="pun">:</span><span class="pln"> zh</span><span class="pun">-</span><span class="pln">CN</span>
然后配置站点的url和permalink,这两个分别是你的站点的url host地址以及文章的永久连接,我这里是:
<span class="pln">url</span><span class="pun">:</span><span class="pln"> http</span><span class="pun">:</span><span class="com">//www.flysnow.org</span>
<span class="pln">root</span><span class="pun">:</span><span class="pun">/</span>
<span class="pln">permalink</span><span class="pun">:</span><span class="pun">:</span><span class="pln">year</span><span class="pun">/:</span><span class="pln">month</span><span class="pun">/:</span><span class="pln">day</span><span class="pun">/:</span><span class="pln">title</span><span class="pun">.</span><span class="pln">html</span>
<span class="pln">permalink_defaults</span><span class="pun">:</span>
permalink 我配置的是年月日以及title,后缀是html,便于搜索收录。permalink详情参见: http://hexo.io/zh-cn/docs/permalinks.html
使用如下命令即可新增一篇md格式的文章:
<span class="pln">hexo </span><span class="kwd">new</span><span class="str">'github-page-with-hexo'</span>
然后就会在sources/_posts生成一篇文件名为github-page-with-hexo.md的markdown文件。编辑该文件就可以写博客了。这里有一些Front-matter需要介绍,可以配置文章使用的模板、所属的分类和tag等。
Front-matter 是文件最上方以 —- 分隔的区域,用于指定个别文件的变量,举例来说:
<span class="pln">title</span><span class="pun">:</span><span class="str">"使用Hexo在Github上搭建自己的博客"</span>
<span class="pln">date</span><span class="pun">:</span><span class="lit">2015</span><span class="pun">-</span><span class="lit">03</span><span class="pun">-</span><span class="lit">10</span><span class="lit">22</span><span class="pun">:</span><span class="lit">30</span><span class="pun">:</span><span class="lit">04</span>
<span class="pln">tags</span><span class="pun">:</span>
<span class="pun">-</span><span class="typ">Hexo</span>
<span class="pun">-</span><span class="typ">Github</span>
<span class="pln">categories</span><span class="pun">:</span>
<span class="pun">-</span><span class="typ">Hexo</span>
<span class="pun">---</span>
请注意,目前的categories只能有一个一级分类,如果填写多个,第二个会被解析为二级分类,以及类推。tags可以允许有多个。更多关于Front-matter请参考 http://hexo.io/zh-cn/docs/front-matter.html 。
首先你已经创建好了你的github page对应的git库,没有创建的可以google相关博客。然后新建一个hexo分支,存放你现在hexo的所有文件。然后执行如下命令清理并生成发布的静态站点文件。
<span class="pln">hexo clean</span>
<span class="pln">hexo generate</span>
然后把生成的public目录下的文件放到你的master分支下即可。git commit后把这两个分支推送到你的github上。git库结构可以参见我的github page库 https://github.com/rujews/rujews.github.io 。
然后等个几分钟,访问你的域名就可以看到你的网站了。如http://www.flysnow.org/ 。关于更多的Hexo请参考官方文档 http://hexo.io/zh-cn/docs/ 。