Better XML beautify

This commit is contained in:
Gregory Schier
2025-10-24 08:59:16 -07:00
parent 754ec0ba86
commit 17ddc76223
5 changed files with 70 additions and 81 deletions

View File

@@ -1,8 +1,12 @@
import xmlFormat from 'xml-formatter';
import XmlBeautify from 'xml-beautify';
import { invokeCmd } from './tauri';
const INDENT = ' ';
const xmlBeautifier = new XmlBeautify({
INDENT,
});
export async function tryFormatJson(text: string): Promise<string> {
if (text === '') return text;
@@ -16,9 +20,8 @@ export async function tryFormatJson(text: string): Promise<string> {
try {
return JSON.stringify(JSON.parse(text), null, 2);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (err) {
// Nothing
console.log("JSON beautify failed", err);
}
return text;
@@ -28,9 +31,9 @@ export async function tryFormatXml(text: string): Promise<string> {
if (text === '') return text;
try {
return xmlFormat(text, { throwOnFailure: true, strictMode: false, indentation: INDENT });
// eslint-disable-next-line @typescript-eslint/no-unused-vars
return xmlBeautifier.beautify(text);
} catch (err) {
console.log("XML beautify failed", err);
return text;
}
}