fix-html-structure.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. const fs = require('fs');
  2. const path = require('path');
  3. // 读取HTML文件
  4. const htmlFilePath = path.join(__dirname, 'src/app/pages/designer/project-detail/project-detail.html');
  5. let htmlContent = fs.readFileSync(htmlFilePath, 'utf8');
  6. console.log('开始修复HTML结构...');
  7. // 分析并修复HTML结构
  8. function fixHtmlStructure(content) {
  9. const lines = content.split('\n');
  10. const stack = [];
  11. const fixedLines = [];
  12. let inIfBlock = false;
  13. let ifBlockDepth = 0;
  14. for (let i = 0; i < lines.length; i++) {
  15. const line = lines[i];
  16. const trimmedLine = line.trim();
  17. // 检测@if控制流
  18. if (trimmedLine.includes('@if')) {
  19. inIfBlock = true;
  20. ifBlockDepth++;
  21. fixedLines.push(line);
  22. continue;
  23. }
  24. // 检测@if控制流结束
  25. if (inIfBlock && trimmedLine === '}') {
  26. ifBlockDepth--;
  27. if (ifBlockDepth === 0) {
  28. inIfBlock = false;
  29. }
  30. fixedLines.push(line);
  31. continue;
  32. }
  33. // 检测div开始标签
  34. if (trimmedLine.includes('<div')) {
  35. const divMatch = trimmedLine.match(/<div[^>]*>/);
  36. if (divMatch && !trimmedLine.includes('</div>')) {
  37. stack.push({
  38. tag: 'div',
  39. line: i + 1,
  40. content: divMatch[0]
  41. });
  42. }
  43. fixedLines.push(line);
  44. continue;
  45. }
  46. // 检测div结束标签
  47. if (trimmedLine.includes('</div>')) {
  48. if (stack.length > 0 && stack[stack.length - 1].tag === 'div') {
  49. stack.pop();
  50. } else {
  51. console.log(`警告:第${i + 1}行发现多余的</div>标签,已移除`);
  52. continue; // 跳过多余的结束标签
  53. }
  54. }
  55. fixedLines.push(line);
  56. }
  57. // 添加缺失的结束标签
  58. while (stack.length > 0) {
  59. const unclosed = stack.pop();
  60. if (unclosed.tag === 'div') {
  61. fixedLines.push('</div>');
  62. console.log(`添加缺失的</div>标签`);
  63. }
  64. }
  65. return fixedLines.join('\n');
  66. }
  67. // 修复HTML结构
  68. const fixedContent = fixHtmlStructure(htmlContent);
  69. // 写回文件
  70. fs.writeFileSync(htmlFilePath, fixedContent, 'utf8');
  71. console.log('HTML结构修复完成!');