-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
128 lines (115 loc) · 3.25 KB
/
index.ts
File metadata and controls
128 lines (115 loc) · 3.25 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
121
122
123
124
125
126
127
128
import JSZip from "jszip"
import addFilesToContainer from "./src/html-to-docx.ts"
import { type DocumentOptions } from "./src/types.ts"
function minifyHTMLString(htmlString: string) {
// First, protect content inside <pre> tags from minification
const preContentMap = new Map<string, string>()
let preIndex = 0
// Find all <pre> tag content and replace with placeholders
const protectedHTMLString = htmlString.replace(
/<pre(\s[^>]*)?>([^]*?)<\/pre>/gi,
(match, attributes, content) => {
const placeholder = `__PRE_PLACEHOLDER_${preIndex++}__`
preContentMap.set(placeholder, content)
return `<pre${attributes || ""}>${placeholder}</pre>`
},
)
let minifiedHTMLString = protectedHTMLString
.replace(/\n/g, " ")
.replace(/\r/g, " ")
.replace(/\r\n/g, " ")
.replace(/[\t]+</g, "<")
.replace(/>[\t ]+$/g, ">")
// Remove HTML comments
minifiedHTMLString = minifiedHTMLString.replace(/<!--.*?-->/g, "")
// Use placeholder to protect spaces between closing and opening tags
minifiedHTMLString = minifiedHTMLString.replace(/>\s+</g, ">__SPACE__<")
// Preserve spaces only between inline elements by restoring specific patterns
const inlineElements = [
"a",
"abbr",
"acronym",
"b",
"bdo",
"big",
"br",
"button",
"cite",
"code",
"dfn",
"em",
"i",
"img",
"input",
"kbd",
"label",
"map",
"object",
"q",
"samp",
"script",
"select",
"small",
"span",
"strong",
"sub",
"sup",
"textarea",
"tt",
"var",
"u",
"ins",
"del",
"s",
"strike",
"mark",
]
// Combined pattern: preserve spaces between inline elements
// (with or without attributes)
const inlinePattern = new RegExp(
`</(${inlineElements.join("|")})>__SPACE__<(${
inlineElements.join("|")
})(\\s[^>]*)?>`,
"gi",
)
minifiedHTMLString = minifiedHTMLString.replace(inlinePattern, "</$1> <$2$3>")
// Remove remaining placeholder spaces
minifiedHTMLString = minifiedHTMLString.replace(/__SPACE__/g, "")
// Restore original pre content
preContentMap.forEach((originalContent, placeholder) => {
minifiedHTMLString = minifiedHTMLString.replace(
placeholder,
originalContent,
)
})
return minifiedHTMLString
}
export default async function generateContainer(
htmlString: string | null,
headerHTMLString: string | null,
documentOptions: DocumentOptions,
footerHTMLString: string | null,
): Promise<Buffer | Blob> {
const zip = new JSZip()
await addFilesToContainer(
zip,
htmlString ? minifyHTMLString(htmlString) : "",
documentOptions,
headerHTMLString ? minifyHTMLString(headerHTMLString) : "",
footerHTMLString ? minifyHTMLString(footerHTMLString) : "",
)
const buffer = await zip.generateAsync({ type: "arraybuffer" })
if (Object.prototype.hasOwnProperty.call(global, "Buffer")) {
return Buffer.from(new Uint8Array(buffer))
}
if (Object.prototype.hasOwnProperty.call(global, "Blob")) {
return new Blob([buffer], {
type: "application/" +
"vnd.openxmlformats-officedocument.wordprocessingml.document",
})
}
throw new Error(
"Add blob support using a polyfill. " +
"E.g. https://github.com/bjornstar/blob-polyfill",
)
}