feat: add xml preview

This commit is contained in:
Nybkox
2025-02-11 16:51:39 +01:00
parent 7b68dd0f47
commit aa8dfa1c87
7 changed files with 296 additions and 0 deletions

View File

@@ -0,0 +1,24 @@
export default function formatXml(xml: string): string {
if (typeof xml !== 'string') return '';
xml = xml.replace(/>\s*</g, '><');
let formatted = '';
let indent = 0;
const tags = xml.split(/(<.*?>)/g).filter(s => s.trim());
for (let tag of tags) {
if (tag.startsWith('</')) {
indent--;
formatted += '\n' + ' '.repeat(indent) + tag;
} else if (tag.startsWith('<') && !tag.endsWith('/>') && !tag.startsWith('<?')) {
formatted += '\n' + ' '.repeat(indent) + tag;
indent++;
} else {
formatted += '\n' + ' '.repeat(indent) + tag;
}
}
return formatted.trim();
}