本篇为常用的《hexo命令》笔记
涵盖了 编译、预览、部署、新建文章 等基本命令
收录了 文章属性,分类与标签、hexo特有标签插件等基本语法与规则

- 若无hexo项目,请花5分钟移步文章【Post not found: build-hexo-project 从零构建hexo网站项目发布】 创建项目。
写作
新建文章/界面
- 新建文章或者页面Tips:默认为 post布局,可以通过修改 _config.yml 中的 default_layout 参数来指定默认布局。
1
hexo new [layout] <title>
layout 路径 post source/_posts page source draft source/_drafts
新建草稿
draft布局为草稿,草稿默认不会显示在页面中
1 | hexo new draft <title> |
您可通过 publish 命令将草稿移动到 source/_posts 文件夹
1 | hexo publish [layout] <title> |
模板
在新建文章时,Hexo 会根据 scaffolds 文件夹内相对应的文件来建立文件,例如:
1 | hexo new test "my test" |
在执行这行指令时,Hexo 会尝试在 scaffolds 文件夹中寻找 test.md,并根据其内容建立文章,
以下是您可以在模版中使用的变量:
变量 | 描述 |
---|---|
layout | 布局 |
title | 标题 |
date | 文件建立日期 |
基本属性
文章属性 Front-matter
Front-matter 是文件最上方以 ---
分隔的区域,用于指定个别文件的变量,举例来说:
1 | --- |
以下是预先定义的参数,您可在模板中使用这些参数值并加以利用。
参数 | 描述 | 默认值 |
---|---|---|
layout | 布局 | 此处请参考 _layout.yml配置 |
title | 标题 | 文章的文件名 |
date | 建立日期 | 文件建立日期 |
updated | 更新日期 | 文件更新日期 |
comments | 开启文章的评论功能 | true |
tags | 标签(不适用于分页) | |
categories | 分类(不适用于分页) | |
permalink | 覆盖文章网址 | |
excerpt | 纯文本的页面摘录。使用此插件格式化文本 | |
disableNunjucks | 启用时禁用Nunjucks标记{{ }}/{% %} 和标记插件的呈现 |
|
lang | 设置语言以覆盖自动检测 | 继承自_config.yml |
分类和标签
只有文章支持分类和标签,您可以在 Front-matter 中设置。
在其他系统中,分类和标签听起来很接近,但是在 Hexo 中两者有着明显的差别:
分类具有顺序性和层次性,也就是说 Foo, Bar 不等于 Bar, Foo;
而标签没有顺序和层次。
1 | categories: |
分类方法的分歧
如果您有过使用 WordPress 的经验,就很容易误解 Hexo 的分类方式。
WordPress 支持对一篇文章设置多个分类,而且这些分类可以是同级的,也可以是父子分类。
但是 Hexo 不支持指定多个同级分类。下面的指定方法:
1
2
3 categories:
- Diary
- Life以上会使分类Life成为Diary的子分类,而不是并列分类。因此,有必要为您的文章选择尽可能准确的分类。
如果你需要为文章添加多个分类,可以尝试以下 list 中的方法。
1
2
3
4 categories:
- [Diary, PlayStation]
- [Diary, Games]
- [Life]此时这篇文章同时包括三个分类: PlayStation 和 Games 分别都是父分类 Diary 的子分类,
同时 Life 是一个没有子分类的分类。
文件名称
Hexo 默认以标题做为文件名称,但您可编辑 new_post_name
参数来改变默认的文件名称,
举例来说,设为 :year-:month-:day-:title.md
可让您更方便的通过日期来管理文章。
变量 | 描述 | |
---|---|---|
:title | 标题(小写,空格将会被替换为短杠) | |
:year | 建立的年份 | 比如, 2015 |
:month | 建立的月份(有前导零) | 比如, 04 |
:i_month | 建立的月份(无前导零) | 比如, 4 |
:day | 建立的日期(有前导零) | 比如, 07 |
:i_day | 建立的日期(无前导零) | 比如, 7 |
标签插件
标签插件和 Front-matter 中的标签不同,它们是用于在文章中快速插入特定内容的插件。
虽然你可以使用任何格式书写你的文章,但是标签插件永远可用,且语法也都是一致的。ps:个人认为这部分的学习成本较高,有特殊需求使用即可,其他情况建议使用Markdown
引用块
- 在文章中插入引言,可包含作者、来源和标题。
1
2
3{% blockquote [author[, source]] [link] [source_link_title] %}
content
{% endblockquote %} - 举例
- 只输出普通的blockquote(无参)效果:
1
2
3{% blockquote %}
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
{% endblockquote %}Lorem ipsum dolor sit amet, consectetur adipiscing elit.
- 引用书上的句子(有参)效果:
1
2
3{% blockquote David Levithan, Wide Awake %}
Do not just seek happiness for yourself. Seek happiness for all. Through kindness. Through mercy.
{% endblockquote %}Do not just seek happiness for yourself. Seek happiness for all. Through kindness. Through mercy.
- 引用网上的文章效果:
1
2
3{% blockquote Seth Godin http://sethgodin.typepad.com/seths_blog/2009/07/welcome-to-island-marketing.html Welcome to Island Marketing %}
Every interaction is both precious and an opportunity to delight.
{% endblockquote %}Every interaction is both precious and an opportunity to delight.
- 只输出普通的blockquote(无参)
代码块
在文章中插入代码。
1
2
3{% codeblock [title] [lang:language] [url] [link text] [additional options] %}
code snippet
{% endcodeblock %}额外选项 说明 默认值 line_number 显示行号 true highlight 启用代码突出显示 true first_line 定第一行号 1 wrap 将代码块包装在 <table>
中true mark 突出显示特定行,每个值用逗号分隔。使用破折号指定数字范围 示例:mark:1,4-7,10将标记第1、4至7和10行 ps:我这里认为此处语法与Markdown中的``````类似举例
- 普通的代码块效果:
1
2
3{% codeblock %}
alert('Hello World!');
{% endcodeblock %}1
alert('Hello World!');
- 指定语言效果:
1
2
3{% codeblock lang:objc %}
[rectangle setX: 10 y: 10 width: 20 height: 20];
{% endcodeblock %}1
[rectangle setX: 10 y: 10 width: 20 height: 20];
- 附加说明效果:
1
2
3{% codeblock Array.map %}
array.map(callback[, thisArg])
{% endcodeblock %}Array.map 1
array.map(callback[, thisArg])
- 附加说明和网址效果:
1
2
3
4{% codeblock _.compact http://underscorejs.org/#compact Underscore.js %}
_.compact([0, 1, false, 2, '', 3]);
=> [1, 2, 3]
{% endcodeblock %}_.compactUnderscore.js 1
2_.compact([0, 1, false, 2, '', 3]);
=> [1, 2, 3]
- 普通的代码块
反引号代码块
1 | ``` [language] [title] [url] [link text] code snippet ``` |
Pull Quote
在文章中插入 Pull quote。
1 | {% pullquote [class] %} |
jsFiddle
1 | {% jsfiddle shorttag [tabs] [skin] [width] [height] %} |
Gist
1 | {% gist gist_id [filename] %} |
iframe
1 | {% iframe url [width] [height] %} |
Image
1 | {% img [class names] /path/to/image [width] [height] '"title text" "alt text"' %} |
Link
1 | {% include_code [title] [lang:language] [from:line] [to:line] path/to/file %} |
Include Code
- 插入 source/downloads/code 文件夹内的代码文件。
source/downloads/code 不是固定的,取决于你在配置文件中 code_dir 的配置。1
{% include_code [title] [lang:language] [from:line] [to:line] path/to/file %}
- 举例
- 嵌入 test.js 文件全文
1
{% include_code lang:javascript test.js %}
- 只嵌入第 3 行
1
{% include_code lang:javascript from:3 to:3 test.js %}
- 嵌入第 5 行至第 8 行
1
{% include_code lang:javascript from:5 to:8 test.js %}
- 嵌入第 5 行至文件结束
1
{% include_code lang:javascript from:5 test.js %}
- 嵌入第 1 行至第 8 行
1
{% include_code lang:javascript to:8 test.js %}
引用文章
- 嵌入 test.js 文件全文
- 引用其他文章的链接。
1
2{% post_path filename %}
{% post_link filename [title] [escape] %} - 在使用此标签时可以忽略文章文件所在的路径或者文章的永久链接信息、如语言、日期。
- 例如,在文章中使用
{% post_link aaaaa %}
时,只需有一个名为aaaaa.md
的文章文件即可。 - 即使这个文件位于站点文件夹的 source/posts/2015-02-my-family-holiday 目录下、或者文章的永久链接是 2018/en/how-to-bake-a-cake,都没有影响。
- 默认链接文字是文章的标题,你也可以自定义要显示的文本。
- 默认对文章的标题和自定义标题里的特殊字符进行转义。可以使用escape选项,禁止对特殊字符进行转义。
- 举例
- 链接使用文章的标题效果: Post not found: build-hexo-project
1
{% post_link build-hexo-project %}
- 链接使用自定义文字效果: Post not found: build-hexo-project 从零构建hexo网站项目发布
1
{% post_link build-hexo-project '从零构建hexo网站项目发布' %}
- 对标题的特殊字符进行转义效果: Post not found: build-hexo-project How to use tag in title
1
{% post_link build-hexo-project 'How to use <b> tag in title' %}
- 禁止对标题的特殊字符进行转义效果: Post not found: build-hexo-project bold custom title false
1
{% post_link build-hexo-project '<b>bold</b> custom title' false %}
引用资源
引用文章的资源。1
2
3{% asset_path filename %}
{% asset_img [class names] slug [width] [height] [title text [alt text]] %}
{% asset_link filename [title] [escape] %}嵌入图片
- 链接使用文章的标题
- 默认(无选项)效果:
1
{% asset_img foo.jpg %}
1
<img src="/2020/01/02/hello/foo.jpg">
- 自定样式效果:
1
{% asset_img post-image foo.jpg %}
1
<img src="/2020/01/02/hello/foo.jpg" class="post-image">
- 显示大小效果:
1
{% asset_img foo.jpg 500 400 %}
1
<img src="/2020/01/02/hello/foo.jpg" width="500" height="400">
- 标题和替换效果:
1
{% asset_img logo.svg "lorem ipsum'dolor'" %}
1
<img src="/2020/01/02/hello/foo.jpg" title="lorem ipsum" alt="dolor">
文章摘要和截断
1 | <!-- more --> |
在文章中使用 <!-- more -->
,那么<!-- more -->
之前的文字将会被视为摘要。
首页中将只出现这部分文字,同时这部分文字也会出现在正文之中。
Raw
1 | {% raw %} |
编译 预览 部署
编译
1 | hexo generate |
选项 | 描述 | |
---|---|---|
-d | –deploy | 文件生成后立即部署网站 |
-w | –watch | 监视文件变动 |
-b | –bail | 生成过程中如果发生任何未处理的异常则抛出异常 |
-f | –force | 强制重新生成文件,Hexo 引入了差分机制,如果 public 目录存在,那么 hexo g 只会重新生成改动的文件。 使用该参数的效果接近 hexo clean && hexo generate |
-c | –concurrency | 最大同时生成文件的数量,默认无限制 |
清除缓存
1 | hexo clean |
预览
1 | hexo server |
- 此命令将启动服务器在本地预览博客
- 默认情况访问网址为http://localhost:4000/
选项 | 描述 | |
---|---|---|
-p | –port | 重设端口 |
-s | –static | 只使用静态文件 |
-l | –log | 启动日记记录,使用覆盖记录格式 |
部署
1 | hexo deploy |
选项 | 描述 | |
---|---|---|
-g | –generate | 部署之前预先生成静态文件 |
调用此目录前需在
_config.yml
中进行部署配置例如部署到git
1
2deploy:
type: git同时部署到git & heroku
1
2
3
4
5deploy:
type: git
repo:
type: heroku
repo:部署到git配置详解
1
2
3
4
5deploy:
type: git
repo: <repository url> #https://bitbucket.org/JohnSmith/johnsmith.bitbucket.io
branch: [branch]
message: [message]参 数 描 述 默认 repo 库(Repository)地址 branch 分支名称 gh-pages(GitHub)
coding-pages(Coding.net)
master (others)
message 自定义提交信息 Site updated: {{now('YYYY-MM-DD HH:mm:ss')}})
token 用于与仓库进行身份验证的可选令牌值。
带$的前缀以从环境变量读取令牌以本项目举例配置为
1
2
3
4deploy:
type: git
repo: git@github.com:wx98/wx98.github.io.git
branch: master注意:若部署至git则需安装名为
hexo-deployer-git
的插件,否则会报如下错误:1
Deployer not found: git
安装只需执行如下代码
1
npm install hexo-deployer-git --save
安装完毕后执行
hexo g
完成部署Tips:可以将部署写成一个脚本简化流程,例如:
1
2
3hexo clean
hexo generate
hexo deploy
文章到此就结束啦,感谢查阅。