欢迎访问我的网站与公众号!点击与扫码即可进入,谢谢关注!

写前端的时候基本框架都有自动刷新功能,hexo本身不自带,需要插件;另外hexo更新部署的时候,固定链接不固定;每次新建一篇post文章需要手动添加Front-matter,比如标题、分类、标签、时间等。根据这几个问题本篇文章简单解决下。

自动刷新

修改文章内容hexo服务会自动刷新页面,需要一个hexo-browsersync

  1. 安装:npm install hexo-browsersync
  2. 安装完hexo s启动服务即可

永久链接

hexo 文章链接默认的生成规则是::year/:month/:day/:title,即按照年、月、日、标题的顺序,问题在于:

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

配置

_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

自动添加Front-matter

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

本文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
---

添加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