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

82 lines
2.5 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}_remove_indent`;
const mockContent = fs.readFileSync('src/mocks/resumeInterviewMock.js', 'utf-8');
fs.writeFileSync(backupPath, mockContent);
console.log(`✓ Backup created: ${backupPath}`);
// Function to completely remove answer indentation
function removeAllIndentation(answer) {
// Split by newlines
const lines = answer.split('\\n');
const cleanedLines = [];
for (let line of lines) {
// Remove ALL leading spaces from each line
// This will make everything left-aligned
let cleanedLine = line.trimStart();
cleanedLines.push(cleanedLine);
}
return cleanedLines.join('\\n');
}
// Process the file
let updatedContent = mockContent;
let count = 0;
// More comprehensive pattern to match answer fields
// This handles multi-line answers with escaped characters
const answerRegex = /"answer":\s*"((?:[^"\\]|\\.)*)"/g;
updatedContent = updatedContent.replace(answerRegex, (match, answerContent) => {
// Unescape the answer content for processing
let unescaped = answerContent
.replace(/\\n/g, '\n')
.replace(/\\"/g, '"')
.replace(/\\\\/g, '\\');
// Remove all indentation
let cleaned = removeAllIndentation(unescaped);
// Re-escape for JSON
let escaped = cleaned
.replace(/\\/g, '\\\\')
.replace(/"/g, '\\"')
.replace(/\n/g, '\\n');
count++;
return `"answer": "${escaped}"`;
});
console.log(`\n✅ Removed indentation from ${count} 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');
// Show a few samples of the cleaned content
console.log('\n📋 Sample of cleaned answers:');
const matches = updatedContent.matchAll(/"answer":\s*"([^"]{0,300})/g);
let samples = 0;
for (const match of matches) {
if (samples >= 2) break;
console.log(`\nSample ${samples + 1}:`);
console.log(match[1].replace(/\\n/g, '\n').substring(0, 250) + '...');
samples++;
}
} catch (error) {
console.error('❌ Syntax error detected, restoring backup...');
fs.writeFileSync('src/mocks/resumeInterviewMock.js', mockContent);
console.log('Backup restored');
console.error('Error:', error.message);
}