mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-23 11:36:01 +00:00
25 lines
628 B
TypeScript
25 lines
628 B
TypeScript
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();
|
|
}
|