-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
120 lines (110 loc) · 3.1 KB
/
Copy pathindex.js
File metadata and controls
120 lines (110 loc) · 3.1 KB
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
const mammoth = require("mammoth");
const path = require('path')
const fs = require('fs')
const argv = process.argv
const filePath = argv.length >= 3 ? argv[2] : 'testFont.docx'
let fileName = /(?=([^.\/]*).docx?$)/.exec(filePath)
fileName = fileName && fileName[1] || 'testFont'
const outputFileName = argv.length >= 4 ? argv[3] : fileName
const asDocPage = argv.length >= 5 ? argv[4] : false
mammoth.convertToHtml({
path: path.resolve(__dirname, filePath)
}, {
transformDocument: mammoth.transforms.paragraph(transformElement),
styleMap: [
"b => span.bold",
"i => span.italic",
"u => span.underline",
"strike => span.del",
"comment-reference => sup",
"p[style-name='Center'] =>p.center:fresh",
"p[style-name='Right'] => p.right:fresh"
],
includeDefaultStyleMap: false,
ignoreEmptyParagraphs: true,
})
.then(function(result) {
let html = result.value // The generated HTML
const messages = result.messages // Any messages, such as warnings during conversion
let style = ''
if(asDocPage) {
html = html.split('<p><a id="page')
html = '<div class="page"><p><a id="page' + html[0] + html.slice(1).join('</div><div class="page"><p><a id="page') + '</div>'
style = `.page {
width: 792px;
height: 1120px;
padding: 50px 120px 0;
box-sizing: border-box;
margin: 15px auto;
box-shadow: 1px 1px 3px 1px rgba(102, 102, 102, 0.6);
-ms-user-select: none;
-moz-user-select: none;
-webkit-user-select: none;
user-select: none;
font-size: 14px;
line-height: 16px;
}`
}
fs.writeFile(path.resolve(__dirname, `./doc/${outputFileName}.html`), createHtml(style, html), (err, data) => {
if (err) {
console.log(err)
}
})
})
.done()
function createHtml(style, html) {
return `<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.bold {font-weight: bold;}
.italic {font-style: italic}
.underline {text-decoration: underline;}
.del{text-decoration: line-through;}
.center {text-align: center;}
.right { text-align: right;}
${style}
</style>
</head>
<body>
${html}
</body>
</html>`
}
function transformElement(element) {
if (element.children) {
const children = element.children.map(transformElement)
element = {
...element,
children: children
};
}
if (element.type === "paragraph") {
element = transformParagraph(element)
}
return element
}
function transformParagraph(element) {
const {
alignment = "left", styleId = ""
} = element
if (alignment === "center" && !styleId) {
return {
...element,
styleId: "center",
styleName: "Center",
}
}
if (alignment === "right" && !styleId) {
return {
...element,
styleId: "right",
styleName: "Right",
}
}
// console.log(element)
return element
}