From 0d55ac6144ec45670b2e56234deb310572a41b55 Mon Sep 17 00:00:00 2001 From: hudson Date: Tue, 26 Nov 2024 02:27:59 +0000 Subject: [PATCH] =?UTF-8?q?=E6=96=B9=E4=BE=BF=20mirror=20=E4=B9=8B?= =?UTF-8?q?=E7=B1=BB=E7=9A=84=E5=B9=B3=E5=8F=B0=E5=88=86=E5=8F=91=EF=BC=8C?= =?UTF-8?q?=E4=B8=BB=E8=A6=81=E6=98=AF=E5=9B=BE=E7=89=87=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=20(#840)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: generate img cdn files --- .gitignore | 3 ++- mirror_gen.js | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 mirror_gen.js diff --git a/.gitignore b/.gitignore index bbebaa22e..47a261f59 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,5 @@ artifacts .history **node_modules** **cache** -**artifacts** \ No newline at end of file +**artifacts** +**readme_mirror.md** \ No newline at end of file diff --git a/mirror_gen.js b/mirror_gen.js new file mode 100644 index 000000000..0676c1149 --- /dev/null +++ b/mirror_gen.js @@ -0,0 +1,68 @@ +const fs = require('fs'); +const path = require('path'); + +const CDN_PREFIX = 'https://cdn.jsdelivr.net/gh/AmazingAng/WTF-Solidity'; + +// 递归查找所有的 readme.md 文件 +function findReadmeFiles(dir) { + let results = []; + const files = fs.readdirSync(dir); + + for (const file of files) { + const filePath = path.join(dir, file); + const stat = fs.statSync(filePath); + + if (stat.isDirectory() && !file.startsWith('.')) { + results = results.concat(findReadmeFiles(filePath)); + } else if (file.toLowerCase() === 'readme.md') { + results.push(filePath); + } + } + + return results; +} + +// 处理图片链接 +function processImageLinks(content, dirName) { + const imageRegex = /!\[(.*?)\]\((\.\/[^)]+)\)/g; + return content.replace(imageRegex, (match, altText, imagePath) => { + const cleanImagePath = imagePath.replace(/^\.\//, ''); + const cdnUrl = `${CDN_PREFIX}/${dirName}/${cleanImagePath}`; + return `![${altText}](${cdnUrl})`; + }); +} + +// 删除 frontmatter +function removeFrontmatter(content) { + // 匹配开头的 --- 到结束的 --- 之间的所有内容 + return content.replace(/^---\n[\s\S]*?\n---\n/, ''); +} + +function main() { + try { + const readmeFiles = findReadmeFiles('.'); + console.log(`找到 ${readmeFiles.length} 个 readme.md 文件`); + + for (const readmePath of readmeFiles) { + let content = fs.readFileSync(readmePath, 'utf-8'); + const dirName = path.dirname(readmePath).replace(/^\.[\\/]/, ''); + + // 先删除 frontmatter + content = removeFrontmatter(content); + // 再处理图片链接 + const processedContent = processImageLinks(content, dirName); + + // 只有当内容有变化时才创建新文件 + if (content !== processedContent) { + const mirrorPath = path.join(path.dirname(readmePath), 'readme_mirror.md'); + fs.writeFileSync(mirrorPath, processedContent, 'utf-8'); + console.log(`已生成镜像文件: ${mirrorPath}`); + } + } + console.log('处理完成!'); + } catch (error) { + console.error('处理过程中出错:', error); + } +} + +main();