feat: add image reference validation to lint system

Add new image reference validation rule in manual_lint.js:
- Validates that referenced images exist in the file system
- Supports relative paths (./, ../) and skips external URLs
- Reports missing images as lint errors

Fixes found issue:
- dishes/soup/排骨山药玉米汤/排骨山药玉米汤.md: removed missing image
  reference './排骨山药玉米汤.jpg'

All lint checks now pass 

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Anduin Xue
2026-05-05 09:09:16 +00:00
parent 31e0396f9f
commit 6f83b5e49d
2 changed files with 44 additions and 2 deletions

View File

@@ -2,6 +2,7 @@ const util = require("util");
const glob = util.promisify(require('glob'));
const fs = require("fs").promises;
const path = require('path');
const fsSyncAccess = require("fs");
const MAX_FILE_SIZE = 1024 * 1024; // 1MB
// glob 模式,定位菜谱 Markdown 文件和所有文件
@@ -143,6 +144,49 @@ const validators = [
if (!lines.includes(footer)) {
errors.push(`文件 ${filePath} 不符合仓库的规范! 它没有包含必需的附加内容!,需要在最后一行添加模板中的【${footer}`);
}
},
// 检查图片引用是否存在
async (filePath, lines, errors) => {
const fileDir = path.dirname(filePath);
const content = lines.join('\n');
// 匹配 ![alt](path) 和 [text](path) 的图片引用
// 支持相对路径和 URL
const imageRegex = /\[([^\]]*)\]\(([^)]+\.(?:jpg|jpeg|png|gif|webp|svg))\)/gi;
let match;
const imageRefs = new Set();
while ((match = imageRegex.exec(content)) !== null) {
imageRefs.add(match[2]);
}
// 检查每个引用的图片是否存在
for (const imagePath of imageRefs) {
// 跳过 URLhttp/https/ftp
if (imagePath.startsWith('http://') || imagePath.startsWith('https://') || imagePath.startsWith('ftp://')) {
continue;
}
// 解析相对路径
let fullImagePath;
if (imagePath.startsWith('/')) {
// 绝对路径相对于repo根目录
fullImagePath = path.resolve(__dirname, '../../', imagePath);
} else if (imagePath.includes('..')) {
// 相对路径(包含 ..
fullImagePath = path.resolve(fileDir, imagePath);
} else {
// 相对路径(同目录或子目录)
fullImagePath = path.resolve(fileDir, imagePath);
}
try {
fsSyncAccess.accessSync(fullImagePath);
} catch (err) {
errors.push(`文件 ${filePath} 引用了不存在的图片: ${imagePath}`);
}
}
}
];

View File

@@ -2,8 +2,6 @@
排骨山药玉米汤是一道兼具清甜与滋补的经典家常汤品。山药软糯、玉米清甜,搭配排骨的醇厚,具有健脾益胃、增强免疫力的功效。
![排骨山药玉米汤成品](./排骨山药玉米汤.jpg)
预估烹饪难度:★★
## 必备原料和工具