1.问题描述

在上传了两篇博客之后发现,假如不自行管理的话,所有的文章都会在source/_post目录下,这样不便于查看和修改。

因此想要在_post目录下建立子目录,之后再添加分类插件,使得在该目录下新建的文章都能够自动添加分类

因此,我想要实现的目的就是

hexo根据_post中的文件夹自动为文章生成分类

2.实现步骤

2.1 安装分类插件

命令行中运行如下命令

1
npm install hexo-auto-category --save

修改项目根目录下的_config.yml文件。向该文件中加入如下内容

1
2
3
4
5
6
7
# Generate categories from directory-tree
# Dependencies: https://github.com/xu-song/hexo-auto-category
# depth: the depth of directory-tree you want to generate, should > 0
auto_category:
enable: true
multiple: false
depth:

2.2 新建测试文件

1.在_posts文件下新建 dir1\dir2 二级文件夹如下

2.在命令行中输入如下命令,以此在dir2下新建一个test文件。

格式为

1
hexo n "文章名" -p 一级目录/二级目录/文件名

即如下

1
hexo n "test" -p dir1/dir2/test

出现如图结果,新建成果

此时test.md文件中内容如下,并无分类和标签

1
2
3
4
5
6
---
title: test
date: 2024-12-22 15:46:32
tags:
---

2.3 编译运行查看效果

命令行中输入hexo clean && hexo g 命令

此时发现2.2 中新建的test.md文件内容已经改变为如下

1
2
3
4
5
6
7
8
9
---
title: test
categories:
- dir1
- dir2
date: 2024-12-22 15:46:32
tags:
---

成功加入标签。