title: 日誌的自動分類插件
tags: hexo-auto-category
cover: ‘https://gitee.com/chuyuxuan/tuc/raw/master/hexo_auto_category/4.png’
categories:
- 技能
- hexo配置相關
date: 2020-02-08 21:00:00
簡介
Hexo寫日誌,通常我們都需要維護一個front-matter信息,包括title、date。博客多了,爲了方便日誌分類,一般還需要設置categories。
比如下面的例子:
這樣就會導致文章特別多而且繁雜,不好整理:
久而久之,就會發現很多問題:
- 工作繁瑣:大量的category是重複性工作
- 容易出錯:大小寫和中英文目錄有可能混雜。比如有個web目錄,偶爾我們寫成了Web,造成了目錄樹中冗餘的節點。
- 可維護性差:如果要更改目錄樹中的節點,就要手動更改每個日誌的categories變量。
所以,這裏介紹一個可以讓我們文章自動生成category
插件 hexo-auto-category,實現自動分類
自動生成 categories
最常用的文件管理策略,就是利用文件系統目錄結構(樹形結構 directory-tree)。
同樣,爲了便於管理大量的日誌文件,採用目錄結構是一種簡便可行的方案。hexo-auto-category根據日誌文件(Markdown)所在文件目錄自動分類,即自動生成markdown
的front-matter
中categories變量。
示例
對於博客 source/_post/web/framework/hexo.md
,該插件會自動生成以下categories:
categories:
- web
- framework
安裝
採用npm
安裝:
$ npm install hexo-auto-category --save
配置
在站點根目錄下的_config.yml添加:
# 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
depth:
如果只想生成第一級目錄分類,可以設置depth屬性,比如:
auto_category:
enable: true
depth: 1
編譯 & 部署
$ hexo clean ; hexo generate ; hexo deploy
大功告成!享受吧!