Skip to content

Commit 2cefdeb

Browse files
committed
fix: update typing for various methods
1 parent eb33b92 commit 2cefdeb

2 files changed

Lines changed: 27 additions & 8 deletions

File tree

src/helpers/render-document-file.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ const convertHTML = HTMLToVDOM({
2828

2929
// eslint-disable-next-line consistent-return, no-shadow
3030
export const buildImage = async (docxDocumentInstance, vNode: VNodeType, maximumWidth = null) => {
31-
let response = null;
32-
let base64Uri = null;
31+
let response: { id: number; fileContent: string; fileNameWithExtension: string } | null = null;
32+
let base64Uri: string | null = null;
3333
try {
3434
const imageSource = vNode.properties.src;
3535
if (isValidUrl(imageSource)) {

src/html-to-docx.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ const convertHTML = HTMLToVDOM({
4343

4444
const mergeOptions = <Type>(options: Type, patch: Type): Type => ({ ...options, ...patch });
4545

46-
const fixupFontSize = (fontSize: string | number) => {
46+
const fixupFontSize = (fontSize?: string | number) => {
4747
let normalizedFontSize;
4848
if (typeof fontSize === 'string' && pointRegex.test(fontSize)) {
4949
const matchedParts = fontSize.match(pointRegex) as RegExpMatchArray;
5050

51+
// @ts-ignore
5152
normalizedFontSize = pointToHIP(matchedParts[1]);
5253
} else if (fontSize) {
5354
// assuming it is already in HIP
@@ -59,23 +60,33 @@ const fixupFontSize = (fontSize: string | number) => {
5960
return normalizedFontSize;
6061
};
6162

62-
const normalizeUnits = (dimensioningObject: Object, defaultDimensionsProperty) => {
63-
let normalizedUnitResult: {} | null = {};
63+
const normalizeUnits = (
64+
dimensioningObject: DocumentOptions['pageSize'] | DocumentOptions['margins'],
65+
defaultDimensionsProperty: DocumentOptions['pageSize'] | DocumentOptions['margins']
66+
) => {
67+
let normalizedUnitResult: Partial<
68+
DocumentOptions['pageSize'] | DocumentOptions['margins']
69+
> | null = {};
6470
if (typeof dimensioningObject === 'object' && dimensioningObject !== null) {
6571
Object.keys(dimensioningObject).forEach((key) => {
6672
if (pixelRegex.test(dimensioningObject[key])) {
67-
const matchedParts = dimensioningObject[key].match(pixelRegex);
73+
const matchedParts = dimensioningObject[key].match(pixelRegex) as RegExpMatchArray;
74+
// @ts-ignore
6875
normalizedUnitResult[key] = pixelToTWIP(matchedParts[1]);
6976
} else if (cmRegex.test(dimensioningObject[key])) {
70-
const matchedParts = dimensioningObject[key].match(cmRegex);
77+
const matchedParts = dimensioningObject[key].match(cmRegex) as RegExpMatchArray;
78+
// @ts-ignore
7179
normalizedUnitResult[key] = cmToTWIP(matchedParts[1]);
7280
} else if (inchRegex.test(dimensioningObject[key])) {
73-
const matchedParts = dimensioningObject[key].match(inchRegex);
81+
const matchedParts = dimensioningObject[key].match(inchRegex) as RegExpMatchArray;
82+
// @ts-ignore
7483
normalizedUnitResult[key] = inchToTWIP(matchedParts[1]);
7584
} else if (dimensioningObject[key]) {
85+
// @ts-ignore
7686
normalizedUnitResult[key] = dimensioningObject[key];
7787
} else {
7888
// incase value is something like 0
89+
// @ts-ignore
7990
normalizedUnitResult[key] = defaultDimensionsProperty[key];
8091
}
8192
});
@@ -96,6 +107,7 @@ const normalizeDocumentOptions = (
96107
switch (key) {
97108
case 'pageSize':
98109
case 'margins':
110+
// @ts-ignore
99111
normalizedDocumentOptions[key] = normalizeUnits(
100112
documentOptions[key],
101113
defaultDocumentOptions[key]
@@ -142,6 +154,7 @@ async function addFilesToContainer(
142154
// Conversion to Word XML happens here
143155
docxDocument.documentXML = await renderDocumentFile(docxDocument);
144156

157+
// @ts-ignore
145158
zip
146159
.folder(relsFolderName)
147160
.file(
@@ -150,6 +163,7 @@ async function addFilesToContainer(
150163
{ createFolders: false }
151164
);
152165

166+
// @ts-ignore
153167
zip.folder('docProps').file('core.xml', docxDocument.generateCoreXML(), {
154168
createFolders: false,
155169
});
@@ -169,6 +183,7 @@ async function addFilesToContainer(
169183
internalRelationship
170184
);
171185

186+
// @ts-ignore
172187
zip.folder(wordFolder).file(fileNameWithExt, headerXML.toString({ prettyPrint: true }), {
173188
createFolders: false,
174189
});
@@ -190,6 +205,7 @@ async function addFilesToContainer(
190205
internalRelationship
191206
);
192207

208+
// @ts-ignore
193209
zip.folder(wordFolder).file(fileNameWithExt, footerXML.toString({ prettyPrint: true }), {
194210
createFolders: false,
195211
});
@@ -203,13 +219,15 @@ async function addFilesToContainer(
203219
`${themeFolder}/${themeFileNameWithExt}`,
204220
internalRelationship
205221
);
222+
// @ts-ignore
206223
zip
207224
.folder(wordFolder)
208225
.folder(themeFolder)
209226
.file(themeFileNameWithExt, docxDocument.generateThemeXML(), {
210227
createFolders: false,
211228
});
212229

230+
// @ts-ignore
213231
zip
214232
.folder(wordFolder)
215233
.file('document.xml', docxDocument.generateDocumentXML(), { createFolders: false })
@@ -222,6 +240,7 @@ async function addFilesToContainer(
222240
const relationshipXMLs = docxDocument.generateRelsXML();
223241
if (relationshipXMLs && Array.isArray(relationshipXMLs)) {
224242
relationshipXMLs.forEach(({ fileName, xmlString }) => {
243+
// @ts-ignore
225244
zip.folder(wordFolder).folder(relsFolderName).file(`${fileName}.xml.rels`, xmlString, {
226245
createFolders: false,
227246
});

0 commit comments

Comments
 (0)