使用hexo-abbrlink插件生成永久短链接

为了解决Hexo默认生成的链接,在修改文件名后,链接会跟这变,如果旧链接在其他地方有被引用,访问旧链接的响应为404错误。且链接为日期 + 目录,层级太多,不利于SEO。

因此急需一种生成永久短链接的方案!

生成链接的默认配置

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

:title 指的是文章在_posts目录下的相对路径,不包含文件名后缀。

该配置会生成像下面这样的链接:

  • 文章在_posts目录下
1
http://localhost:4000/2021/05/24/hello-world/
  • 文章在_posts/git目录下

image-20210530103935067

如果文件名为中文,复制的URL是URL编码后的URL,这样不仅显示得很长,不易阅读,而且还不不易于SEO(通常搜索引擎只会爬取URL前三层)。

1
http://localhost:4000/2021/05/24/git/Git%20LFS%E4%BD%BF%E7%94%A8%E9%81%87%E5%88%B0%E7%9A%84%E9%97%AE%E9%A2%981/

缺陷

  • 修改文件名后,链接会跟这变。如果旧链接在其他地方有被引用,访问旧链接的响应为404错误。
  • 日期 + 目录,层级太多,不利于SEO。

安装

直接看hexo-abbrlink的 README

配置

你可以使用README中的配置。

下面是我的配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# permalink: ':year/:month/:day/:title/'
# 生成的文件在public目录下面
# permalink: :abbrlink/
# 生成的文件在p目录下面
permalink: p/: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: true #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.

配置完成后先执行 hexo clean ,然后执行 hexo g 重新生成。

它会在每篇MarkDown的front matter中添加 abbrlink,例如:

1
2
3
4
5
6
7
8
9
10
11
12
---
title: 使用hexo-abbrlink插件生成永久短链接
typora-copy-images-to: upload
categories:
- Blog
- Hexo
abbrlink: fcaf5192
date: 2021-06-01 12:52:35
tags:
- Blog
- Hexo
---

我的配置生成的链接如下:

1
2
http://localhost:4000/p/bb533952/
http://localhost:4000/p/5a8bb790/

问题

此插件我也发现了一个问题:如果没有title,生成的abbrlink为’0’ #56

使用hexo-abbrlink插件生成永久短链接

https://ganzhixiong.com/p/fcaf5192/

Author

干志雄

Posted on

2021-06-01

Updated on

2021-06-01

Licensed under

Comments