fix: XML body disappearing with invalid XML (#3567)
fix: catch xmlformatter errors
This commit is contained in:
@@ -44,6 +44,8 @@ import { invokeAction } from "~/helpers/actions"
|
|||||||
import { useDebounceFn } from "@vueuse/core"
|
import { useDebounceFn } from "@vueuse/core"
|
||||||
// TODO: Migrate from legacy mode
|
// TODO: Migrate from legacy mode
|
||||||
|
|
||||||
|
import * as E from "fp-ts/Either"
|
||||||
|
|
||||||
type ExtendedEditorConfig = {
|
type ExtendedEditorConfig = {
|
||||||
mode: string
|
mode: string
|
||||||
placeholder: string
|
placeholder: string
|
||||||
@@ -160,6 +162,21 @@ const getLanguage = (langMime: string): Language | null => {
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const formatXML = (doc: string) => {
|
||||||
|
try {
|
||||||
|
const formatted = xmlFormat(doc, {
|
||||||
|
indentation: " ",
|
||||||
|
collapseContent: true,
|
||||||
|
lineSeparator: "\n",
|
||||||
|
whiteSpaceAtEndOfSelfclosingTag: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
return E.right(formatted)
|
||||||
|
} catch (e) {
|
||||||
|
return E.left(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Uses xml-formatter to format the XML document
|
* Uses xml-formatter to format the XML document
|
||||||
* @param doc Document to parse
|
* @param doc Document to parse
|
||||||
@@ -171,14 +188,11 @@ const parseDoc = (
|
|||||||
langMime: string
|
langMime: string
|
||||||
): string | undefined => {
|
): string | undefined => {
|
||||||
if (langMime === "application/xml" && doc) {
|
if (langMime === "application/xml" && doc) {
|
||||||
return xmlFormat(doc, {
|
const xmlFormatingResult = formatXML(doc)
|
||||||
indentation: " ",
|
if (E.isRight(xmlFormatingResult)) return xmlFormatingResult.right
|
||||||
collapseContent: true,
|
|
||||||
lineSeparator: "\n",
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
return doc
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return doc
|
||||||
}
|
}
|
||||||
|
|
||||||
const getEditorLanguage = (
|
const getEditorLanguage = (
|
||||||
|
|||||||
Reference in New Issue
Block a user