Custom JSON formatter that works with template syntax (#128)

This commit is contained in:
Gregory Schier
2024-10-21 22:17:14 +00:00
committed by GitHub
parent aa7f18a16f
commit e216214085
17 changed files with 414 additions and 117 deletions

View File

@@ -0,0 +1,28 @@
import { useQuery } from '@tanstack/react-query';
import type { EditorProps } from '../components/core/Editor';
import { tryFormatJson, tryFormatXml } from '../lib/formatters';
export function useFormatText({
text,
language,
pretty,
}: {
text: string;
language: EditorProps['language'];
pretty: boolean;
}) {
return useQuery({
queryKey: [text],
queryFn: async () => {
if (text === '' || !pretty) {
return text;
} else if (language === 'json') {
return tryFormatJson(text);
} else if (language === 'xml' || language === 'html') {
return tryFormatXml(text);
} else {
return text;
}
},
});
}