-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: generate img cdn files
- Loading branch information
Showing
2 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,5 @@ artifacts | |
.history | ||
**node_modules** | ||
**cache** | ||
**artifacts** | ||
**artifacts** | ||
**readme_mirror.md** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); |