在Hexo发布好服务后,我们可以在浏览器输入http://<用户名>.github.io就可以访问我们的网站,但是我们在浏览文章的时候,会发现地址栏的因为有中文标题,url会变得很长,例如: https://devsu.top/2023/03/08/2020-07-10-Markdown%E7%94%A8%E6%B3%95/,复制访问的时候,非常不友好。

因此,我们在hexo插件中心,找到了 hexo-abbrlink 短链生成工具:

安装插件

npm install hexo-abbrlink --save

在Hexo根目录的_config.xml中修改permalink

默认的规则是:

permalink: year/:month/:day/:title/

修改为:

permalink: posts/:abbrlink/ 
# or
permalink: posts/:abbrlink.html

设置permalink生成的算法和进制

# 新增
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

短链配置

生成样例

官方短链生成的样例是:

crc16 & hex
https://post.zz173.com/posts/66c8.html

crc16 & dec
https://post.zz173.com/posts/65535.html

使用

在文章的Front-matter内新增abbrlink字段,值为生成的ID。这个字段确保了在我们修改了Front-matter 内的博客标题title或创建日期date字段之后而不会改变链接地址。如果不增加这个字段的话,我们的URL就会变成https://xxx/posts/undefined/,并且打开文章的链接不会跳转到对应的文章页。

---
title:
date:
categories:
-
tags:
-
abbrlink: #留空就好,会自动生成,并不会因下次更新文章而变动
---

因为每篇文章都需要修改abbrlink,因此我们需要修改Front-matter的统一设置,在Hexo根目录的scaffolds文件夹下,修改post.md的内容

front-matter设置