fix(scripts): 修复图片路径处理中的括号和空格问题

- 修改正则表达式以支持URL中包含括号的情况
- 改进图片重命名逻辑,处理多种路径格式
- 移除路径中的%20空格编码
This commit is contained in:
二叉树树
2026-01-12 23:31:53 +08:00
parent 07c604c97b
commit 3d63d3e592
3 changed files with 17 additions and 5 deletions

View File

@@ -63,7 +63,8 @@ async function processMarkdownFile(filePath) {
}
// 2. 处理 Markdown 图片语法 ![alt](url)
const markdownImageRegex = /!\[.*?\]\((.*?)\)/g;
// 修复:支持 URL 中包含一层括号,例如 image(1).png
const markdownImageRegex = /!\[.*?\]\(((?:[^()]+|\([^()]*\))+)\)/g;
while ((match = markdownImageRegex.exec(content)) !== null) {
const fullUrl = match[1];
// 去除可能的 title 部分
@@ -169,7 +170,18 @@ async function handleImageRename(markdownPath, imagePath) {
return null;
} else {
// 相对路径
absolutePath = path.resolve(markdownDir, decodedPath);
const candidates = [decodedPath];
if (decodedPath !== imagePath) {
candidates.push(imagePath);
}
for (const candidate of candidates) {
const candidateAbs = path.resolve(markdownDir, candidate);
if (fs.existsSync(candidateAbs)) {
absolutePath = candidateAbs;
break;
}
}
}
if (!fs.existsSync(absolutePath)) {
@@ -248,8 +260,8 @@ async function handleImageRename(markdownPath, imagePath) {
} else {
newReferencePath = imagePath.substring(0, lastSeparatorIndex + 1) + newFilename;
}
return newReferencePath;
return newReferencePath.replace(/%20/g, "");
}
/**

View File

@@ -68,7 +68,7 @@ Umami用于在网站插入一个JS来进行访客统计以及展示访客信
### 博客本体
![](../assets/images/https___blog.acofork.com__多地区多线路HTTP测速%20(1).png)
![](../assets/images/https___blogacoforkcom__多地区多线路HTTP测速(1).png)
### Umami
![](../assets/images/https___umamiacoforkcom__多地区多线路HTTP测速.png)
### 随机图