* feat(new tool): xml formatter * feat(xml-formatter): added happy path e2e tests * refactor(xml-formatter): improved unit tests * refactor(xml-formatter): add better suitable icon * feat(xml-formatter): added happy path e2e tests * feat(xml-formatter): registered xml as syntax highlighter * chore(auto-import): removed unused NSpace --------- Co-authored-by: Corentin Thomasset <corentin.thomasset74@gmail.com>
29 lines
641 B
TypeScript
29 lines
641 B
TypeScript
import xmlFormat, { type XMLFormatterOptions } from 'xml-formatter';
|
|
import { withDefaultOnError } from '@/utils/defaults';
|
|
|
|
export { formatXml, isValidXML };
|
|
|
|
function cleanRawXml(rawXml: string): string {
|
|
return rawXml.trim();
|
|
}
|
|
|
|
function formatXml(rawXml: string, options?: XMLFormatterOptions): string {
|
|
return withDefaultOnError(() => xmlFormat(cleanRawXml(rawXml), options) ?? '', '');
|
|
}
|
|
|
|
function isValidXML(rawXml: string): boolean {
|
|
const cleanedRawXml = cleanRawXml(rawXml);
|
|
|
|
if (cleanedRawXml === '') {
|
|
return true;
|
|
}
|
|
|
|
try {
|
|
xmlFormat(cleanedRawXml);
|
|
return true;
|
|
}
|
|
catch (e) {
|
|
return false;
|
|
}
|
|
}
|