一、使用背景

在 Hexo 使用过程中,遇到一个问题:使用 Hexo 评论插件的时候,如何识别哪一条评论是哪一个文章页面的呢?答案是根据页面的 URL 来识别一个页面。

而 Hexo 默认生成文章链接公示如下:

1
permalink: ':year/:month/:day/:title/'

根据文章的一些属性,来生成链接。上面的年月日还好,是文章创建的时间,这个时间轻易不会更改。但是后面的文章标题,感觉修改的可能性有点大。而修改了标题之后,生成的文章链接就会跟着变,这样就会导致文章页面与评论无法对应。

所以需要一个插件,用来为每个文章生成固定的链接。

安装 abbrlink 插件。

1
$ cnpm install hexo-abbrlink --save

修改 Hexo 配置文件 _config.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
permalink: ':abbrlink/'

# 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.

可以选择算法:crc16 和 crc32,也可以选择进制:十进制或十六进制,可以按照需求选择。

之后在执行了 hexo clean && hexo generate 之后,会在文章顶部添加一条 abbrlink 配置,文章头示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
title: macOS将zsh用作默认Shell
categories: macOS
tags:
- macOS
- Shell
keywords:
- macOS
- Shell
- shell描述文件
description: 在macOS中默认的Shell为zsh(某个版本及之后),更换默认shell及shell的描述文件
abbrlink: 54261472
date: 2024-01-15 01:12:20
updated:

这样每一篇文章就有一个固定不变的 ID 了。

三、hexo-abbrlink2

该插件跟上面的 hexo-abbrlink 插件作用相同,都是会为文章生成一个固定的链接,但是生成的 ID 不是通过 crc 算法生成的串,而是自增序列,即 1、2、3、4 这种,所以如果需要使用有序增长的 ID 时可以使用该插件。

相关链接

rozbo/hexo-abbrlink: create one and only link for every post for hexo (github.com)

rozbo/hexo-abbrlink2 (github.com)

OB tags

#Hexo