hexo的一些指令

下面是hexo的一些常用指令,做个记录:

1
2
3
4
5
hexo new "postName" #新建文章
hexo new page "pageName" #新建页面
hexo generate #生成静态页面至public目录
hexo server #开启预览访问端口(默认端口4000,'ctrl + c'关闭server)
hexo deploy #将.deploy目录部署到GitHub

常用复合命令:

1
2
hexo deploy -g
hexo server -g

简写:

1
2
3
4
hexo n == hexo new
hexo g == hexo generate
hexo s == hexo server
hexo d == hexo deploy

目录介绍

默认目录结构:

1
2
3
4
5
6
7
8
9
10
11
12

.
├── .deploy
├── public
├── scaffolds
├── scripts
├── source
| ├── _drafts
| └── _posts
├── themes
├── _config.yml
└── package.json

目录解释

.deploy:执行hexo deploy命令部署到GitHub上的内容目录
public:执行hexo generate命令,输出的静态网页内容目录
scaffolds:layout模板文件目录,其中的md文件可以添加编辑
scripts:扩展脚本目录,这里可以自定义一些javascript脚本
source: 文章源码目录,该目录下的markdown和html文件均会被hexo处理。
该页面对应 repository的根目录,
404文件,favicon.ico文件,CNAME文件等都应该放这里,该目录下可新建页面目录。
_drafts:草稿文章
_posts:发布文章
themes:主题文件目录
_config.yml:全局配置文件,大多数的设置都在这里
package.json:应用程序数据,指明hexo的版本等信息,类似于一般软件中的关于按钮

修改局部页面

页面展现的全部逻辑都在每个主题中控制,源代码在hexo\themes\你使用的主题\中,以modernist主题为例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
'
.
├── languages #多语言
| ├── default.yml #默认语言
| └── zh-CN.yml #中文语言
├── layout #布局,根目录下的*.ejs文件是对主页,分页,存档等的控制
| ├── _partial #局部的布局,此目录下的*.ejs是对头尾等局部的控制
| └── _widget #小挂件的布局,页面下方小挂件的控制
├── source #源码
| ├── css #css源码
| | ├── _base #*.styl基础css
| | ├── _partial #*.styl局部css
| | ├── fonts #字体
| | ├── images #图片
| | └── style.styl #*.styl引入需要的css源码
| ├── fancybox #fancybox效果源码
| └── js #javascript源代码
├── _config.yml #主题配置文件
└── README.md #用GitHub的都知道
'

如果你需要修改头部,直接修改 hexo\themes\modernist\layout_partial\header.ejs,比如头上加个搜索框:

1
2
3
4
5
6
7

<div>
<form class="search" action="//google.com/search" method="get" accept-charset="utf-8">
<input type="search" name="q" id="search" autocomplete="off" autocorrect="off" autocapitalize="off" maxlength="20" placeholder="Search" />
<input type="hidden" name="q" value="site:<%- config.url.replace(/^https?:\/\//, '') %>">
</form>
</div>

将如上代码加入即可,您需要修改css以便这个搜索框比较美观。

再如,你要修改页脚版权信息,直接编辑 hexo\themes\modernist\layout_partial\footer.ejs。同理,你需要修改css,直接去修改对应位置的style文件。

统计

页面上显示访问次数可以使用 不蒜子,两行代码即可搞定。

因Google Analytics偶尔被墙,故用百度统计,以modernist主题为例,介绍如何添加。
编辑文件 hexo\themes\modernist_config.yml,增加配置选项:

1
baidu_tongji: true

新建文件 hexo\themes\modernist\layout_partial\baidu_tongji.ejs,内容如下:

1
2
3
4
5
<% if (theme.baidu_tongji){ %>
<script type="text/javascript">
#你的百度统计代码
</script>
<% } %>

注册并登录百度统计获取你的统计代码。

编辑文件 hexo\themes\modernist\layout_partial\head.ejs,在
『/head』之前增加:

1
<%- partial('baidu_tongji') %>

重新生成并部署你的站点。

不出意外的话,在你的站点的每个页面的左上角都会看到一个恶心的百度LOGO。你只能在『百度统计首页->网站列表->获取代码->系统管理设置->统计图标设置->显示图标』,把那个勾去掉。百度真是恶心,我准备还是用 Google Analytics

分享

以加网为例介绍如何添加:

在hexo\themes\modernist\layout_partial\post下新建jiathis.ejs文件。
注册加网获得你的分享代码,写入jiathis.ejs。
在hexo\themes\modernist\layout_partial\article.ejs中,添加<%-partial(‘post/jiathis’)%>。

分享服务还可以使用如下企业提供的技术加网百度分享

网站图标

看一下 hexo\themes\modernist\layout_partial\head.ejs,找到这句:

1
<link rel="icon" type="image/x-icon" href="<%- config.root %>favicon.ico">

你懂的,将你的favicon.ico放到工程根目录下即可,也就是hexo\source目录。可以在Faviconer制作你的ico图标,国内有比特虫

自定义挂件

除了默认已提供的挂件外,你还可以自定义自己的小挂件,在 hexo\themes\modernist\layout_widget\ 下,新建自己的ejs文件,如myWidget.ejs,然后在配置文件 hexo\themes\modernist_config.yml中配置。

1
2
widgets:
- myWidget

用上述方法可以添加新浪微博小挂件。

生成自己的微博组件
添加hexo\themes\modernist\layout_widget\weibo.ejs文件。
配置hexo\themes\modernist_config.yml。

以上部分信息转载来自 hexo你的博客;