-
Notifications
You must be signed in to change notification settings - Fork 0
/
word2html.js
45 lines (42 loc) · 1.24 KB
/
word2html.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
// @ts-check
const fs = require('fs-extra');
const path = require('path');
const { command } = require('execa');
const klaw = require('klaw');
const handleHtml = require('./handleHtml');
const word2html = async (/** @type {string} */ dirPath, /** @type {string} */ outputDirPath, /** @type {string} */ exePath, /** @type {boolean} */ removeOrigin) => {
return new Promise(async (resolve) => {
const htmlDir = path.resolve(dirPath, 'html');
await fs.ensureDir(htmlDir);
await command(
exePath +
` -f ${dirPath} -O ${htmlDir} -T wdFormatFilteredHTML -E 65001 -OX .html${
removeOrigin ? ' -R true' : ''
}`,
{
stdio: 'inherit',
}
);
for await (const html of klaw(htmlDir, {
depthLimit: -1,
filter: (filePath) => {
if (['.html'].includes(path.extname(filePath))) {
return true;
}
if (fs.statSync(filePath).isDirectory()) {
return true;
}
return false;
},
})) {
if (html.stats.isFile()) {
await fs.writeFile(
path.resolve(outputDirPath, path.basename(html.path)),
handleHtml(await fs.readFile(html.path))
);
}
}
resolve();
});
};
module.exports = word2html;