Hexo写作的基本语法

本文最后更新于:2023年10月31日 上午

最最最开始,当然是建立一个.md文档啦,使用以下命令:

1
$ hexo n [layout] <title>

如果添加环境变量总是失败,则使用以下命令:

1
2
$ npx hexo n [layout] <title>
# 作为一个程序猿,你该懂得[]里的是可选参数(我一开始也不懂),layout详见[1]的Hexo官方注解

现在,你已经拥有第一篇博客文章(的毛坯)了!

Hexo本身支持的特色语法[1]

便签

Hexo 默认安装了 hexo-renderer-marked 和 hexo-renderer-ejs ,因此你不仅可以用 Markdown 写作,你还可以用 EJS 写作。如果你安装了 hexo-renderer-pug ,你甚至可以用 Pug 模板语言书写文章。只需要将文章的扩展名从 md 改成 ejs ,Hexo 就会使用 hexo-renderer-ejs 渲染这个文件,其他格式同理。

以上使用了Fluid主题的便签特性。

使用 HTML 形式:

<p class="note note-primary">标签</p>

便签种类

引用块Quote:

基于Markdown的区块引用:

1
> cite

cite

在文章中插入引言,可包含作者、来源和标题。

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. Pellentesque hendrerit lacus ut purus iaculis feugiat. Sed nec tempor elit, quis aliquam neque. Curabitur sed diam eget dolor fermentum semper at eu lorem.
{% endblockquote %}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque hendrerit lacus ut purus iaculis feugiat. Sed nec tempor elit, quis aliquam neque. Curabitur sed diam eget dolor fermentum semper at eu lorem.

引用书上的句子

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.

David LevithanWide Awake

引用 Twitter

1
2
3
{% blockquote @DevDocs https://twitter.com/devdocs/status/356095192085962752 %}
NEW: DevDocs now comes with syntax highlighting. http://devdocs.io
{% endblockquote %}

NEW: DevDocs now comes with syntax highlighting. http://devdocs.io

引用网络上的文章

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.

引用文章

引用其他文章的链接。

1
2
{% post_path filename %}
{% post_link filename [title] [escape] %}

在使用此标签时可以忽略文章文件所在的路径或者文章的永久链接信息、如语言、日期。

例如,在文章中使用 {% post_link how-to-bake-a-cake %} 时,只需有一个名为 how-to-bake-a-cake.md 的文章文件即可。即使这个文件位于站点文件夹的 source/posts/2015-02-my-family-holiday 目录下、或者文章的永久链接是 2018/en/how-to-bake-a-cake,都没有影响。

默认链接文字是文章的标题,你也可以自定义要显示的文本。

默认对文章的标题和自定义标题里的特殊字符进行转义。可以使用 escape 选项,禁止对特殊字符进行转义。

例如:

链接使用文章的标题

1
{% post_link hexo-3-8-released %}

Hexo 3.8.0 Released

链接使用自定义文字

1
{% post_link hexo-3-8-released '通往文章的链接' %}

通往文章的链接

对标题的特殊字符进行转义

1
{% post_link hexo-4-released 'How to use <b> tag in title' %}

How to use tag in title

禁止对标题的特殊字符进行转义

1
{% post_link hexo-4-released '<b>bold</b> custom title' false %}

bold custom title

语法相对来说繁杂,非必要不建议使用

代码块Code:

最常用的是Markdown方法插入代码:

`code`

code

```code```

1
code

Gist

在文章中嵌入 Gist。

1
<script src="https://gist.githubusercontent.com/MegaSuite/aaa482f9062bc94ec61f1eecef329d1d/raw/57f8bfc04945d46e924f904bcb2ec0f1e6b3aca8/gistfile1.txt"></script>

部分网络环境下,Gist无法正常加载

iframe[2]

在文章中插入 iframe。

1
<iframe src="https://hexo.fluid-dev.com/" width="100%" height="500" name="topFrame" scrolling="yes"  noresize="noresize" frameborder="0" id="topFrame"></iframe>

Fluid开发者官方博客,部分网络环境可能加载失败

Bilibili

以B站为例,去相应播放页面复制代码即可

1
<iframe src="//player.bilibili.com/player.html?aid=53851218&bvid=BV1j4411W7F7&cid=94198756&page=1&autoplay=0" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true"> </iframe>

文章摘要和截断

在文章中使用 <!-- more -->,那么 <!-- more --> 之前的文字将会被视为摘要。首页中将只出现这部分文字,同时这部分文字也会出现在正文之中。

注意,摘要会被 Front Matter 中的 excerpt 覆盖。

Tips

文件名称

Hexo 默认以标题做为文件名称,但您可编辑 new_post_name 参数来改变默认的文件名称,举例来说,设为 :year-:month-:day-:title.md 可让您更方便的通过日期来管理文章。


分类方法的分歧

如果您有过使用 WordPress 的经验,就很容易误解 Hexo 的分类方式。WordPress 支持对一篇文章设置多个分类,而且这些分类可以是同级的,也可以是父子分类。但是 Hexo 不支持指定多个同级分类。下面的指定方法:

1
2
3
categories:
- Diary
- Life

会使分类 Life 成为 Diary 的子分类,而不是并列分类。因此,有必要为您的文章选择尽可能准确的分类。

如果你需要为文章添加多个分类,可以尝试以下 list 中的方法。

1
2
3
4
categories:
- [Diary, PlayStation]
- [Diary, Games]
- [Life]

此时这篇文章同时包括三个分类: PlayStationGames 分别都是父分类 Diary 的子分类,同时 Life 是一个没有子分类的分类。


以上仅陈述个人认为比较常用或者比较有用的部分语法,具体细致解释参见Hexo和Hexo-Fluid作者博客。

参考


Hexo写作的基本语法
https://www.0co.dev/hexo-grammars/
作者
Konrad Gerrens
发布于
2023年7月3日
许可协议