From 6f83b5e49de4494c6bcb8851e950160de73dbdc9 Mon Sep 17 00:00:00 2001 From: Anduin Xue Date: Tue, 5 May 2026 09:09:16 +0000 Subject: [PATCH] feat: add image reference validation to lint system MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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> --- .github/manual_lint.js | 44 ++++++++++++++++++++ dishes/soup/排骨山药玉米汤/排骨山药玉米汤.md | 2 - 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/.github/manual_lint.js b/.github/manual_lint.js index af0a963c..973e1c49 100644 --- a/.github/manual_lint.js +++ b/.github/manual_lint.js @@ -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) { + // 跳过 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}`); + } + } } ]; diff --git a/dishes/soup/排骨山药玉米汤/排骨山药玉米汤.md b/dishes/soup/排骨山药玉米汤/排骨山药玉米汤.md index 942db7f3..639de098 100644 --- a/dishes/soup/排骨山药玉米汤/排骨山药玉米汤.md +++ b/dishes/soup/排骨山药玉米汤/排骨山药玉米汤.md @@ -2,8 +2,6 @@ 排骨山药玉米汤是一道兼具清甜与滋补的经典家常汤品。山药软糯、玉米清甜,搭配排骨的醇厚,具有健脾益胃、增强免疫力的功效。 -![排骨山药玉米汤成品](./排骨山药玉米汤.jpg) - 预估烹饪难度:★★ ## 必备原料和工具