Files
ALL-teach_sys/frontend_视觉设计/correctCleanIndent.cjs

112 lines
3.2 KiB
JavaScript
Raw Normal View History

#!/usr/bin/env node
const fs = require('fs');
// Create backup
const timestamp = new Date().toISOString().replace(/[:.]/g, '-').slice(0, -5);
const backupPath = `src/mocks/resumeInterviewMock.js.backup_${timestamp}_correct_clean`;
const originalContent = fs.readFileSync('src/mocks/resumeInterviewMock.js', 'utf-8');
fs.writeFileSync(backupPath, originalContent);
console.log(`✓ Backup created: ${backupPath}`);
// Function to clean indentation from text
function cleanIndentation(text) {
const lines = text.split('\n');
const cleanedLines = lines.map(line => {
// Remove all leading whitespace (spaces and tabs)
return line.replace(/^[\s\t]+/, '');
});
return cleanedLines.join('\n');
}
let totalCount = 0;
let updatedContent = originalContent;
// Use a more specific regex that handles multi-line content
const regex = /"answer":\s*"((?:[^"\\]|\\.)*)"/g;
let match;
const replacements = [];
while ((match = regex.exec(originalContent)) !== null) {
const fullMatch = match[0];
const answerContent = match[1];
// Unescape the content
let unescaped = answerContent
.replace(/\\n/g, '\n')
.replace(/\\"/g, '"')
.replace(/\\\\/g, '\\');
// Clean indentation
let cleaned = cleanIndentation(unescaped);
// Re-escape for JSON
let escaped = cleaned
.replace(/\\/g, '\\\\')
.replace(/"/g, '\\"')
.replace(/\n/g, '\\n');
const newMatch = `"answer": "${escaped}"`;
replacements.push({
original: fullMatch,
replacement: newMatch
});
totalCount++;
}
// Apply all replacements
replacements.forEach(({ original, replacement }) => {
updatedContent = updatedContent.replace(original, replacement);
});
console.log(`\n✅ Processed ${totalCount} answer fields`);
// Save the file
fs.writeFileSync('src/mocks/resumeInterviewMock.js', updatedContent);
// Verify syntax
const { execSync } = require('child_process');
try {
execSync('node -c src/mocks/resumeInterviewMock.js', { encoding: 'utf-8' });
console.log('✅ Syntax validation passed');
// Verify the changes
console.log('\n📋 Verification of cleaned answers:\n');
// Check a few samples
const sampleRegex = /"answer":\s*"([^"]*?阶段[^"]{0,400})"/;
const sampleMatch = updatedContent.match(sampleRegex);
if (sampleMatch) {
const sample = sampleMatch[1].replace(/\\n/g, '\n').substring(0, 500);
console.log('Sample answer (should have NO leading spaces):');
console.log('═'.repeat(60));
console.log(sample);
console.log('═'.repeat(60));
// Check each line for indentation
const lines = sample.split('\n');
let hasIndentation = false;
lines.forEach((line, idx) => {
if (line.match(/^[\s\t]+/)) {
hasIndentation = true;
console.log(`Line ${idx + 1} still has indentation: "${line.substring(0, 40)}..."`);
}
});
if (!hasIndentation) {
console.log('\n✅ SUCCESS: All indentation has been removed!');
} else {
console.log('\n⚠ WARNING: Some lines still have indentation');
}
}
} catch (error) {
console.error('❌ Syntax error detected, restoring backup...');
fs.writeFileSync('src/mocks/resumeInterviewMock.js', originalContent);
console.log('Backup restored');
console.error('Error:', error.message);
}