mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-23 23:05:59 +00:00
feat: add xml preview
This commit is contained in:
24
packages/web/src/celldata/formatXml.ts
Normal file
24
packages/web/src/celldata/formatXml.ts
Normal 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();
|
||||
}
|
||||
Reference in New Issue
Block a user