Spaces:
Sleeping
Sleeping
| 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}`); | |
| } | |
| }); | |