@@ -337,105 +337,107 @@ function fixupMargin(marginString: string) {
337
337
338
338
function modifiedStyleAttributesBuilder (
339
339
docxDocumentInstance : DocxDocument ,
340
- vNode : VNode | null ,
340
+ vNode : VTree | null ,
341
341
attributes : Attributes ,
342
342
options = { isParagraph : false } ,
343
343
) {
344
344
const modifiedAttributes = { ...attributes }
345
345
346
346
// styles
347
- if ( vNode && isVNode ( vNode ) && vNode . properties && vNode . properties . style ) {
348
- if (
349
- vNode . properties . style . color &&
350
- ! colorlessColors . includes ( vNode . properties . style . color )
351
- ) {
352
- modifiedAttributes . color = fixupColorCode ( vNode . properties . style . color )
353
- }
347
+ if ( vNode && isVNode ( vNode ) ) {
348
+ const properties = ( vNode as VNode ) . properties
349
+ if ( properties && properties . style ) {
350
+ if (
351
+ properties . style . color &&
352
+ ! colorlessColors . includes ( properties . style . color )
353
+ ) {
354
+ modifiedAttributes . color = fixupColorCode ( properties . style . color )
355
+ }
354
356
355
- if (
356
- vNode . properties . style [ "background-color" ] &&
357
- ! colorlessColors . includes ( vNode . properties . style [ "background-color" ] )
358
- ) {
359
- modifiedAttributes . backgroundColor = fixupColorCode (
360
- vNode . properties . style [ "background-color" ] ,
361
- )
362
- }
357
+ if (
358
+ properties . style [ "background-color" ] &&
359
+ ! colorlessColors . includes ( properties . style [ "background-color" ] )
360
+ ) {
361
+ modifiedAttributes . backgroundColor = fixupColorCode (
362
+ properties . style [ "background-color" ] ,
363
+ )
364
+ }
363
365
364
- if (
365
- vNode . properties . style [ "vertical-align" ] &&
366
- verticalAlignValues . includes ( vNode . properties . style [ "vertical-align" ] )
367
- ) {
368
- modifiedAttributes . verticalAlign =
369
- vNode . properties . style [ "vertical-align" ]
370
- }
366
+ if (
367
+ properties . style [ "vertical-align" ] &&
368
+ verticalAlignValues . includes ( properties . style [ "vertical-align" ] )
369
+ ) {
370
+ modifiedAttributes . verticalAlign = properties . style [ "vertical-align" ]
371
+ }
371
372
372
- if (
373
- vNode . properties . style [ "text-align" ] &&
374
- [ "left" , "right" , "center" , "justify" ] . includes (
375
- vNode . properties . style [ "text-align" ] ,
376
- )
377
- ) {
378
- modifiedAttributes . textAlign = vNode . properties . style [ "text-align" ]
379
- }
373
+ if (
374
+ properties . style [ "text-align" ] &&
375
+ [ "left" , "right" , "center" , "justify" ] . includes (
376
+ properties . style [ "text-align" ] ,
377
+ )
378
+ ) {
379
+ modifiedAttributes . textAlign = properties . style [ "text-align" ]
380
+ }
380
381
381
- // FIXME: remove bold check when other font weights are handled.
382
- if (
383
- vNode . properties . style [ "font-weight" ] &&
384
- vNode . properties . style [ "font-weight" ] === "bold"
385
- ) {
386
- modifiedAttributes . strong = vNode . properties . style [ "font-weight" ]
387
- }
388
- if ( vNode . properties . style [ "font-family" ] ) {
389
- modifiedAttributes . font = docxDocumentInstance . createFont (
390
- vNode . properties . style [ "font-family" ] ,
391
- )
392
- }
393
- if ( vNode . properties . style [ "font-size" ] ) {
394
- modifiedAttributes . fontSize = fixupFontSize (
395
- vNode . properties . style [ "font-size" ] ,
396
- )
397
- }
398
- if ( vNode . properties . style [ "line-height" ] ) {
399
- modifiedAttributes . lineHeight = fixupLineHeight (
400
- vNode . properties . style [ "line-height" ] ,
401
- vNode . properties . style [ "font-size" ]
402
- ? fixupFontSize ( vNode . properties . style [ "font-size" ] )
403
- : undefined ,
404
- )
405
- }
406
- if (
407
- vNode . properties . style [ "margin-left" ] ||
408
- vNode . properties . style [ "margin-right" ]
409
- ) {
410
- const leftMargin = fixupMargin ( vNode . properties . style [ "margin-left" ] )
411
- const rightMargin = fixupMargin ( vNode . properties . style [ "margin-right" ] )
382
+ // FIXME: remove bold check when other font weights are handled.
383
+ if (
384
+ properties . style [ "font-weight" ] &&
385
+ properties . style [ "font-weight" ] === "bold"
386
+ ) {
387
+ modifiedAttributes . strong = properties . style [ "font-weight" ]
388
+ }
389
+ if ( properties . style [ "font-family" ] ) {
390
+ modifiedAttributes . font = docxDocumentInstance . createFont (
391
+ properties . style [ "font-family" ] ,
392
+ )
393
+ }
394
+ if ( properties . style [ "font-size" ] ) {
395
+ modifiedAttributes . fontSize = fixupFontSize (
396
+ properties . style [ "font-size" ] ,
397
+ )
398
+ }
399
+ if ( properties . style [ "line-height" ] ) {
400
+ modifiedAttributes . lineHeight = fixupLineHeight (
401
+ properties . style [ "line-height" ] ,
402
+ properties . style [ "font-size" ]
403
+ ? fixupFontSize ( properties . style [ "font-size" ] )
404
+ : undefined ,
405
+ )
406
+ }
407
+ if (
408
+ properties . style [ "margin-left" ] ||
409
+ properties . style [ "margin-right" ]
410
+ ) {
411
+ const leftMargin = fixupMargin ( properties . style [ "margin-left" ] )
412
+ const rightMargin = fixupMargin ( properties . style [ "margin-right" ] )
412
413
413
- if ( leftMargin || rightMargin ) {
414
- modifiedAttributes . indentation = {
415
- left : leftMargin ,
416
- right : rightMargin ,
414
+ if ( leftMargin || rightMargin ) {
415
+ modifiedAttributes . indentation = {
416
+ left : leftMargin ,
417
+ right : rightMargin ,
418
+ }
417
419
}
418
420
}
419
- }
420
- if ( vNode . properties . style . display ) {
421
- modifiedAttributes . display = vNode . properties . style . display
422
- }
421
+ if ( properties . style . display ) {
422
+ modifiedAttributes . display = properties . style . display
423
+ }
423
424
424
- if ( vNode . properties . style . width ) {
425
- modifiedAttributes . width = vNode . properties . style . width
425
+ if ( properties . style . width ) {
426
+ modifiedAttributes . width = properties . style . width
427
+ }
426
428
}
427
429
}
428
430
429
431
// paragraph only
430
432
if ( options ?. isParagraph ) {
431
- if ( vNode && isVNode ( vNode ) && vNode . tagName === "blockquote" ) {
433
+ if ( vNode && isVNode ( vNode ) && ( vNode as VNode ) . tagName === "blockquote" ) {
432
434
modifiedAttributes . indentation = { left : 284 , right : 0 }
433
435
modifiedAttributes . textAlign = "justify"
434
436
}
435
- else if ( vNode && isVNode ( vNode ) && vNode . tagName === "code" ) {
437
+ else if ( vNode && isVNode ( vNode ) && ( vNode as VNode ) . tagName === "code" ) {
436
438
modifiedAttributes . highlightColor = "lightGray"
437
439
}
438
- else if ( vNode && isVNode ( vNode ) && vNode . tagName === "pre" ) {
440
+ else if ( vNode && isVNode ( vNode ) && ( vNode as VNode ) . tagName === "pre" ) {
439
441
modifiedAttributes . font = "Courier"
440
442
}
441
443
}
@@ -1165,7 +1167,7 @@ function computeImageDimensions(vNode: VNode, attributes: Attributes) {
1165
1167
}
1166
1168
1167
1169
async function buildParagraph (
1168
- vNode : VNode | null ,
1170
+ vNode : VTree | null ,
1169
1171
attributes : Attributes ,
1170
1172
docxDocumentInstance : DocxDocument ,
1171
1173
) {
@@ -1183,7 +1185,7 @@ async function buildParagraph(
1183
1185
modifiedAttributes ,
1184
1186
)
1185
1187
paragraphFragment . import ( paragraphPropertiesFragment )
1186
- if ( vNode && isVNode ( vNode ) && vNodeHasChildren ( vNode ) ) {
1188
+ if ( vNode && isVNode ( vNode ) && vNodeHasChildren ( vNode as VNode ) ) {
1187
1189
if (
1188
1190
[
1189
1191
"span" ,
@@ -1202,10 +1204,10 @@ async function buildParagraph(
1202
1204
"a" ,
1203
1205
"code" ,
1204
1206
"pre" ,
1205
- ] . includes ( vNode . tagName )
1207
+ ] . includes ( ( vNode as VNode ) . tagName )
1206
1208
) {
1207
1209
const runOrHyperlinkFragments = await buildRunOrHyperLink (
1208
- vNode ,
1210
+ vNode as VNode ,
1209
1211
modifiedAttributes ,
1210
1212
docxDocumentInstance ,
1211
1213
)
@@ -1224,7 +1226,7 @@ async function buildParagraph(
1224
1226
paragraphFragment . import ( runOrHyperlinkFragments )
1225
1227
}
1226
1228
}
1227
- else if ( vNode . tagName === "blockquote" ) {
1229
+ else if ( ( vNode as VNode ) . tagName === "blockquote" ) {
1228
1230
const runFragmentOrFragments = await buildRun (
1229
1231
vNode ,
1230
1232
attributes ,
@@ -1240,8 +1242,8 @@ async function buildParagraph(
1240
1242
}
1241
1243
}
1242
1244
else {
1243
- for ( let index = 0 ; index < vNode . children . length ; index ++ ) {
1244
- const childVNode = vNode . children [ index ]
1245
+ for ( let index = 0 ; index < ( vNode as VNode ) . children . length ; index ++ ) {
1246
+ const childVNode = ( vNode as VNode ) . children [ index ]
1245
1247
if ( isVNode ( childVNode ) && ( childVNode as VNode ) . tagName === "img" ) {
1246
1248
if ( isValidUrl ( ( childVNode as VNode ) . properties . src ) ) {
1247
1249
; ( childVNode as VNode ) . properties . src = await fetchImageToDataUrl (
@@ -1298,10 +1300,12 @@ async function buildParagraph(
1298
1300
// In case paragraphs has to be rendered
1299
1301
// where vText is present. Eg. table-cell
1300
1302
// Or in case the vNode is something like img
1301
- if ( vNode && isVNode ( vNode ) && vNode . tagName === "img" ) {
1302
- const imageSource = vNode . properties . src
1303
- if ( isValidUrl ( vNode . properties . src ) ) {
1304
- vNode . properties . src = await fetchImageToDataUrl ( vNode . properties . src )
1303
+ if ( vNode && isVNode ( vNode ) && ( vNode as VNode ) . tagName === "img" ) {
1304
+ const imageSource = ( vNode as VNode ) . properties . src
1305
+ if ( isValidUrl ( ( vNode as VNode ) . properties . src ) ) {
1306
+ ; ( vNode as VNode ) . properties . src = await fetchImageToDataUrl (
1307
+ ( vNode as VNode ) . properties . src ,
1308
+ )
1305
1309
}
1306
1310
const base64String = extractBase64Data ( imageSource ) ?. base64Content
1307
1311
const imageBuffer = Buffer . from (
@@ -1315,10 +1319,10 @@ async function buildParagraph(
1315
1319
modifiedAttributes . originalWidth = imageProperties . width
1316
1320
modifiedAttributes . originalHeight = imageProperties . height
1317
1321
1318
- computeImageDimensions ( vNode , modifiedAttributes )
1322
+ computeImageDimensions ( vNode as VNode , modifiedAttributes )
1319
1323
}
1320
1324
const runFragments = await buildRunOrRuns (
1321
- vNode ,
1325
+ vNode as VNode ,
1322
1326
modifiedAttributes ,
1323
1327
docxDocumentInstance ,
1324
1328
)
@@ -1724,9 +1728,9 @@ async function buildTableCell(
1724
1728
await buildList ( childVNode , docxDocumentInstance , tableCellFragment )
1725
1729
}
1726
1730
}
1727
- else if ( isVNode ( childVNode ) ) {
1731
+ else {
1728
1732
const paragraphFragment = await buildParagraph (
1729
- childVNode as VNode ,
1733
+ childVNode ,
1730
1734
modifiedAttributes ,
1731
1735
docxDocumentInstance ,
1732
1736
)
0 commit comments