mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-19 11:56:00 +00:00
feat: add xml-formatter
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
<script>
|
||||
import hljs from 'highlight.js/lib/core';
|
||||
import xmlGrammar from './xmlGrammar';
|
||||
import formatXml from './formatXml';
|
||||
import xmlFormat from 'xml-formatter';
|
||||
import { afterUpdate, onMount } from 'svelte';
|
||||
|
||||
import 'highlight.js/styles/vs.css';
|
||||
|
||||
export let code = '';
|
||||
|
||||
$: formattedCode = formatXml(code);
|
||||
$: formattedCode = xmlFormat(code);
|
||||
|
||||
onMount(() => {
|
||||
hljs.registerLanguage('xml', xmlGrammar);
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
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