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

85 lines
2.6 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}_clean_indent`;
const mockContent = fs.readFileSync('src/mocks/resumeInterviewMock.js', 'utf-8');
fs.writeFileSync(backupPath, mockContent);
console.log(`✓ Backup created: ${backupPath}`);
// Function to clean answer indentation
function cleanAnswerIndentation(answer) {
// Split by newlines
const lines = answer.split('\\n');
const cleanedLines = [];
for (const line of lines) {
// Remove leading spaces (4 spaces or more) while keeping list markers
let cleanedLine = line;
// If line starts with spaces followed by a list marker (-, *, numbers)
if (/^\s{4,}[-\*\d]/.test(line)) {
// Remove the leading spaces but keep the list marker
cleanedLine = line.replace(/^\s{4,}/, '');
} else if (/^\s{4,}/.test(line)) {
// For other lines with 4+ spaces, remove them
cleanedLine = line.replace(/^\s{4,}/, '');
}
cleanedLines.push(cleanedLine);
}
return cleanedLines.join('\\n');
}
// Process the file
let updatedContent = mockContent;
let count = 0;
// Find all answer fields and clean them
const answerPattern = /"answer":\s*"([^"\\]*(\\.[^"\\]*)*)"/g;
updatedContent = updatedContent.replace(answerPattern, (match, answerContent) => {
// Unescape the answer content for processing
let unescaped = answerContent
.replace(/\\n/g, '\n')
.replace(/\\"/g, '"')
.replace(/\\\\/g, '\\');
// Clean indentation
let cleaned = cleanAnswerIndentation(unescaped);
// Re-escape for JSON
let escaped = cleaned
.replace(/\\/g, '\\\\')
.replace(/"/g, '\\"')
.replace(/\n/g, '\\n');
count++;
return `"answer": "${escaped}"`;
});
console.log(`\n✅ Cleaned indentation in ${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 sample of the cleaned content
console.log('\n📋 Sample of cleaned answer:');
const sampleMatch = updatedContent.match(/"answer":\s*"([^"]{0,200})/);
if (sampleMatch) {
console.log(sampleMatch[1].replace(/\\n/g, '\n').substring(0, 200));
}
} catch (error) {
console.error('❌ Syntax error detected, restoring backup...');
fs.writeFileSync('src/mocks/resumeInterviewMock.js', mockContent);
console.log('Backup restored');
}