前言

Hexo的默认文章链接格式是年,月,日,标题这种格式来生成的。如果你的标题是中文的话,那你的URL链接就会包含中文,复制后的URL路径就是把中文变成了一大堆字符串编码,如果你在其他地方用这边文章的url链接,偶然你又修改了改文章的标题,那这个URL链接就会失效。为了给每一篇文章来上一个属于自己的链接,写下此教程,利用 hexo-abbrlink 插件,A Hexo plugin to generate static post link based on post titles ,来解决这个问题。

安装插件

1
npm install hexo-abbrlink --save

配置文件_config.yml

修改config.yml文件中的永久链接:

1
2
3
permalink: posts/:abbrlink.html
# or
# permalink: posts/:abbrlink/

再修改设置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# permalink: :year/:month/:day/:title/
- permalink: :year/:month/:day/:title/
# abbrlink config
abbrlink:
alg: crc32 #support crc16(default) and crc32
rep: hex #support dec(default) and hex
drafts: false #(true)Process draft,(false)Do not process draft. false(default)
# Generate categories from directory-tree
# depth: the max_depth of directory-tree you want to generate, should > 0
auto_category:
enable: true #true(default)
depth: #3(default)
over_write: false
auto_title: false #enable auto title, it can auto fill the title by path
auto_date: false #enable auto date, it can auto fill the date by time today
force: false #enable force mode,in this mode, the plugin will ignore the cache, and calc the abbrlink for every post even it already had abbrlink. This only updates abbrlink rather than other front variables.

运行网站

运行网站后文章Front-matter处自动生成abbrlink: 79dba5d2唯一链接

1
2
3
INFO  Start processing
INFO Generate link [79dba5d2] for post [source/_posts/生成文章唯一链接.md][ 生成文章唯一链接 ]
INFO Generated: categories [undefined] for post [source/_posts/生成文章唯一链接.md][ 生成文章唯一链接 ]

得到文章链接如下:http://localhost:4000/post/79dba5d2.html