惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

NISL@THU
NISL@THU
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
D
Darknet – Hacking Tools, Hacker News & Cyber Security
阮一峰的网络日志
阮一峰的网络日志
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
WordPress大学
WordPress大学
IT之家
IT之家
Cyberwarzone
Cyberwarzone
博客园_首页
博客园 - 聂微东
V
Visual Studio Blog
Cisco Talos Blog
Cisco Talos Blog
V
Vulnerabilities – Threatpost
Google DeepMind News
Google DeepMind News
Schneier on Security
Schneier on Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
The Hacker News
The Hacker News
雷峰网
雷峰网
Last Week in AI
Last Week in AI
Spread Privacy
Spread Privacy
L
Lohrmann on Cybersecurity
O
OpenAI News
人人都是产品经理
人人都是产品经理
AWS News Blog
AWS News Blog
小众软件
小众软件
T
Tailwind CSS Blog
The Cloudflare Blog
L
LINUX DO - 最新话题
有赞技术团队
有赞技术团队
Know Your Adversary
Know Your Adversary
The GitHub Blog
The GitHub Blog
L
LINUX DO - 热门话题
Y
Y Combinator Blog
Stack Overflow Blog
Stack Overflow Blog
B
Blog
MyScale Blog
MyScale Blog
S
SegmentFault 最新的问题
S
Schneier on Security
The Last Watchdog
The Last Watchdog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
大猫的无限游戏
大猫的无限游戏
罗磊的独立博客
Blog — PlanetScale
Blog — PlanetScale
博客园 - Franky
I
InfoQ
P
Proofpoint News Feed
量子位
S
Security @ Cisco Blogs

Hunsh's Blog

生成个人的 Github PR List 如何选择合适的加密手段 Nginx SNI Proxy 如何优雅导入 k8s.io/kubernetes Make Go mod and Git to use specify .netrc 多架构镜像的体积优化 使用 buildkit 进行多架构构建并提取产物 如何修改镜像 layer(以 sourcemap-less grafana 为例) acme.sh 自动化 Google CA 申请证书 微信小程序嵌入任意公众号文章 k8s && bazel 项目从 go1.20 升级 go1.21 go serve http and https on same port typecho 迁移到 hexo golang mitm 遇到的问题 流式数据专用的 mine-type Golang 正向代理对于 Host 的处理 (RFC 7230) 分享 MacOS 的一些系统fix脚本(dns、sleep) prometheus rate/increase/delta/sum等函数不符合预期或出现小数的原理 [油猴脚本] USTB Everywhere 为校外访问添加访问任意网站的能力
实现 hexo 文章和资源在同一目录下
2023-08-06 · via Hunsh's Blog

前言

最近起了写博客的欲望,但是感觉 typecho 用起来不是很喜欢,于是通过先找主题再确定博客系统的原则选择了 hexo。

但是发现 hexo 的这个文件结构属实是比较丑陋。

一般有两种结构

  1. 图片和图片在单独的目录下,文章通过相对/绝对路径引用
  2. 开启 post_asset_folder,得到这样的结构
    1
    2
    3
    4
    5
    _posts
    ├── article1/
    ├── article2/
    ├── article1.md
    ├── article2.md

即使是第二种结构,也不够优雅,因为 title 出现太多次了,文章一多找依赖也麻烦。搜索了老半天居然没发现有人吐槽这个,我差点想换 hugo。

实现

看了下 hexo 的源码,找到了结构二的实现。

1
2
3
4
5
6
7
8
9
10
// hexo/lib/plugins/processor/post.js
function scanAssetDir(ctx, post) {
if (!ctx.config.post_asset_folder) return;

const assetDir = post.asset_dir;
const baseDir = ctx.base_dir;
const baseDirLength = baseDir.length;
const PostAsset = ctx.model('PostAsset');
// ...
}

如果开启了 post_asset_folder,就会把 md 同名的资源文件夹里识别为资源文件夹。

那么 asset_dir 是如何定义的呢?

1
2
3
4
5
6
// hexo/lib/models/post.js
// 声明了一个 getter
Post.virtual('asset_dir').get(function() {
const src = this.full_source;
return src.substring(0, src.length - extname(src).length) + sep;
});

所以思路很简单,只要把 asset_dir 改成 md 所在就可以了。

这里就用到了 hexo 的插件机制,把这个 getter patch 了一下,改成了 md 所在的目录。

1
2
3
4
5
6
7
8
'use strict';

const { dirname, sep } = require('path');

hexo.database.model('Post').schema.virtual('asset_dir').get(function() {
const src = this.full_source;
return dirname(src) + sep;
});

使用方式

我把这个插件发布到了 npm 上,可以直接安装使用。

1
2
3
4
// npm
npm install hexo-asset-dir --save
// pnpm
pnpm i hexo-asset-dir

然后在 _config.yml 里面设置 post_asset_folder: truenew_post_name: :title/index.md

这样的目录结构就会是

1
2
3
4
5
6
7
_posts
├── example
├── image.jpg
└── index.md
├── example2
├── image.jpg
└── index.md

效果

image

还有

不过我用的这个 fluid 主题的缩略图功能不支持这样的相对路径,还需要再一次插件 hack。

1
2
3
4
5
6
7
const relative = /^[^/](?!.*:\/\/)/

hexo.extend.filter.register('before_post_render', function(data){
if (data.index_img && relative.test(data.index_img))
data.index_img = data.path + data.index_img;
return data;
});

把以上脚本命名为 hexo-fluid-index-img.js 放置到 scripts 目录下即可。