mirror of
https://github.com/Anduin2017/HowToCook.git
synced 2026-05-18 21:31:26 +08:00
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:
44
.github/manual_lint.js
vendored
44
.github/manual_lint.js
vendored
@@ -2,6 +2,7 @@ const util = require("util");
|
|||||||
const glob = util.promisify(require('glob'));
|
const glob = util.promisify(require('glob'));
|
||||||
const fs = require("fs").promises;
|
const fs = require("fs").promises;
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
const fsSyncAccess = require("fs");
|
||||||
|
|
||||||
const MAX_FILE_SIZE = 1024 * 1024; // 1MB
|
const MAX_FILE_SIZE = 1024 * 1024; // 1MB
|
||||||
// glob 模式,定位菜谱 Markdown 文件和所有文件
|
// glob 模式,定位菜谱 Markdown 文件和所有文件
|
||||||
@@ -143,6 +144,49 @@ const validators = [
|
|||||||
if (!lines.includes(footer)) {
|
if (!lines.includes(footer)) {
|
||||||
errors.push(`文件 ${filePath} 不符合仓库的规范! 它没有包含必需的附加内容!,需要在最后一行添加模板中的【${footer}】`);
|
errors.push(`文件 ${filePath} 不符合仓库的规范! 它没有包含必需的附加内容!,需要在最后一行添加模板中的【${footer}】`);
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
// 检查图片引用是否存在
|
||||||
|
async (filePath, lines, errors) => {
|
||||||
|
const fileDir = path.dirname(filePath);
|
||||||
|
const content = lines.join('\n');
|
||||||
|
|
||||||
|
// 匹配  和 [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) {
|
||||||
|
// 跳过 URL(http/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}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|||||||
@@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
排骨山药玉米汤是一道兼具清甜与滋补的经典家常汤品。山药软糯、玉米清甜,搭配排骨的醇厚,具有健脾益胃、增强免疫力的功效。
|
排骨山药玉米汤是一道兼具清甜与滋补的经典家常汤品。山药软糯、玉米清甜,搭配排骨的醇厚,具有健脾益胃、增强免疫力的功效。
|
||||||
|
|
||||||

|
|
||||||
|
|
||||||
预估烹饪难度:★★
|
预估烹饪难度:★★
|
||||||
|
|
||||||
## 必备原料和工具
|
## 必备原料和工具
|
||||||
|
|||||||
Reference in New Issue
Block a user