给Hexo博客文章固定一个永久链接

最近在使用Hexo博客框架构建博客,感觉还是非常方便的,和很多的博客程序一样,Hexo博客也支持对文章链接的修改,虽然默认的Hexo博客链接并没有太多的问题,例如hexo 文章链接默认的生成规则是:year/:month/:day/:title,即按照年、月、日、标题的顺序,小问题在于:

  1. 时间是基于文件时间的,文件的创建更新时间是可变的,包括文章标题和文件名都可变,如果修改之后链接就会变,更新部署之后原来的链接就会失效,就像我前几篇公众号的阅读原文链接打不开。
  2. 当文件名为中文时,会导致 url 链接中也出现中文,中文会转码,写公众号的时候填原文链接就是非常长的一串符号。

安装hexo-abbrlink插件

1
npm install hexo-abbrlink --save

配置hexo-abbrlink插件

插件安装好后,我们需要修改_cofing.yml中的永久链接:

1
permalink: artciles/:year/:abbrlink/

在_config.yml添加配置abbrlink配置:

然后在配置文件的适当位置,添加如下代码

1
2
3
4
5
6
7
8
9
10
11
12
13
## 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: true
auto_title: true #enable auto title, it can auto fill the title by path
auto_date: true #enable auto date, it can auto fill the date by time today

来看一下abbrlink算法效果示例:

如果您对代码的设置有问题,特别是链接的样式,不妨看一下下面的文件说明

1
2
3
4
5
6
7
8
crc16 & hex
https://test.com/artciles/55c6.html
crc16 & dec
https://test.com/artciles/43212.html
crc32 & hex
https://test.com/artciles/6ec16a2c.html
crc32 & dec
https://test.com/artciles/1521457752.html

hexo-abbrlink插件自动添加Front-matter

hexo-abbrlink除了永久链接的作用之外,新建post还可以自动添加Front-matter,只要上述配置中的auto写入true即可。但是自动添加的没那么全面,所以我就动手增加了我需要的两个选项,

  1. 来看本文Front-matter示例:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    ---
    cover: 'https://pic1.zhimg.com/v2-f5ca1150bf22dd77e636b0e2fced6d1c_1440w.jpg?source=172ae18b'
    top_img: transparent
    title: hexo自动刷新,永久链接,自动添加文章Front-matter
    date: '2024-05-16 20:08:22'
    categories:
    - hexo
    tags:
    - hexo
    - hexo插件
    abbrlink: bda0f54f
    ---
  2. 添加post其他需要的Front-matter。其中,npm包是不建议修改node_modules原包的,不过为了方便,我就写文记录了一下 添加至hexo-abbrlink——lib——logic.js第51行,tmpPost.abbrlink = abbrlink;的后面:

    1
    2
    3
    4
    // 自定义:post页面封面图:空,tags:未定义,顶部图:透明
    tmpPost.cover = '';
    tmpPost.tags = ['未定义标签'];
    tmpPost.top_img = 'transparent';

    修改好后hexo s运行,创建一篇文章即可自动添加abbrlink和其他Front-matter

希望能够帮助大家,如果您对Hexo博客插件安装遇到过问题,可以搜一下本站的其他文章,估计会帮助到您!