replace-picsum-links.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. const fs = require('fs');
  2. const path = require('path');
  3. // 定义要替换的目录
  4. const srcDir = path.join(__dirname, 'src');
  5. // 定义要搜索的文件扩展名
  6. const fileExtensions = ['.ts', '.html'];
  7. // 替换 picsum.photos 链接的函数
  8. function replacePicsumLinks(fileContent, filePath) {
  9. let modifiedContent = fileContent;
  10. let replacementCount = 0;
  11. // 正则表达式匹配 picsum.photos 链接或不正确的 placeholder.com 链接
  12. const picsumRegex = /(?:https:\/\/(?:fastly\.)?picsum\.photos\/id\/(\d+)(?:\/([^"'\s]+))?)|(?:https:\/\/via\.placeholder\.com\/(\d+)x(\d+)[^"']*?\?text=IMG)/g;
  13. // 为不同类型的图片生成不同的占位图颜色(使用十六进制格式)
  14. const colors = {
  15. avatar: ['FFCCCC', 'CCFFCC', 'CCCCFF', 'FFFFCC'],
  16. image: ['F0F0F0', 'E6E6E6', 'DCDCDC'],
  17. cover: ['C8C8C8', 'BABABA', 'B4B4B4']
  18. };
  19. // 替换匹配的链接
  20. modifiedContent = modifiedContent.replace(picsumRegex, (match, id, sizePart, placeholderWidth, placeholderHeight) => {
  21. // 解析图片尺寸
  22. let width = 400;
  23. let height = 300;
  24. // 如果是 placeholder 链接,直接使用提取的尺寸
  25. if (placeholderWidth && placeholderHeight) {
  26. width = parseInt(placeholderWidth);
  27. height = parseInt(placeholderHeight);
  28. } else if (sizePart) {
  29. // 从 picsum 链接的尺寸部分提取尺寸信息
  30. const sizeParts = sizePart.split('/').filter(Boolean);
  31. if (sizeParts.length === 2) {
  32. width = parseInt(sizeParts[0]);
  33. height = parseInt(sizeParts[1]);
  34. } else if (sizeParts.length === 1) {
  35. // 如果只有一个尺寸值,使用它作为宽高
  36. width = parseInt(sizeParts[0]);
  37. height = parseInt(sizeParts[0]);
  38. }
  39. }
  40. // 根据文件路径和图片ID决定使用哪种颜色
  41. let colorType = 'image';
  42. if (filePath.includes('avatar') || match.includes('/40/40') || match.includes('/48/48')) {
  43. colorType = 'avatar';
  44. } else if (match.includes('/600/400') || match.includes('/800/600')) {
  45. colorType = 'cover';
  46. }
  47. // 选择一个颜色(基于ID的哈希值或随机值)
  48. const colorIndex = id ? parseInt(id) % colors[colorType].length : Math.floor(Math.random() * colors[colorType].length);
  49. const color = colors[colorType][colorIndex];
  50. replacementCount++;
  51. // 返回正确格式的 placeholder.com 链接
  52. return `https://via.placeholder.com/${width}x${height}/${color}/555555?text=IMG`;
  53. });
  54. return { modifiedContent, replacementCount };
  55. }
  56. // 遍历目录中的文件
  57. function traverseDirectory(dir) {
  58. const files = fs.readdirSync(dir);
  59. let totalReplacements = 0;
  60. files.forEach(file => {
  61. const filePath = path.join(dir, file);
  62. const stat = fs.statSync(filePath);
  63. if (stat.isDirectory()) {
  64. const subDirReplacements = traverseDirectory(filePath);
  65. totalReplacements += subDirReplacements;
  66. } else if (fileExtensions.some(ext => file.endsWith(ext))) {
  67. // 读取文件内容
  68. const fileContent = fs.readFileSync(filePath, 'utf8');
  69. // 替换 picsum.photos 链接
  70. const { modifiedContent, replacementCount } = replacePicsumLinks(fileContent, filePath);
  71. // 如果有替换,写入文件
  72. if (replacementCount > 0) {
  73. fs.writeFileSync(filePath, modifiedContent, 'utf8');
  74. console.log(`已替换 ${filePath} 中的 ${replacementCount} 个图片链接`);
  75. totalReplacements += replacementCount;
  76. }
  77. }
  78. });
  79. return totalReplacements;
  80. }
  81. // 执行替换操作
  82. console.log('开始替换 picsum.photos 链接...');
  83. const totalReplacements = traverseDirectory(srcDir);
  84. console.log(`替换完成!总共替换了 ${totalReplacements} 个图片链接。`);