-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
143 lines (124 loc) · 4.26 KB
/
index.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
const mammoth = require("mammoth")
const argv = require('yargs/yargs')(process.argv.slice(2))
.usage('Usage: $0 [file] [output]')
.default('file', 'input.docx')
.default('output', 'pipe') // Output can be pipe or json
.argv;
const puppeteer = require('puppeteer')
const filesystem = require('fs')
const fs = require('fs/promises')
var mammothOptions = {
styleMap: [
"b => b",
"i => i",
"p[style-name='Text Body'] => p:fresh",
"p[style-name='Body Text'] => p:fresh",
"p[style-name='No Spacing'] => p:fresh",
"p[style-name='Emphasis'] => p:fresh",
"p[style-name='StrongEmphasis'] => p:fresh",
"p[style-name='Strong Emphasis'] => p:fresh",
"p[style-name='Caption'] => p:fresh",
"p[style-name='Markedcontent'] => p:fresh",
"p[style-name='Rynqvb'] => p:fresh",
]
};
mammoth.convertToHtml({path: argv.file}, mammothOptions)
.then(function(result){
let htmlFromFile = result.value
let messages = result.messages
// Add additional paragraph to the end of the file
htmlFromFile += '<p>_</p>'
// Get filename from argv and remove extension
const filename = argv.file.split('.')[0]
// Slugify the filename and add .json extension
const slugifiedFilename = filename.replace(/\s+/g, '-').toLowerCase() + '.html'
filesystem.writeFile(`/usr/src/app/${slugifiedFilename}`, htmlFromFile, function (err) {
if (err) throw err;
});
callPuppeteer(htmlFromFile)
if (messages.length > 0) {
throw new Error(JSON.stringify(messages))
}
})
.catch(function(error) {
console.error(error);
});
function callPuppeteer(htmlFromFile) {
(async () => {
const browser = await puppeteer.launch({
defaultViewport: {
width: 1920,
height: 1080,
},
bindAddress: '0.0.0.0',
args: [
'--no-sandbox',
'--headless',
'--disable-gpu',
'--disable-dev-shm-usage',
'--remote-debugging-port=9222',
'--remote-debugging-address=0.0.0.0',
],
});
const page = await browser.newPage();
const htmlContent = `<body>
<div id="editorjs"></div>
<div id="outout"></div>
</body>`
await page.setContent(htmlContent);
await page.addScriptTag({path: './src/editorjs/editor.min.js'});
await page.addScriptTag({path: './src/editorjs/paragraph.min.js'});
await page.addScriptTag({path: './src/editorjs/list.min.js'});
await page.addScriptTag({path: './src/editorjs/link.min.js'});
await page.addScriptTag({path: './src/editorjs/header.min.js'});
await page.addScriptTag({path: './src/editorjs/raw.min.js'});
const scriptContent = `
const editor = new EditorJS({
holder: 'editorjs',
tools: {
paragraph: {
class: Paragraph,
inlineToolbar: true,
},
list: {
class: List,
inlineToolbar: true,
config: {
defaultStyle: 'unordered'
}
},
header: Header,
linkTool: {
class: LinkTool,
},
raw: RawTool,
},
onReady: () => {
editor.blocks.renderFromHTML('${htmlFromFile}')
},
onChange: () => {
editor.save().then((outputData) => {
document.getElementById('outout').innerHTML = JSON.stringify(outputData)
})
},
})
`
await page.addScriptTag({content: scriptContent});
// await page.waitForSelector('#complete');
await page.waitForTimeout(2000);
const output = await page.evaluate(() => {
return document.getElementById('outout').innerHTML
})
await browser.close();
if (argv.output === 'pipe') {
console.log(output)
return
}
// Get filename from argv and remove extension
const filename = argv.file.split('.')[0]
// Slugify the filename and add .json extension
const slugifiedFilename = filename.replace(/\s+/g, '-').toLowerCase() + '.json'
// Write the output to a file
await fs.writeFile(`/usr/src/app/${slugifiedFilename}`, output)
})();
}