|
|
@@ -2,21 +2,13 @@
|
|
|
import pako from 'pako';
|
|
|
import xmldoc from 'xmldoc';
|
|
|
|
|
|
-export const extractCodeFromMxfile = (input: string): string => {
|
|
|
- const doc = new xmldoc.XmlDocument(input);
|
|
|
- return doc.valueWithPath('diagram');
|
|
|
+const validateUncompressedData = (input: string): boolean => {
|
|
|
+ return new RegExp('/<mxGraphModel/').test(input);
|
|
|
};
|
|
|
|
|
|
-const validateInputData = (input: string): boolean => {
|
|
|
+const validateCompressedData = (input: string): boolean => {
|
|
|
let data = input;
|
|
|
|
|
|
- try {
|
|
|
- data = extractCodeFromMxfile(data);
|
|
|
- }
|
|
|
- catch (e) {
|
|
|
- // ignore
|
|
|
- }
|
|
|
-
|
|
|
try {
|
|
|
data = Buffer.from(data, 'base64').toString('binary');
|
|
|
}
|
|
|
@@ -65,27 +57,20 @@ export const generateMxgraphData = (code: string): string => {
|
|
|
return '';
|
|
|
}
|
|
|
|
|
|
- validateInputData(trimedCode);
|
|
|
-
|
|
|
- let xml;
|
|
|
- try {
|
|
|
- // may be XML Format <mxfile><diagram> ... </diagram></mxfile>
|
|
|
- const doc = new xmldoc.XmlDocument(trimedCode);
|
|
|
- const diagram = doc.valueWithPath('diagram');
|
|
|
- if (diagram) {
|
|
|
- xml = trimedCode;
|
|
|
- }
|
|
|
- }
|
|
|
- catch (e) {
|
|
|
- // may be NOT XML Format
|
|
|
- xml = `
|
|
|
-<mxfile version="6.8.9" editor="www.draw.io" type="atlas">
|
|
|
- <mxAtlasLibraries/>
|
|
|
- <diagram>${trimedCode}</diagram>
|
|
|
-</mxfile>
|
|
|
-`;
|
|
|
+ // Evaluate the code is whether uncompressed data that are generated by v21.1.0 or above
|
|
|
+ // see: https://github.com/jgraph/drawio/issues/3106#issuecomment-1479352026
|
|
|
+ const isUncompressedData = validateUncompressedData(trimedCode);
|
|
|
+ if (isUncompressedData) {
|
|
|
+ validateCompressedData(trimedCode);
|
|
|
}
|
|
|
|
|
|
+ const xml = `
|
|
|
+ <mxfile version="6.8.9" editor="www.draw.io" type="atlas">
|
|
|
+ <mxAtlasLibraries/>
|
|
|
+ <diagram>${isUncompressedData ? xmldoc.XmlDocument(trimedCode) : trimedCode}</diagram>
|
|
|
+ </mxfile>
|
|
|
+ `;
|
|
|
+
|
|
|
// see options: https://drawio.freshdesk.com/support/solutions/articles/16000042542-embed-html
|
|
|
const mxGraphData = {
|
|
|
editable: false,
|