-
Notifications
You must be signed in to change notification settings - Fork 8
/
export.js
197 lines (179 loc) · 5.93 KB
/
export.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
const _ = require('lodash');
const puppeteer = require('puppeteer');
const axios = require('axios');
const fs = require('fs');
const path = require('path');
const shelljs = require('shelljs');
const PDFMerger = require('pdf-merger-js');
const readFile = (file) => new Promise((resolve, reject) => {
fs.readFile(file, 'utf-8', (err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
});
});
const cachePath = (() => {
if (process.env.XDG_CACHE_HOME)
return path.join(process.env.XDG_CACHE_HOME, 'draw.io-export');
if (process.env.HOME)
return path.join(process.env.HOME, '.cache', 'draw.io-export');
return path.join(__dirname, '.cache');
})();
const cacheExists = (t) => new Promise((resolve) => {
fs.exists(path.join(cachePath, t), resolve);
});
const cacheDict = {
'https://app.diagrams.net/export3.html': 'export3.html',
'https://app.diagrams.net/js/app.min.js': 'app.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_HTMLorMML': 'MathJax.js',
'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/config/TeX-MML-AM_HTMLorMML.js?V=2.7.5': 'TeX-MML-AM_HTMLorMML.js',
'https://cdn.mathjax.org/mathjax/contrib/a11y/accessibility-menu.js?V=2.7.5': 'accessibility-menu.js',
};
const cache = async (f, t) => {
if (await cacheExists(t)) {
return;
}
const response = await axios.get(f, {
responseType: 'stream',
});
shelljs.mkdir('-p', path.join(cachePath, path.dirname(t)));
response.data.pipe(fs.createWriteStream(path.join(cachePath, t)));
return new Promise((resolve, reject) => {
response.data.on('end', () => {
resolve();
});
response.data.on('error', () => {
reject();
});
});
};
module.exports = async ({ file, format, path: p }) => {
await Promise.all(_.toPairs(cacheDict).map(([f, t]) => cache(f, t)));
const fullXml = await readFile(file);
const browser = await puppeteer.launch({
executablePath: process.env.CHROMIUM_PATH,
headless: true,
args: ['--no-sandbox']
});
try {
const page = await browser.newPage();
await page.setRequestInterception(true);
page.on('request', (interceptedRequest) => {
const t = cacheDict[interceptedRequest.url()];
if (t) {
fs.readFile(path.join(cachePath, t), (err, res) => {
if (err) {
interceptedRequest.abort();
} else {
interceptedRequest.respond({
status: 200,
body: res,
});
}
});
} else {
interceptedRequest.continue();
}
});
await page.goto('https://www.draw.io/export3.html', { waitUntil: 'networkidle0' });
await page.evaluate((obj) => doc = mxUtils.parseXml(obj), fullXml);
const pages = +await page.evaluate(() => doc.documentElement.getAttribute('pages') || 1);
const gen = async (fmt, path) => {
await page.evaluate((obj) => {
const dup = doc.documentElement.cloneNode(false);
while (true) {
const n = doc.documentElement.firstChild;
dup.appendChild(n);
if (n.nodeType === Node.ELEMENT_NODE)
break;
}
obj.xml = dup.outerHTML;
render(obj);
}, {
format: 'png',
w: 0,
h: 0,
border: 0,
bg: 'none',
scale: 1,
});
await page.waitForSelector('#LoadingComplete');
const boundsJson = await page.mainFrame().$eval('#LoadingComplete', (div) => div.getAttribute('bounds'));
const bounds = JSON.parse(boundsJson);
const fixingScale = 1; // 0.959;
const w = Math.ceil(bounds.width * fixingScale);
const h = Math.ceil(bounds.height * fixingScale);
switch (fmt) {
case 'png':
await page.setViewport({ width: w, height: h });
await page.screenshot({
omitBackground: true,
type: 'png',
fullPage: true,
path,
});
break;
case 'pdf': {
await page.setViewport({ width: w, height: h });
await page.pdf({
printBackground: false,
width: `${w}px`,
height: `${h + 1}px`, // the extra pixel to prevent adding an extra empty page
margin: { top: '0px', bottom: '0px', left: '0px', right: '0px' },
path,
});
break;
}
default:
throw new Error(`Format ${fmt} not allowed, valid options are: png, pdf`);
}
};
const m = format.match(/^(?<prefix>.*-)?(?<core>png|pdf)$/);
const { prefix, core } = m.groups;
switch (prefix) {
case undefined:
await gen(core, p);
break;
case 'cat-':
if (core != 'pdf')
throw new Error('Format not allowed');
if (pages === 1) {
await gen(core, p);
} else {
const merger = new PDFMerger();
for (let i = 0; i < pages; i++) {
const fn = p + '__' + i + '.' + core;
await gen(core, fn);
merger.add(fn);
}
await merger.save(p);
for (let i = 0; i < pages; i++)
shelljs.rm(p + '__' + i + '.' + core);
}
break;
case 'split-':
case 'split-index-':
for (let i = 0; i < pages; i++)
await gen(core, p + i + core);
break;
case 'split-id-':
for (let i = 0; i < pages; i++) {
const id = await page.evaluate(() => doc.documentElement.firstChild.getAttribute('id'));
await gen(core, p + id + '.' + core);
}
break;
case 'split-name-':
for (let i = 0; i < pages; i++) {
const name = await page.evaluate(() => doc.documentElement.firstChild.getAttribute('name'));
await gen(core, p + name + '.' + core);
}
break;
default:
throw new Error(`Format prefix ${prefix} not allowed, valid options are: cat-, split-, split-index-, split-id-, split-name-`);
}
} finally {
await browser.close();
}
};