From 1e5f8eb33bc41cb59faf059e83701152785cabea Mon Sep 17 00:00:00 2001 From: yingyu5658 Date: Sat, 13 Dec 2025 08:33:08 +0800 Subject: Initial commit --- ...256\241\347\220\206\346\226\271\346\241\210.md" | 76 ++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 "content/posts/\350\277\231\345\217\257\350\203\275\346\230\257\346\234\200\345\245\275\347\232\204Hexo\345\233\276\347\211\207\347\256\241\347\220\206\346\226\271\346\241\210.md" (limited to 'content/posts/这可能是最好的Hexo图片管理方案.md') diff --git "a/content/posts/\350\277\231\345\217\257\350\203\275\346\230\257\346\234\200\345\245\275\347\232\204Hexo\345\233\276\347\211\207\347\256\241\347\220\206\346\226\271\346\241\210.md" "b/content/posts/\350\277\231\345\217\257\350\203\275\346\230\257\346\234\200\345\245\275\347\232\204Hexo\345\233\276\347\211\207\347\256\241\347\220\206\346\226\271\346\241\210.md" new file mode 100644 index 0000000..1ea52a0 --- /dev/null +++ "b/content/posts/\350\277\231\345\217\257\350\203\275\346\230\257\346\234\200\345\245\275\347\232\204Hexo\345\233\276\347\211\207\347\256\241\347\220\206\346\226\271\346\241\210.md" @@ -0,0 +1,76 @@ +--- +abbrlink: 1950788762 +categories: +- 往昔 +date: "2025-06-29 10:52:05" +tags: +- Hexo +title: 这可能是最好的Hexo图片管理方案 +--- + +## 前言 +通常在Hexo博客中,我们管理图片资源都有以下两种方案: + +1. 在`_post`目录下新建文章同名文件夹 +2. 在`source`目录下新建images文件夹,存放所有图片 + +两种方法各有优劣,前者方便查找但污染目录,后者集中管理但维护成本高。所以就诞生出本文要介绍的方法——`images`目录下新建文章同名目录 + +这是一个折中的办法,既保留方法1的查找方便,又保留方法2的集中性。 + +## 实现方法 + +**创建脚本文件** + +在Hexo根目录的`scripts`文件夹(若不存在则新建)下创建一个javascript脚本,我这里就命名为`auto-image-folder.js` + +```js +const fs = require("fs") +const path = require("path") + +hexo.on("new", function(data) { + const postName = path.basename(data.path, ".md") + const imageDir = path.join(hexo.source_dir, "images", postName) + + if(!fs.existsSync(imageDir)){ + fs.mkdirSync(imageDir, {recusive: true}) + } +}) +``` + +**效果测试** +```bash +hexo new "新文章" +``` + +这时候就在images目录下新建了一个与文章同名的文件夹。 + + +## 编辑器设置优化 + +### Typora + +打开Typora → 偏好设置 → 图像 + +设置: + + - 插入图片时:复制到指定路径 + + - 自定义路径:../source/images/${filename}/ + + - 勾选:优先使用相对路径 + +``` +PS E:\blog> ls .\source\images\测试\ + + + 目录: E:\blog\source\images\测试 + + +Mode LastWriteTime Length Name +---- ------------- ------ ---- +-a---- 2025/6/29 11:14 229 image-20250629111411456.png +``` + +完成! + -- cgit v1.2.3