Spaces:
Sleeping
Sleeping
File size: 1,443 Bytes
4bea261 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | import fs from 'fs';
import path from 'path';
function formatXml(xml) {
let formatted = '';
let pad = 0;
xml = xml.replace(/(>)(<)(\/*)/g, '$1\r\n$2$3');
xml.split('\r\n').forEach(node => {
let indent = 0;
if (node.match(/.+<\/\w[^>]*>$/)) {
indent = 0;
} else if (node.match(/^<\/\w/)) {
if (pad !== 0) { pad -= 1; }
} else if (node.match(/^<\w[^>]*[^\/]>.*$/)) {
indent = 1;
} else {
indent = 0;
}
formatted += ' '.repeat(pad) + node + '\r\n';
pad += indent;
});
return formatted.trim();
}
const filesToFormat = [
'dist/client/sitemap-index.xml',
'dist/client/sitemap-0.xml',
'dist/client/sitemap-1.xml',
'dist/client/sitemap-2.xml',
'dist/client/sitemap-3.xml',
'dist/client/sitemap-4.xml',
'dist/client/sitemap-5.xml',
'.vercel/output/static/sitemap-index.xml',
'.vercel/output/static/sitemap-0.xml',
'.vercel/output/static/sitemap-1.xml',
'.vercel/output/static/sitemap-2.xml',
'.vercel/output/static/sitemap-3.xml',
'.vercel/output/static/sitemap-4.xml',
'.vercel/output/static/sitemap-5.xml'
];
filesToFormat.forEach(filePath => {
const fullPath = path.resolve(process.cwd(), filePath);
if (fs.existsSync(fullPath)) {
const xmlContent = fs.readFileSync(fullPath, 'utf8');
const prettyXml = formatXml(xmlContent);
fs.writeFileSync(fullPath, prettyXml, 'utf8');
console.log(`Formatted XML: ${filePath}`);
}
});
|