fix-placeholder-images.js 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. // 修复HTML文件中的placeholder链接
  8. function fixHtmlPlaceholders(fileContent) {
  9. let modifiedContent = fileContent;
  10. let replacementCount = 0;
  11. // 正则表达式匹配img标签中的via.placeholder.com链接
  12. const imgPlaceholderRegex = /<img\s+src="https:\/\/via\.placeholder\.com\/(\d+)x(\d+)\/([0-9A-Fa-f]+)\/([0-9A-Fa-f]+)\?text=([^"']*)"\s+alt="([^"]*)"\s+class="([^"]*)"\s*\/?>/g;
  13. // 替换匹配的img标签为div元素
  14. modifiedContent = modifiedContent.replace(imgPlaceholderRegex, (match, width, height, bgColor, textColor, text, alt, className) => {
  15. replacementCount++;
  16. // 返回一个带有内联样式的div元素,保留原有的类名
  17. return `<div style="width: ${width}px; height: ${height}px; background-color: #${bgColor}; color: #${textColor}; display: flex; align-items: center; justify-content: center; font-size: ${Math.min(parseInt(width), parseInt(height)) / 3}px; font-weight: bold;" class="${className}" title="${alt}">${text}</div>`;
  18. });
  19. return { modifiedContent, replacementCount };
  20. }
  21. // 修复TypeScript文件中的placeholder链接
  22. function fixTsPlaceholders(fileContent) {
  23. let modifiedContent = fileContent;
  24. let replacementCount = 0;
  25. // 正则表达式匹配TypeScript中的via.placeholder.com链接
  26. const tsPlaceholderRegex = /'https:\/\/via\.placeholder\.com\/(\d+)x(\d+)\/([0-9A-Fa-f]+)\/([0-9A-Fa-f]+)\?text=([^']*)'/g;
  27. // 替换匹配的链接为一个简单的base64编码的data URL
  28. modifiedContent = modifiedContent.replace(tsPlaceholderRegex, (match, width, height, bgColor, textColor, text) => {
  29. replacementCount++;
  30. // 生成一个简单的SVG data URL,避免对外部服务的依赖
  31. // 这是一个简化的SVG,包含背景色和文本
  32. const svg = `data:image/svg+xml,%3Csvg width='${width}' height='${height}' xmlns='http://www.w3.org/2000/svg'%3E%3Crect width='100%25' height='100%25' fill='%23${bgColor}'/%3E%3Ctext x='50%25' y='50%25' font-family='Arial' font-size='${Math.min(parseInt(width), parseInt(height)) / 3}' font-weight='bold' text-anchor='middle' fill='%23${textColor}' dy='0.3em'%3E${encodeURIComponent(text)}%3C/text%3E%3C/svg%3E`;
  33. return `'${svg}'`;
  34. });
  35. return { modifiedContent, replacementCount };
  36. }
  37. // 遍历目录中的文件
  38. function traverseDirectory(dir) {
  39. const files = fs.readdirSync(dir);
  40. let totalReplacements = 0;
  41. files.forEach(file => {
  42. const filePath = path.join(dir, file);
  43. const stat = fs.statSync(filePath);
  44. if (stat.isDirectory()) {
  45. const subDirReplacements = traverseDirectory(filePath);
  46. totalReplacements += subDirReplacements;
  47. } else if (file.endsWith('.html')) {
  48. // 读取文件内容
  49. const fileContent = fs.readFileSync(filePath, 'utf8');
  50. // 修复HTML中的placeholder链接
  51. const { modifiedContent, replacementCount } = fixHtmlPlaceholders(fileContent);
  52. // 如果有替换,写入文件
  53. if (replacementCount > 0) {
  54. fs.writeFileSync(filePath, modifiedContent, 'utf8');
  55. console.log(`已修复 ${filePath} 中的 ${replacementCount} 个HTML placeholder链接`);
  56. totalReplacements += replacementCount;
  57. }
  58. } else if (file.endsWith('.ts')) {
  59. // 读取文件内容
  60. const fileContent = fs.readFileSync(filePath, 'utf8');
  61. // 修复TypeScript中的placeholder链接
  62. const { modifiedContent, replacementCount } = fixTsPlaceholders(fileContent);
  63. // 如果有替换,写入文件
  64. if (replacementCount > 0) {
  65. fs.writeFileSync(filePath, modifiedContent, 'utf8');
  66. console.log(`已修复 ${filePath} 中的 ${replacementCount} 个TypeScript placeholder链接`);
  67. totalReplacements += replacementCount;
  68. }
  69. }
  70. });
  71. return totalReplacements;
  72. }
  73. // 执行修复操作
  74. console.log('开始修复placeholder链接...');
  75. const totalReplacements = traverseDirectory(srcDir);
  76. console.log(`修复完成!总共修复了 ${totalReplacements} 个placeholder链接。`);