docxmlater 2.2.4 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core/Document.d.ts +38 -5
- package/dist/core/Document.d.ts.map +1 -1
- package/dist/core/Document.js +512 -295
- package/dist/core/Document.js.map +1 -1
- package/dist/formatting/NumberingLevel.d.ts +9 -5
- package/dist/formatting/NumberingLevel.d.ts.map +1 -1
- package/dist/formatting/NumberingLevel.js +156 -110
- package/dist/formatting/NumberingLevel.js.map +1 -1
- package/package.json +1 -1
package/dist/core/Document.js
CHANGED
|
@@ -773,7 +773,7 @@ class Document {
|
|
|
773
773
|
}
|
|
774
774
|
applyStandardTableFormatting(colorOrOptions, multiCellColor) {
|
|
775
775
|
let options;
|
|
776
|
-
if (typeof colorOrOptions ===
|
|
776
|
+
if (typeof colorOrOptions === "string") {
|
|
777
777
|
if (multiCellColor) {
|
|
778
778
|
options = {
|
|
779
779
|
singleCellShading: colorOrOptions,
|
|
@@ -788,13 +788,13 @@ class Document {
|
|
|
788
788
|
options = colorOrOptions;
|
|
789
789
|
}
|
|
790
790
|
const singleCellShading = options?.singleCellShading?.toUpperCase();
|
|
791
|
-
const headerRowShading = (options?.headerRowShading ||
|
|
791
|
+
const headerRowShading = (options?.headerRowShading || "E9E9E9").toUpperCase();
|
|
792
792
|
const headerRowFormatting = {
|
|
793
793
|
bold: options?.headerRowFormatting?.bold !== false,
|
|
794
|
-
alignment: options?.headerRowFormatting?.alignment ||
|
|
795
|
-
font: options?.headerRowFormatting?.font ||
|
|
794
|
+
alignment: options?.headerRowFormatting?.alignment || "center",
|
|
795
|
+
font: options?.headerRowFormatting?.font || "Verdana",
|
|
796
796
|
size: options?.headerRowFormatting?.size || 12,
|
|
797
|
-
color: options?.headerRowFormatting?.color ||
|
|
797
|
+
color: options?.headerRowFormatting?.color || "000000",
|
|
798
798
|
spacingBefore: options?.headerRowFormatting?.spacingBefore ?? 60,
|
|
799
799
|
spacingAfter: options?.headerRowFormatting?.spacingAfter ?? 60,
|
|
800
800
|
};
|
|
@@ -814,12 +814,12 @@ class Document {
|
|
|
814
814
|
const rowCount = table.getRowCount();
|
|
815
815
|
const columnCount = table.getColumnCount();
|
|
816
816
|
table.setAllBorders({
|
|
817
|
-
style:
|
|
817
|
+
style: "single",
|
|
818
818
|
size: 4,
|
|
819
|
-
color:
|
|
819
|
+
color: "000000",
|
|
820
820
|
});
|
|
821
|
-
table.setLayout(
|
|
822
|
-
table.setWidthType(
|
|
821
|
+
table.setLayout("auto");
|
|
822
|
+
table.setWidthType("pct");
|
|
823
823
|
table.setWidth(5000);
|
|
824
824
|
const is1x1Table = rowCount === 1 && columnCount === 1;
|
|
825
825
|
if (is1x1Table) {
|
|
@@ -840,7 +840,8 @@ class Document {
|
|
|
840
840
|
cell.setMargins(cellMargins);
|
|
841
841
|
for (const para of cell.getParagraphs()) {
|
|
842
842
|
const numPr = para.getFormatting().numbering;
|
|
843
|
-
if (numPr &&
|
|
843
|
+
if (numPr &&
|
|
844
|
+
(numPr.level !== undefined || numPr.numId !== undefined)) {
|
|
844
845
|
continue;
|
|
845
846
|
}
|
|
846
847
|
para.setAlignment(headerRowFormatting.alignment);
|
|
@@ -864,22 +865,23 @@ class Document {
|
|
|
864
865
|
cell.setMargins(cellMargins);
|
|
865
866
|
const currentShading = cell.getFormatting().shading;
|
|
866
867
|
const currentColor = currentShading?.fill?.toUpperCase();
|
|
867
|
-
const isValidHexColor = /^[0-9A-F]{6}$/i.test(currentColor ||
|
|
868
|
-
if (currentColor && currentColor !==
|
|
868
|
+
const isValidHexColor = /^[0-9A-F]{6}$/i.test(currentColor || "");
|
|
869
|
+
if (currentColor && currentColor !== "FFFFFF" && isValidHexColor) {
|
|
869
870
|
cell.setShading({ fill: headerRowShading });
|
|
870
871
|
cellsRecolored++;
|
|
871
872
|
for (const para of cell.getParagraphs()) {
|
|
872
873
|
const numPr = para.getFormatting().numbering;
|
|
873
|
-
if (numPr &&
|
|
874
|
+
if (numPr &&
|
|
875
|
+
(numPr.level !== undefined || numPr.numId !== undefined)) {
|
|
874
876
|
continue;
|
|
875
877
|
}
|
|
876
|
-
para.setAlignment(
|
|
878
|
+
para.setAlignment("center");
|
|
877
879
|
para.setSpaceBefore(60);
|
|
878
880
|
para.setSpaceAfter(60);
|
|
879
881
|
for (const run of para.getRuns()) {
|
|
880
882
|
run.setBold(true);
|
|
881
|
-
run.setFont(
|
|
882
|
-
run.setColor(
|
|
883
|
+
run.setFont("Verdana", 12);
|
|
884
|
+
run.setColor("000000");
|
|
883
885
|
}
|
|
884
886
|
}
|
|
885
887
|
}
|
|
@@ -1085,15 +1087,15 @@ class Document {
|
|
|
1085
1087
|
if (!abstractNum)
|
|
1086
1088
|
continue;
|
|
1087
1089
|
const level0 = abstractNum.getLevel(0);
|
|
1088
|
-
if (!level0 || level0.getFormat() !==
|
|
1090
|
+
if (!level0 || level0.getFormat() !== "bullet")
|
|
1089
1091
|
continue;
|
|
1090
1092
|
for (let levelIndex = 0; levelIndex < 9; levelIndex++) {
|
|
1091
1093
|
const numLevel = abstractNum.getLevel(levelIndex);
|
|
1092
1094
|
if (!numLevel)
|
|
1093
1095
|
continue;
|
|
1094
|
-
const bullet = levelIndex % 2 === 0 ?
|
|
1096
|
+
const bullet = levelIndex % 2 === 0 ? "•" : "○";
|
|
1095
1097
|
numLevel.setText(bullet);
|
|
1096
|
-
numLevel.setFont(
|
|
1098
|
+
numLevel.setFont("Arial");
|
|
1097
1099
|
numLevel.setFontSize(24);
|
|
1098
1100
|
numLevel.setLeftIndent(720 * (levelIndex + 1));
|
|
1099
1101
|
numLevel.setHangingIndent(360);
|
|
@@ -1112,17 +1114,17 @@ class Document {
|
|
|
1112
1114
|
if (!abstractNum)
|
|
1113
1115
|
continue;
|
|
1114
1116
|
const level0 = abstractNum.getLevel(0);
|
|
1115
|
-
if (!level0 || level0.getFormat() ===
|
|
1117
|
+
if (!level0 || level0.getFormat() === "bullet")
|
|
1116
1118
|
continue;
|
|
1117
1119
|
for (let levelIndex = 0; levelIndex < 9; levelIndex++) {
|
|
1118
1120
|
const numLevel = abstractNum.getLevel(levelIndex);
|
|
1119
1121
|
if (!numLevel)
|
|
1120
1122
|
continue;
|
|
1121
|
-
numLevel.setFont(
|
|
1123
|
+
numLevel.setFont("Verdana");
|
|
1122
1124
|
numLevel.setFontSize(24);
|
|
1123
1125
|
numLevel.setLeftIndent(720 * (levelIndex + 1));
|
|
1124
1126
|
numLevel.setHangingIndent(360);
|
|
1125
|
-
numLevel.setAlignment(
|
|
1127
|
+
numLevel.setAlignment("left");
|
|
1126
1128
|
}
|
|
1127
1129
|
this.applyFormattingToListParagraphs(instance.getNumId());
|
|
1128
1130
|
count++;
|
|
@@ -1136,7 +1138,7 @@ class Document {
|
|
|
1136
1138
|
if (numbering?.numId === numId) {
|
|
1137
1139
|
const runs = para.getRuns();
|
|
1138
1140
|
for (const run of runs) {
|
|
1139
|
-
run.setFont(
|
|
1141
|
+
run.setFont("Verdana", 12);
|
|
1140
1142
|
}
|
|
1141
1143
|
para.setSpaceBefore(0);
|
|
1142
1144
|
para.setSpaceAfter(60);
|
|
@@ -1164,30 +1166,32 @@ class Document {
|
|
|
1164
1166
|
wrapParagraphInTable(para, options) {
|
|
1165
1167
|
const paraIndex = this.bodyElements.indexOf(para);
|
|
1166
1168
|
if (paraIndex === -1) {
|
|
1167
|
-
throw new Error(
|
|
1169
|
+
throw new Error("Paragraph not found in document body elements");
|
|
1168
1170
|
}
|
|
1169
1171
|
const table = new Table_1.Table(1, 1);
|
|
1170
1172
|
const cell = table.getCell(0, 0);
|
|
1171
1173
|
if (!cell) {
|
|
1172
|
-
throw new Error(
|
|
1174
|
+
throw new Error("Failed to get cell from newly created table");
|
|
1173
1175
|
}
|
|
1174
1176
|
this.bodyElements.splice(paraIndex, 1);
|
|
1175
1177
|
cell.addParagraph(para);
|
|
1176
1178
|
if (options.shading) {
|
|
1177
1179
|
cell.setShading({ fill: options.shading });
|
|
1178
1180
|
}
|
|
1179
|
-
if (options.marginTop !== undefined ||
|
|
1180
|
-
options.
|
|
1181
|
+
if (options.marginTop !== undefined ||
|
|
1182
|
+
options.marginBottom !== undefined ||
|
|
1183
|
+
options.marginLeft !== undefined ||
|
|
1184
|
+
options.marginRight !== undefined) {
|
|
1181
1185
|
cell.setMargins({
|
|
1182
1186
|
top: options.marginTop ?? 100,
|
|
1183
1187
|
bottom: options.marginBottom ?? 100,
|
|
1184
1188
|
left: options.marginLeft ?? 100,
|
|
1185
|
-
right: options.marginRight ?? 100
|
|
1189
|
+
right: options.marginRight ?? 100,
|
|
1186
1190
|
});
|
|
1187
1191
|
}
|
|
1188
1192
|
if (options.tableWidthPercent !== undefined) {
|
|
1189
1193
|
table.setWidth(options.tableWidthPercent);
|
|
1190
|
-
table.setWidthType(
|
|
1194
|
+
table.setWidthType("pct");
|
|
1191
1195
|
}
|
|
1192
1196
|
this.bodyElements.splice(paraIndex, 0, table);
|
|
1193
1197
|
return table;
|
|
@@ -1195,60 +1199,60 @@ class Document {
|
|
|
1195
1199
|
applyCustomStylesToDocument() {
|
|
1196
1200
|
const counts = { heading1: 0, heading2: 0, normal: 0 };
|
|
1197
1201
|
const header1Style = Style_1.Style.create({
|
|
1198
|
-
styleId:
|
|
1199
|
-
name:
|
|
1200
|
-
type:
|
|
1201
|
-
basedOn:
|
|
1202
|
+
styleId: "CustomHeader1",
|
|
1203
|
+
name: "Custom Header 1",
|
|
1204
|
+
type: "paragraph",
|
|
1205
|
+
basedOn: "Normal",
|
|
1202
1206
|
runFormatting: {
|
|
1203
|
-
font:
|
|
1207
|
+
font: "Verdana",
|
|
1204
1208
|
size: 18,
|
|
1205
1209
|
bold: true,
|
|
1206
|
-
color:
|
|
1210
|
+
color: "000000",
|
|
1207
1211
|
},
|
|
1208
1212
|
paragraphFormatting: {
|
|
1209
|
-
alignment:
|
|
1213
|
+
alignment: "left",
|
|
1210
1214
|
spacing: {
|
|
1211
1215
|
before: 0,
|
|
1212
|
-
after: 240
|
|
1213
|
-
}
|
|
1214
|
-
}
|
|
1216
|
+
after: 240,
|
|
1217
|
+
},
|
|
1218
|
+
},
|
|
1215
1219
|
});
|
|
1216
1220
|
const header2Style = Style_1.Style.create({
|
|
1217
|
-
styleId:
|
|
1218
|
-
name:
|
|
1219
|
-
type:
|
|
1220
|
-
basedOn:
|
|
1221
|
+
styleId: "CustomHeader2",
|
|
1222
|
+
name: "Custom Header 2",
|
|
1223
|
+
type: "paragraph",
|
|
1224
|
+
basedOn: "Normal",
|
|
1221
1225
|
runFormatting: {
|
|
1222
|
-
font:
|
|
1226
|
+
font: "Verdana",
|
|
1223
1227
|
size: 14,
|
|
1224
1228
|
bold: true,
|
|
1225
|
-
color:
|
|
1229
|
+
color: "000000",
|
|
1226
1230
|
},
|
|
1227
1231
|
paragraphFormatting: {
|
|
1228
|
-
alignment:
|
|
1232
|
+
alignment: "left",
|
|
1229
1233
|
spacing: {
|
|
1230
1234
|
before: 120,
|
|
1231
|
-
after: 120
|
|
1232
|
-
}
|
|
1233
|
-
}
|
|
1235
|
+
after: 120,
|
|
1236
|
+
},
|
|
1237
|
+
},
|
|
1234
1238
|
});
|
|
1235
1239
|
const normalStyle = Style_1.Style.create({
|
|
1236
|
-
styleId:
|
|
1237
|
-
name:
|
|
1238
|
-
type:
|
|
1239
|
-
basedOn:
|
|
1240
|
+
styleId: "CustomNormal",
|
|
1241
|
+
name: "Custom Normal",
|
|
1242
|
+
type: "paragraph",
|
|
1243
|
+
basedOn: "Normal",
|
|
1240
1244
|
runFormatting: {
|
|
1241
|
-
font:
|
|
1245
|
+
font: "Verdana",
|
|
1242
1246
|
size: 12,
|
|
1243
|
-
color:
|
|
1247
|
+
color: "000000",
|
|
1244
1248
|
},
|
|
1245
1249
|
paragraphFormatting: {
|
|
1246
|
-
alignment:
|
|
1250
|
+
alignment: "left",
|
|
1247
1251
|
spacing: {
|
|
1248
1252
|
before: 60,
|
|
1249
|
-
after: 60
|
|
1250
|
-
}
|
|
1251
|
-
}
|
|
1253
|
+
after: 60,
|
|
1254
|
+
},
|
|
1255
|
+
},
|
|
1252
1256
|
});
|
|
1253
1257
|
this.addStyle(header1Style);
|
|
1254
1258
|
this.addStyle(header2Style);
|
|
@@ -1256,17 +1260,17 @@ class Document {
|
|
|
1256
1260
|
const allParagraphs = this.getAllParagraphs();
|
|
1257
1261
|
for (const para of allParagraphs) {
|
|
1258
1262
|
const currentStyle = para.getStyle();
|
|
1259
|
-
if (currentStyle ===
|
|
1260
|
-
para.setStyle(
|
|
1263
|
+
if (currentStyle === "Heading1" || currentStyle === "Heading 1") {
|
|
1264
|
+
para.setStyle("CustomHeader1");
|
|
1261
1265
|
counts.heading1++;
|
|
1262
1266
|
}
|
|
1263
|
-
else if (currentStyle ===
|
|
1264
|
-
para.setStyle(
|
|
1267
|
+
else if (currentStyle === "Heading2" || currentStyle === "Heading 2") {
|
|
1268
|
+
para.setStyle("CustomHeader2");
|
|
1265
1269
|
const { inTable, cell } = this.isParagraphInTable(para);
|
|
1266
1270
|
if (inTable && cell) {
|
|
1267
|
-
cell.setShading({ fill:
|
|
1271
|
+
cell.setShading({ fill: "BFBFBF" });
|
|
1268
1272
|
cell.setMargins({ top: 0, bottom: 0, left: 101, right: 101 });
|
|
1269
|
-
const table = this.getAllTables().find(t => {
|
|
1273
|
+
const table = this.getAllTables().find((t) => {
|
|
1270
1274
|
for (const row of t.getRows()) {
|
|
1271
1275
|
for (const c of row.getCells()) {
|
|
1272
1276
|
if (c === cell)
|
|
@@ -1277,23 +1281,23 @@ class Document {
|
|
|
1277
1281
|
});
|
|
1278
1282
|
if (table) {
|
|
1279
1283
|
table.setWidth(5000);
|
|
1280
|
-
table.setWidthType(
|
|
1284
|
+
table.setWidthType("pct");
|
|
1281
1285
|
}
|
|
1282
1286
|
}
|
|
1283
1287
|
else {
|
|
1284
1288
|
this.wrapParagraphInTable(para, {
|
|
1285
|
-
shading:
|
|
1289
|
+
shading: "BFBFBF",
|
|
1286
1290
|
marginTop: 0,
|
|
1287
1291
|
marginBottom: 0,
|
|
1288
1292
|
marginLeft: 101,
|
|
1289
1293
|
marginRight: 101,
|
|
1290
|
-
tableWidthPercent: 5000
|
|
1294
|
+
tableWidthPercent: 5000,
|
|
1291
1295
|
});
|
|
1292
1296
|
}
|
|
1293
1297
|
counts.heading2++;
|
|
1294
1298
|
}
|
|
1295
|
-
else if (currentStyle ===
|
|
1296
|
-
para.setStyle(
|
|
1299
|
+
else if (currentStyle === "Normal" || currentStyle === undefined) {
|
|
1300
|
+
para.setStyle("CustomNormal");
|
|
1297
1301
|
counts.normal++;
|
|
1298
1302
|
}
|
|
1299
1303
|
}
|
|
@@ -1301,29 +1305,29 @@ class Document {
|
|
|
1301
1305
|
}
|
|
1302
1306
|
static DEFAULT_HEADING1_CONFIG = {
|
|
1303
1307
|
run: {
|
|
1304
|
-
font:
|
|
1308
|
+
font: "Verdana",
|
|
1305
1309
|
size: 18,
|
|
1306
1310
|
bold: true,
|
|
1307
|
-
color:
|
|
1311
|
+
color: "000000",
|
|
1308
1312
|
},
|
|
1309
1313
|
paragraph: {
|
|
1310
|
-
alignment:
|
|
1311
|
-
spacing: { before: 0, after: 240, line: 240, lineRule:
|
|
1314
|
+
alignment: "left",
|
|
1315
|
+
spacing: { before: 0, after: 240, line: 240, lineRule: "auto" },
|
|
1312
1316
|
},
|
|
1313
1317
|
};
|
|
1314
1318
|
static DEFAULT_HEADING2_CONFIG = {
|
|
1315
1319
|
run: {
|
|
1316
|
-
font:
|
|
1320
|
+
font: "Verdana",
|
|
1317
1321
|
size: 14,
|
|
1318
1322
|
bold: true,
|
|
1319
|
-
color:
|
|
1323
|
+
color: "000000",
|
|
1320
1324
|
},
|
|
1321
1325
|
paragraph: {
|
|
1322
|
-
alignment:
|
|
1323
|
-
spacing: { before: 120, after: 120, line: 240, lineRule:
|
|
1326
|
+
alignment: "left",
|
|
1327
|
+
spacing: { before: 120, after: 120, line: 240, lineRule: "auto" },
|
|
1324
1328
|
},
|
|
1325
1329
|
tableOptions: {
|
|
1326
|
-
shading:
|
|
1330
|
+
shading: "BFBFBF",
|
|
1327
1331
|
marginTop: 0,
|
|
1328
1332
|
marginBottom: 0,
|
|
1329
1333
|
marginLeft: 115,
|
|
@@ -1333,99 +1337,114 @@ class Document {
|
|
|
1333
1337
|
};
|
|
1334
1338
|
static DEFAULT_HEADING3_CONFIG = {
|
|
1335
1339
|
run: {
|
|
1336
|
-
font:
|
|
1340
|
+
font: "Verdana",
|
|
1337
1341
|
size: 12,
|
|
1338
1342
|
bold: true,
|
|
1339
|
-
color:
|
|
1343
|
+
color: "000000",
|
|
1340
1344
|
},
|
|
1341
1345
|
paragraph: {
|
|
1342
|
-
alignment:
|
|
1343
|
-
spacing: { before: 60, after: 60, line: 240, lineRule:
|
|
1346
|
+
alignment: "left",
|
|
1347
|
+
spacing: { before: 60, after: 60, line: 240, lineRule: "auto" },
|
|
1344
1348
|
},
|
|
1345
1349
|
};
|
|
1346
1350
|
static DEFAULT_NORMAL_CONFIG = {
|
|
1347
1351
|
run: {
|
|
1348
|
-
font:
|
|
1352
|
+
font: "Verdana",
|
|
1349
1353
|
size: 12,
|
|
1350
|
-
color:
|
|
1354
|
+
color: "000000",
|
|
1351
1355
|
},
|
|
1352
1356
|
paragraph: {
|
|
1353
|
-
alignment:
|
|
1354
|
-
spacing: { before: 60, after: 60, line: 240, lineRule:
|
|
1357
|
+
alignment: "left",
|
|
1358
|
+
spacing: { before: 60, after: 60, line: 240, lineRule: "auto" },
|
|
1355
1359
|
},
|
|
1356
1360
|
};
|
|
1357
1361
|
static DEFAULT_LIST_PARAGRAPH_CONFIG = {
|
|
1358
1362
|
run: {
|
|
1359
|
-
font:
|
|
1363
|
+
font: "Verdana",
|
|
1360
1364
|
size: 12,
|
|
1361
|
-
color:
|
|
1365
|
+
color: "000000",
|
|
1362
1366
|
},
|
|
1363
1367
|
paragraph: {
|
|
1364
|
-
alignment:
|
|
1365
|
-
spacing: { before: 0, after: 60, line: 240, lineRule:
|
|
1368
|
+
alignment: "left",
|
|
1369
|
+
spacing: { before: 0, after: 60, line: 240, lineRule: "auto" },
|
|
1366
1370
|
indentation: { left: 360, hanging: 360 },
|
|
1367
1371
|
contextualSpacing: true,
|
|
1368
1372
|
},
|
|
1369
1373
|
};
|
|
1370
1374
|
applyCustomFormattingToExistingStyles(options) {
|
|
1371
|
-
const results = {
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1375
|
+
const results = {
|
|
1376
|
+
heading1: false,
|
|
1377
|
+
heading2: false,
|
|
1378
|
+
heading3: false,
|
|
1379
|
+
normal: false,
|
|
1380
|
+
listParagraph: false,
|
|
1381
|
+
};
|
|
1382
|
+
const heading1 = this.stylesManager.getStyle("Heading1");
|
|
1383
|
+
const heading2 = this.stylesManager.getStyle("Heading2");
|
|
1384
|
+
const heading3 = this.stylesManager.getStyle("Heading3");
|
|
1385
|
+
const normal = this.stylesManager.getStyle("Normal");
|
|
1386
|
+
const listParagraph = this.stylesManager.getStyle("ListParagraph");
|
|
1377
1387
|
const h1Config = {
|
|
1378
1388
|
run: {
|
|
1379
|
-
...(heading1?.getRunFormatting() ||
|
|
1380
|
-
|
|
1389
|
+
...(heading1?.getRunFormatting() ||
|
|
1390
|
+
Document.DEFAULT_HEADING1_CONFIG.run),
|
|
1391
|
+
...options?.heading1?.run,
|
|
1381
1392
|
},
|
|
1382
1393
|
paragraph: {
|
|
1383
|
-
...(heading1?.getParagraphFormatting() ||
|
|
1384
|
-
|
|
1394
|
+
...(heading1?.getParagraphFormatting() ||
|
|
1395
|
+
Document.DEFAULT_HEADING1_CONFIG.paragraph),
|
|
1396
|
+
...options?.heading1?.paragraph,
|
|
1385
1397
|
},
|
|
1386
1398
|
};
|
|
1387
1399
|
const h2Config = {
|
|
1388
1400
|
run: {
|
|
1389
|
-
...(heading2?.getRunFormatting() ||
|
|
1390
|
-
|
|
1401
|
+
...(heading2?.getRunFormatting() ||
|
|
1402
|
+
Document.DEFAULT_HEADING2_CONFIG.run),
|
|
1403
|
+
...options?.heading2?.run,
|
|
1391
1404
|
},
|
|
1392
1405
|
paragraph: {
|
|
1393
|
-
...(heading2?.getParagraphFormatting() ||
|
|
1394
|
-
|
|
1406
|
+
...(heading2?.getParagraphFormatting() ||
|
|
1407
|
+
Document.DEFAULT_HEADING2_CONFIG.paragraph),
|
|
1408
|
+
...options?.heading2?.paragraph,
|
|
1395
1409
|
},
|
|
1396
1410
|
tableOptions: {
|
|
1397
1411
|
...Document.DEFAULT_HEADING2_CONFIG.tableOptions,
|
|
1398
|
-
...options?.heading2?.tableOptions
|
|
1412
|
+
...options?.heading2?.tableOptions,
|
|
1399
1413
|
},
|
|
1400
1414
|
};
|
|
1401
1415
|
const h3Config = {
|
|
1402
1416
|
run: {
|
|
1403
|
-
...(heading3?.getRunFormatting() ||
|
|
1404
|
-
|
|
1417
|
+
...(heading3?.getRunFormatting() ||
|
|
1418
|
+
Document.DEFAULT_HEADING3_CONFIG.run),
|
|
1419
|
+
...options?.heading3?.run,
|
|
1405
1420
|
},
|
|
1406
1421
|
paragraph: {
|
|
1407
|
-
...(heading3?.getParagraphFormatting() ||
|
|
1408
|
-
|
|
1422
|
+
...(heading3?.getParagraphFormatting() ||
|
|
1423
|
+
Document.DEFAULT_HEADING3_CONFIG.paragraph),
|
|
1424
|
+
...options?.heading3?.paragraph,
|
|
1409
1425
|
},
|
|
1410
1426
|
};
|
|
1411
1427
|
const normalConfig = {
|
|
1412
1428
|
run: {
|
|
1413
1429
|
...(normal?.getRunFormatting() || Document.DEFAULT_NORMAL_CONFIG.run),
|
|
1414
|
-
...options?.normal?.run
|
|
1430
|
+
...options?.normal?.run,
|
|
1415
1431
|
},
|
|
1416
1432
|
paragraph: {
|
|
1417
|
-
...(normal?.getParagraphFormatting() ||
|
|
1418
|
-
|
|
1433
|
+
...(normal?.getParagraphFormatting() ||
|
|
1434
|
+
Document.DEFAULT_NORMAL_CONFIG.paragraph),
|
|
1435
|
+
...options?.normal?.paragraph,
|
|
1419
1436
|
},
|
|
1420
1437
|
};
|
|
1421
1438
|
const listParaConfig = {
|
|
1422
1439
|
run: {
|
|
1423
|
-
...(listParagraph?.getRunFormatting() ||
|
|
1424
|
-
|
|
1440
|
+
...(listParagraph?.getRunFormatting() ||
|
|
1441
|
+
Document.DEFAULT_LIST_PARAGRAPH_CONFIG.run),
|
|
1442
|
+
...options?.listParagraph?.run,
|
|
1425
1443
|
},
|
|
1426
1444
|
paragraph: {
|
|
1427
|
-
...(listParagraph?.getParagraphFormatting() ||
|
|
1428
|
-
|
|
1445
|
+
...(listParagraph?.getParagraphFormatting() ||
|
|
1446
|
+
Document.DEFAULT_LIST_PARAGRAPH_CONFIG.paragraph),
|
|
1447
|
+
...options?.listParagraph?.paragraph,
|
|
1429
1448
|
},
|
|
1430
1449
|
};
|
|
1431
1450
|
const preserveBlankLines = options?.preserveBlankLinesAfterHeader2Tables ?? true;
|
|
@@ -1496,7 +1515,7 @@ class Document {
|
|
|
1496
1515
|
continue;
|
|
1497
1516
|
}
|
|
1498
1517
|
const styleId = para.getStyle();
|
|
1499
|
-
if (styleId ===
|
|
1518
|
+
if (styleId === "Heading1" && heading1) {
|
|
1500
1519
|
para.clearDirectFormattingConflicts(heading1);
|
|
1501
1520
|
for (const run of para.getRuns()) {
|
|
1502
1521
|
if (!h1Preserve.bold) {
|
|
@@ -1506,25 +1525,33 @@ class Document {
|
|
|
1506
1525
|
run.setItalic(h1Config.run?.italic ?? false);
|
|
1507
1526
|
}
|
|
1508
1527
|
if (!h1Preserve.underline) {
|
|
1509
|
-
run.setUnderline(h1Config.run?.underline ?
|
|
1528
|
+
run.setUnderline(h1Config.run?.underline ? "single" : false);
|
|
1510
1529
|
}
|
|
1511
1530
|
}
|
|
1512
1531
|
if (para.formatting.paragraphMarkRunProperties) {
|
|
1513
1532
|
const markProps = para.formatting.paragraphMarkRunProperties;
|
|
1514
|
-
if (!h1Preserve.bold &&
|
|
1533
|
+
if (!h1Preserve.bold &&
|
|
1534
|
+
h1Config.run?.bold === false &&
|
|
1535
|
+
markProps.bold) {
|
|
1515
1536
|
delete markProps.bold;
|
|
1516
1537
|
}
|
|
1517
|
-
if (!h1Preserve.italic &&
|
|
1538
|
+
if (!h1Preserve.italic &&
|
|
1539
|
+
h1Config.run?.italic === false &&
|
|
1540
|
+
markProps.italic) {
|
|
1518
1541
|
delete markProps.italic;
|
|
1519
1542
|
}
|
|
1520
|
-
if (!h1Preserve.underline &&
|
|
1543
|
+
if (!h1Preserve.underline &&
|
|
1544
|
+
h1Config.run?.underline === false &&
|
|
1545
|
+
markProps.underline) {
|
|
1521
1546
|
delete markProps.underline;
|
|
1522
1547
|
}
|
|
1523
1548
|
}
|
|
1524
1549
|
processedParagraphs.add(para);
|
|
1525
1550
|
}
|
|
1526
|
-
else if (styleId ===
|
|
1527
|
-
const hasContent = para
|
|
1551
|
+
else if (styleId === "Heading2" && heading2) {
|
|
1552
|
+
const hasContent = para
|
|
1553
|
+
.getRuns()
|
|
1554
|
+
.some((run) => run.getText().trim().length > 0);
|
|
1528
1555
|
if (!hasContent) {
|
|
1529
1556
|
processedParagraphs.add(para);
|
|
1530
1557
|
continue;
|
|
@@ -1538,18 +1565,24 @@ class Document {
|
|
|
1538
1565
|
run.setItalic(h2Config.run?.italic ?? false);
|
|
1539
1566
|
}
|
|
1540
1567
|
if (!h2Preserve.underline) {
|
|
1541
|
-
run.setUnderline(h2Config.run?.underline ?
|
|
1568
|
+
run.setUnderline(h2Config.run?.underline ? "single" : false);
|
|
1542
1569
|
}
|
|
1543
1570
|
}
|
|
1544
1571
|
if (para.formatting.paragraphMarkRunProperties) {
|
|
1545
1572
|
const markProps = para.formatting.paragraphMarkRunProperties;
|
|
1546
|
-
if (!h2Preserve.bold &&
|
|
1573
|
+
if (!h2Preserve.bold &&
|
|
1574
|
+
h2Config.run?.bold === false &&
|
|
1575
|
+
markProps.bold) {
|
|
1547
1576
|
delete markProps.bold;
|
|
1548
1577
|
}
|
|
1549
|
-
if (!h2Preserve.italic &&
|
|
1578
|
+
if (!h2Preserve.italic &&
|
|
1579
|
+
h2Config.run?.italic === false &&
|
|
1580
|
+
markProps.italic) {
|
|
1550
1581
|
delete markProps.italic;
|
|
1551
1582
|
}
|
|
1552
|
-
if (!h2Preserve.underline &&
|
|
1583
|
+
if (!h2Preserve.underline &&
|
|
1584
|
+
h2Config.run?.underline === false &&
|
|
1585
|
+
markProps.underline) {
|
|
1553
1586
|
delete markProps.underline;
|
|
1554
1587
|
}
|
|
1555
1588
|
}
|
|
@@ -1558,8 +1591,10 @@ class Document {
|
|
|
1558
1591
|
if (h2Config.tableOptions?.shading) {
|
|
1559
1592
|
cell.setShading({ fill: h2Config.tableOptions.shading });
|
|
1560
1593
|
}
|
|
1561
|
-
if (h2Config.tableOptions?.marginTop !== undefined ||
|
|
1562
|
-
h2Config.tableOptions?.
|
|
1594
|
+
if (h2Config.tableOptions?.marginTop !== undefined ||
|
|
1595
|
+
h2Config.tableOptions?.marginBottom !== undefined ||
|
|
1596
|
+
h2Config.tableOptions?.marginLeft !== undefined ||
|
|
1597
|
+
h2Config.tableOptions?.marginRight !== undefined) {
|
|
1563
1598
|
cell.setMargins({
|
|
1564
1599
|
top: h2Config.tableOptions.marginTop ?? 0,
|
|
1565
1600
|
bottom: h2Config.tableOptions.marginBottom ?? 0,
|
|
@@ -1568,7 +1603,7 @@ class Document {
|
|
|
1568
1603
|
});
|
|
1569
1604
|
}
|
|
1570
1605
|
if (h2Config.tableOptions?.tableWidthPercent) {
|
|
1571
|
-
const table = this.getAllTables().find(t => {
|
|
1606
|
+
const table = this.getAllTables().find((t) => {
|
|
1572
1607
|
for (const row of t.getRows()) {
|
|
1573
1608
|
for (const c of row.getCells()) {
|
|
1574
1609
|
if (c === cell)
|
|
@@ -1579,13 +1614,13 @@ class Document {
|
|
|
1579
1614
|
});
|
|
1580
1615
|
if (table) {
|
|
1581
1616
|
table.setWidth(h2Config.tableOptions.tableWidthPercent);
|
|
1582
|
-
table.setWidthType(
|
|
1617
|
+
table.setWidthType("pct");
|
|
1583
1618
|
}
|
|
1584
1619
|
}
|
|
1585
1620
|
}
|
|
1586
1621
|
else {
|
|
1587
1622
|
const table = this.wrapParagraphInTable(para, {
|
|
1588
|
-
shading: h2Config.tableOptions?.shading ??
|
|
1623
|
+
shading: h2Config.tableOptions?.shading ?? "BFBFBF",
|
|
1589
1624
|
marginTop: h2Config.tableOptions?.marginTop ?? 0,
|
|
1590
1625
|
marginBottom: h2Config.tableOptions?.marginBottom ?? 0,
|
|
1591
1626
|
marginLeft: h2Config.tableOptions?.marginLeft ?? 115,
|
|
@@ -1602,12 +1637,12 @@ class Document {
|
|
|
1602
1637
|
if (!content || content.length === 0)
|
|
1603
1638
|
return true;
|
|
1604
1639
|
for (const item of content) {
|
|
1605
|
-
if (item.constructor.name ===
|
|
1640
|
+
if (item.constructor.name === "Hyperlink") {
|
|
1606
1641
|
return false;
|
|
1607
1642
|
}
|
|
1608
1643
|
if (item.getText) {
|
|
1609
1644
|
const text = item.getText().trim();
|
|
1610
|
-
if (text !==
|
|
1645
|
+
if (text !== "")
|
|
1611
1646
|
return false;
|
|
1612
1647
|
}
|
|
1613
1648
|
}
|
|
@@ -1625,7 +1660,7 @@ class Document {
|
|
|
1625
1660
|
}
|
|
1626
1661
|
processedParagraphs.add(para);
|
|
1627
1662
|
}
|
|
1628
|
-
else if (styleId ===
|
|
1663
|
+
else if (styleId === "Heading3" && heading3) {
|
|
1629
1664
|
para.clearDirectFormattingConflicts(heading3);
|
|
1630
1665
|
for (const run of para.getRuns()) {
|
|
1631
1666
|
if (!h3Preserve.bold) {
|
|
@@ -1635,31 +1670,37 @@ class Document {
|
|
|
1635
1670
|
run.setItalic(h3Config.run?.italic ?? false);
|
|
1636
1671
|
}
|
|
1637
1672
|
if (!h3Preserve.underline) {
|
|
1638
|
-
run.setUnderline(h3Config.run?.underline ?
|
|
1673
|
+
run.setUnderline(h3Config.run?.underline ? "single" : false);
|
|
1639
1674
|
}
|
|
1640
1675
|
}
|
|
1641
1676
|
if (para.formatting.paragraphMarkRunProperties) {
|
|
1642
1677
|
const markProps = para.formatting.paragraphMarkRunProperties;
|
|
1643
|
-
if (!h3Preserve.bold &&
|
|
1678
|
+
if (!h3Preserve.bold &&
|
|
1679
|
+
h3Config.run?.bold === false &&
|
|
1680
|
+
markProps.bold) {
|
|
1644
1681
|
delete markProps.bold;
|
|
1645
1682
|
}
|
|
1646
|
-
if (!h3Preserve.italic &&
|
|
1683
|
+
if (!h3Preserve.italic &&
|
|
1684
|
+
h3Config.run?.italic === false &&
|
|
1685
|
+
markProps.italic) {
|
|
1647
1686
|
delete markProps.italic;
|
|
1648
1687
|
}
|
|
1649
|
-
if (!h3Preserve.underline &&
|
|
1688
|
+
if (!h3Preserve.underline &&
|
|
1689
|
+
h3Config.run?.underline === false &&
|
|
1690
|
+
markProps.underline) {
|
|
1650
1691
|
delete markProps.underline;
|
|
1651
1692
|
}
|
|
1652
1693
|
}
|
|
1653
1694
|
processedParagraphs.add(para);
|
|
1654
1695
|
}
|
|
1655
|
-
else if (styleId ===
|
|
1656
|
-
const preservedFormatting = para.getRuns().map(run => {
|
|
1696
|
+
else if (styleId === "ListParagraph" && listParagraph) {
|
|
1697
|
+
const preservedFormatting = para.getRuns().map((run) => {
|
|
1657
1698
|
const fmt = run.getFormatting();
|
|
1658
1699
|
return {
|
|
1659
1700
|
run: run,
|
|
1660
1701
|
bold: listParaPreserve.bold ? fmt.bold : undefined,
|
|
1661
1702
|
italic: listParaPreserve.italic ? fmt.italic : undefined,
|
|
1662
|
-
underline: listParaPreserve.underline ? fmt.underline : undefined
|
|
1703
|
+
underline: listParaPreserve.underline ? fmt.underline : undefined,
|
|
1663
1704
|
};
|
|
1664
1705
|
});
|
|
1665
1706
|
para.clearDirectFormattingConflicts(listParagraph);
|
|
@@ -1682,31 +1723,37 @@ class Document {
|
|
|
1682
1723
|
run.setItalic(listParaConfig.run?.italic ?? false);
|
|
1683
1724
|
}
|
|
1684
1725
|
if (!listParaPreserve.underline) {
|
|
1685
|
-
run.setUnderline(listParaConfig.run?.underline ?
|
|
1726
|
+
run.setUnderline(listParaConfig.run?.underline ? "single" : false);
|
|
1686
1727
|
}
|
|
1687
1728
|
}
|
|
1688
1729
|
if (para.formatting.paragraphMarkRunProperties) {
|
|
1689
1730
|
const markProps = para.formatting.paragraphMarkRunProperties;
|
|
1690
|
-
if (!listParaPreserve.bold &&
|
|
1731
|
+
if (!listParaPreserve.bold &&
|
|
1732
|
+
listParaConfig.run?.bold === false &&
|
|
1733
|
+
markProps.bold) {
|
|
1691
1734
|
delete markProps.bold;
|
|
1692
1735
|
}
|
|
1693
|
-
if (!listParaPreserve.italic &&
|
|
1736
|
+
if (!listParaPreserve.italic &&
|
|
1737
|
+
listParaConfig.run?.italic === false &&
|
|
1738
|
+
markProps.italic) {
|
|
1694
1739
|
delete markProps.italic;
|
|
1695
1740
|
}
|
|
1696
|
-
if (!listParaPreserve.underline &&
|
|
1741
|
+
if (!listParaPreserve.underline &&
|
|
1742
|
+
listParaConfig.run?.underline === false &&
|
|
1743
|
+
markProps.underline) {
|
|
1697
1744
|
delete markProps.underline;
|
|
1698
1745
|
}
|
|
1699
1746
|
}
|
|
1700
1747
|
processedParagraphs.add(para);
|
|
1701
1748
|
}
|
|
1702
|
-
else if ((styleId ===
|
|
1703
|
-
const preservedFormatting = para.getRuns().map(run => {
|
|
1749
|
+
else if ((styleId === "Normal" || styleId === undefined) && normal) {
|
|
1750
|
+
const preservedFormatting = para.getRuns().map((run) => {
|
|
1704
1751
|
const fmt = run.getFormatting();
|
|
1705
1752
|
return {
|
|
1706
1753
|
run: run,
|
|
1707
1754
|
bold: normalPreserve.bold ? fmt.bold : undefined,
|
|
1708
1755
|
italic: normalPreserve.italic ? fmt.italic : undefined,
|
|
1709
|
-
underline: normalPreserve.underline ? fmt.underline : undefined
|
|
1756
|
+
underline: normalPreserve.underline ? fmt.underline : undefined,
|
|
1710
1757
|
};
|
|
1711
1758
|
});
|
|
1712
1759
|
para.clearDirectFormattingConflicts(normal);
|
|
@@ -1729,18 +1776,24 @@ class Document {
|
|
|
1729
1776
|
run.setItalic(normalConfig.run?.italic ?? false);
|
|
1730
1777
|
}
|
|
1731
1778
|
if (!normalPreserve.underline) {
|
|
1732
|
-
run.setUnderline(normalConfig.run?.underline ?
|
|
1779
|
+
run.setUnderline(normalConfig.run?.underline ? "single" : false);
|
|
1733
1780
|
}
|
|
1734
1781
|
}
|
|
1735
1782
|
if (para.formatting.paragraphMarkRunProperties) {
|
|
1736
1783
|
const markProps = para.formatting.paragraphMarkRunProperties;
|
|
1737
|
-
if (!normalPreserve.bold &&
|
|
1784
|
+
if (!normalPreserve.bold &&
|
|
1785
|
+
normalConfig.run?.bold === false &&
|
|
1786
|
+
markProps.bold) {
|
|
1738
1787
|
delete markProps.bold;
|
|
1739
1788
|
}
|
|
1740
|
-
if (!normalPreserve.italic &&
|
|
1789
|
+
if (!normalPreserve.italic &&
|
|
1790
|
+
normalConfig.run?.italic === false &&
|
|
1791
|
+
markProps.italic) {
|
|
1741
1792
|
delete markProps.italic;
|
|
1742
1793
|
}
|
|
1743
|
-
if (!normalPreserve.underline &&
|
|
1794
|
+
if (!normalPreserve.underline &&
|
|
1795
|
+
normalConfig.run?.underline === false &&
|
|
1796
|
+
markProps.underline) {
|
|
1744
1797
|
delete markProps.underline;
|
|
1745
1798
|
}
|
|
1746
1799
|
}
|
|
@@ -1754,32 +1807,32 @@ class Document {
|
|
|
1754
1807
|
for (const style of styles) {
|
|
1755
1808
|
const styleId = style.getStyleId();
|
|
1756
1809
|
switch (styleId) {
|
|
1757
|
-
case
|
|
1810
|
+
case "Heading1":
|
|
1758
1811
|
options.heading1 = {
|
|
1759
1812
|
run: style.getRunFormatting(),
|
|
1760
1813
|
paragraph: style.getParagraphFormatting(),
|
|
1761
1814
|
};
|
|
1762
1815
|
break;
|
|
1763
|
-
case
|
|
1816
|
+
case "Heading2":
|
|
1764
1817
|
options.heading2 = {
|
|
1765
1818
|
run: style.getRunFormatting(),
|
|
1766
1819
|
paragraph: style.getParagraphFormatting(),
|
|
1767
1820
|
tableOptions: style.getHeading2TableOptions(),
|
|
1768
1821
|
};
|
|
1769
1822
|
break;
|
|
1770
|
-
case
|
|
1823
|
+
case "Heading3":
|
|
1771
1824
|
options.heading3 = {
|
|
1772
1825
|
run: style.getRunFormatting(),
|
|
1773
1826
|
paragraph: style.getParagraphFormatting(),
|
|
1774
1827
|
};
|
|
1775
1828
|
break;
|
|
1776
|
-
case
|
|
1829
|
+
case "Normal":
|
|
1777
1830
|
options.normal = {
|
|
1778
1831
|
run: style.getRunFormatting(),
|
|
1779
1832
|
paragraph: style.getParagraphFormatting(),
|
|
1780
1833
|
};
|
|
1781
1834
|
break;
|
|
1782
|
-
case
|
|
1835
|
+
case "ListParagraph":
|
|
1783
1836
|
options.listParagraph = {
|
|
1784
1837
|
run: style.getRunFormatting(),
|
|
1785
1838
|
paragraph: style.getParagraphFormatting(),
|
|
@@ -1842,7 +1895,7 @@ class Document {
|
|
|
1842
1895
|
return {
|
|
1843
1896
|
removed,
|
|
1844
1897
|
preserved,
|
|
1845
|
-
total: toRemove.length
|
|
1898
|
+
total: toRemove.length,
|
|
1846
1899
|
};
|
|
1847
1900
|
}
|
|
1848
1901
|
processConsecutiveBlanks(blanks, keepOne, toRemove) {
|
|
@@ -1875,8 +1928,10 @@ class Document {
|
|
|
1875
1928
|
let hasHeader2 = false;
|
|
1876
1929
|
for (const para of cellParas) {
|
|
1877
1930
|
const style = para.getStyle();
|
|
1878
|
-
if (style ===
|
|
1879
|
-
style ===
|
|
1931
|
+
if (style === "Heading2" ||
|
|
1932
|
+
style === "Heading 2" ||
|
|
1933
|
+
style === "CustomHeader2" ||
|
|
1934
|
+
style === "Header2") {
|
|
1880
1935
|
hasHeader2 = true;
|
|
1881
1936
|
break;
|
|
1882
1937
|
}
|
|
@@ -1928,6 +1983,67 @@ class Document {
|
|
|
1928
1983
|
}
|
|
1929
1984
|
else {
|
|
1930
1985
|
const blankPara = Paragraph_1.Paragraph.create();
|
|
1986
|
+
blankPara.setStyle("Normal");
|
|
1987
|
+
blankPara.setSpaceAfter(spacingAfter);
|
|
1988
|
+
if (markAsPreserved) {
|
|
1989
|
+
blankPara.setPreserved(true);
|
|
1990
|
+
}
|
|
1991
|
+
this.bodyElements.splice(tableIndex + 1, 0, blankPara);
|
|
1992
|
+
blankLinesAdded++;
|
|
1993
|
+
}
|
|
1994
|
+
}
|
|
1995
|
+
else {
|
|
1996
|
+
const blankPara = Paragraph_1.Paragraph.create();
|
|
1997
|
+
blankPara.setStyle("Normal");
|
|
1998
|
+
blankPara.setSpaceAfter(spacingAfter);
|
|
1999
|
+
if (markAsPreserved) {
|
|
2000
|
+
blankPara.setPreserved(true);
|
|
2001
|
+
}
|
|
2002
|
+
this.bodyElements.splice(tableIndex + 1, 0, blankPara);
|
|
2003
|
+
blankLinesAdded++;
|
|
2004
|
+
}
|
|
2005
|
+
}
|
|
2006
|
+
return {
|
|
2007
|
+
tablesProcessed,
|
|
2008
|
+
blankLinesAdded,
|
|
2009
|
+
existingLinesMarked,
|
|
2010
|
+
};
|
|
2011
|
+
}
|
|
2012
|
+
ensureBlankLinesAfterOtherTables(options) {
|
|
2013
|
+
const spacingAfter = options?.spacingAfter ?? 120;
|
|
2014
|
+
const markAsPreserved = options?.markAsPreserved ?? true;
|
|
2015
|
+
const filter = options?.filter;
|
|
2016
|
+
let tablesProcessed = 0;
|
|
2017
|
+
let blankLinesAdded = 0;
|
|
2018
|
+
let existingLinesMarked = 0;
|
|
2019
|
+
const tables = this.getAllTables();
|
|
2020
|
+
for (let i = 0; i < tables.length; i++) {
|
|
2021
|
+
const table = tables[i];
|
|
2022
|
+
if (!table)
|
|
2023
|
+
continue;
|
|
2024
|
+
const rowCount = table.getRowCount();
|
|
2025
|
+
const colCount = table.getColumnCount();
|
|
2026
|
+
if (rowCount === 1 && colCount === 1) {
|
|
2027
|
+
continue;
|
|
2028
|
+
}
|
|
2029
|
+
if (filter && !filter(table, i)) {
|
|
2030
|
+
continue;
|
|
2031
|
+
}
|
|
2032
|
+
tablesProcessed++;
|
|
2033
|
+
const tableIndex = this.bodyElements.indexOf(table);
|
|
2034
|
+
if (tableIndex === -1)
|
|
2035
|
+
continue;
|
|
2036
|
+
const nextElement = this.bodyElements[tableIndex + 1];
|
|
2037
|
+
if (nextElement instanceof Paragraph_1.Paragraph) {
|
|
2038
|
+
if (this.isParagraphBlank(nextElement)) {
|
|
2039
|
+
if (markAsPreserved && !nextElement.isPreserved()) {
|
|
2040
|
+
nextElement.setPreserved(true);
|
|
2041
|
+
existingLinesMarked++;
|
|
2042
|
+
}
|
|
2043
|
+
}
|
|
2044
|
+
else {
|
|
2045
|
+
const blankPara = Paragraph_1.Paragraph.create();
|
|
2046
|
+
blankPara.setStyle("Normal");
|
|
1931
2047
|
blankPara.setSpaceAfter(spacingAfter);
|
|
1932
2048
|
if (markAsPreserved) {
|
|
1933
2049
|
blankPara.setPreserved(true);
|
|
@@ -1938,6 +2054,7 @@ class Document {
|
|
|
1938
2054
|
}
|
|
1939
2055
|
else {
|
|
1940
2056
|
const blankPara = Paragraph_1.Paragraph.create();
|
|
2057
|
+
blankPara.setStyle("Normal");
|
|
1941
2058
|
blankPara.setSpaceAfter(spacingAfter);
|
|
1942
2059
|
if (markAsPreserved) {
|
|
1943
2060
|
blankPara.setPreserved(true);
|
|
@@ -1952,6 +2069,73 @@ class Document {
|
|
|
1952
2069
|
existingLinesMarked,
|
|
1953
2070
|
};
|
|
1954
2071
|
}
|
|
2072
|
+
standardizeBulletSymbols(options) {
|
|
2073
|
+
const { bold = true, fontSize = 24, color = "000000", font = "Arial", } = options || {};
|
|
2074
|
+
let listsUpdated = 0;
|
|
2075
|
+
let levelsModified = 0;
|
|
2076
|
+
const instances = this.numberingManager.getAllInstances();
|
|
2077
|
+
for (const instance of instances) {
|
|
2078
|
+
const abstractNumId = instance.getAbstractNumId();
|
|
2079
|
+
const abstractNum = this.numberingManager.getAbstractNumbering(abstractNumId);
|
|
2080
|
+
if (!abstractNum)
|
|
2081
|
+
continue;
|
|
2082
|
+
const level0 = abstractNum.getLevel(0);
|
|
2083
|
+
if (!level0 || level0.getFormat() !== "bullet")
|
|
2084
|
+
continue;
|
|
2085
|
+
for (let levelIndex = 0; levelIndex < 9; levelIndex++) {
|
|
2086
|
+
const numLevel = abstractNum.getLevel(levelIndex);
|
|
2087
|
+
if (!numLevel)
|
|
2088
|
+
continue;
|
|
2089
|
+
numLevel.setFont(font);
|
|
2090
|
+
numLevel.setFontSize(fontSize);
|
|
2091
|
+
numLevel.setBold(bold);
|
|
2092
|
+
numLevel.setColor(color);
|
|
2093
|
+
levelsModified++;
|
|
2094
|
+
}
|
|
2095
|
+
listsUpdated++;
|
|
2096
|
+
}
|
|
2097
|
+
return { listsUpdated, levelsModified };
|
|
2098
|
+
}
|
|
2099
|
+
standardizeNumberedListPrefixes(options) {
|
|
2100
|
+
const { bold = true, fontSize = 24, color = "000000", font = "Verdana", } = options || {};
|
|
2101
|
+
let listsUpdated = 0;
|
|
2102
|
+
let levelsModified = 0;
|
|
2103
|
+
const instances = this.numberingManager.getAllInstances();
|
|
2104
|
+
for (const instance of instances) {
|
|
2105
|
+
const abstractNumId = instance.getAbstractNumId();
|
|
2106
|
+
const abstractNum = this.numberingManager.getAbstractNumbering(abstractNumId);
|
|
2107
|
+
if (!abstractNum)
|
|
2108
|
+
continue;
|
|
2109
|
+
const level0 = abstractNum.getLevel(0);
|
|
2110
|
+
if (!level0 || level0.getFormat() === "bullet")
|
|
2111
|
+
continue;
|
|
2112
|
+
for (let levelIndex = 0; levelIndex < 9; levelIndex++) {
|
|
2113
|
+
const numLevel = abstractNum.getLevel(levelIndex);
|
|
2114
|
+
if (!numLevel)
|
|
2115
|
+
continue;
|
|
2116
|
+
numLevel.setFont(font);
|
|
2117
|
+
numLevel.setFontSize(fontSize);
|
|
2118
|
+
numLevel.setBold(bold);
|
|
2119
|
+
numLevel.setColor(color);
|
|
2120
|
+
levelsModified++;
|
|
2121
|
+
}
|
|
2122
|
+
listsUpdated++;
|
|
2123
|
+
}
|
|
2124
|
+
return { listsUpdated, levelsModified };
|
|
2125
|
+
}
|
|
2126
|
+
standardizeAllHyperlinks(options) {
|
|
2127
|
+
const { font = "Verdana", size = 12, color = "0000FF", underline = true, } = options || {};
|
|
2128
|
+
const hyperlinks = this.getHyperlinks();
|
|
2129
|
+
for (const { hyperlink } of hyperlinks) {
|
|
2130
|
+
hyperlink.setFormatting({
|
|
2131
|
+
font: font,
|
|
2132
|
+
size: size,
|
|
2133
|
+
color: color,
|
|
2134
|
+
underline: underline ? "single" : false,
|
|
2135
|
+
});
|
|
2136
|
+
}
|
|
2137
|
+
return hyperlinks.length;
|
|
2138
|
+
}
|
|
1955
2139
|
cleanFormatting(styleNames) {
|
|
1956
2140
|
let cleaned = 0;
|
|
1957
2141
|
for (const para of this.bodyElements) {
|
|
@@ -1970,24 +2154,26 @@ class Document {
|
|
|
1970
2154
|
return cleaned;
|
|
1971
2155
|
}
|
|
1972
2156
|
applyH1(options) {
|
|
1973
|
-
return this.applyStyleToMatching(
|
|
2157
|
+
return this.applyStyleToMatching("Heading1", options, (style) => /^(heading\s*1|header\s*1|h1)$/i.test(style));
|
|
1974
2158
|
}
|
|
1975
2159
|
applyH2(options) {
|
|
1976
|
-
return this.applyStyleToMatching(
|
|
2160
|
+
return this.applyStyleToMatching("Heading2", options, (style) => /^(heading\s*2|header\s*2|h2)$/i.test(style));
|
|
1977
2161
|
}
|
|
1978
2162
|
applyH3(options) {
|
|
1979
|
-
return this.applyStyleToMatching(
|
|
2163
|
+
return this.applyStyleToMatching("Heading3", options, (style) => /^(heading\s*3|header\s*3|h3)$/i.test(style));
|
|
1980
2164
|
}
|
|
1981
2165
|
applyNormal(options) {
|
|
1982
|
-
const targets = options?.paragraphs ||
|
|
1983
|
-
|
|
1984
|
-
|
|
1985
|
-
|
|
2166
|
+
const targets = options?.paragraphs ||
|
|
2167
|
+
this.getAllParagraphs().filter((p) => {
|
|
2168
|
+
const style = p.getStyle();
|
|
2169
|
+
return (!style ||
|
|
2170
|
+
!/^(heading|header|h\d|list|toc|tod|caution|table)/i.test(style));
|
|
2171
|
+
});
|
|
1986
2172
|
let count = 0;
|
|
1987
2173
|
for (const para of targets) {
|
|
1988
2174
|
if (para.isPreserved())
|
|
1989
2175
|
continue;
|
|
1990
|
-
para.setStyle(
|
|
2176
|
+
para.setStyle("Normal");
|
|
1991
2177
|
if (options?.keepProperties && options.keepProperties.length > 0) {
|
|
1992
2178
|
this.clearFormattingExcept(para, options.keepProperties);
|
|
1993
2179
|
}
|
|
@@ -2002,19 +2188,19 @@ class Document {
|
|
|
2002
2188
|
return count;
|
|
2003
2189
|
}
|
|
2004
2190
|
applyNumList(options) {
|
|
2005
|
-
return this.applyStyleToMatching(
|
|
2191
|
+
return this.applyStyleToMatching("ListParagraph", options, (style) => /^(list\s*number|numbered\s*list|list\s*paragraph)$/i.test(style));
|
|
2006
2192
|
}
|
|
2007
2193
|
applyBulletList(options) {
|
|
2008
|
-
return this.applyStyleToMatching(
|
|
2194
|
+
return this.applyStyleToMatching("ListParagraph", options, (style) => /^(list\s*bullet|bullet\s*list|list\s*paragraph)$/i.test(style));
|
|
2009
2195
|
}
|
|
2010
2196
|
applyTOC(options) {
|
|
2011
|
-
return this.applyStyleToMatching(
|
|
2197
|
+
return this.applyStyleToMatching("TOC", options, (style) => /^(toc|table\s*of\s*contents|toc\s*heading)$/i.test(style));
|
|
2012
2198
|
}
|
|
2013
2199
|
applyTOD(options) {
|
|
2014
|
-
return this.applyStyleToMatching(
|
|
2200
|
+
return this.applyStyleToMatching("TopOfDocument", options, (style) => /^(tod|top\s*of\s*document|document\s*top)$/i.test(style));
|
|
2015
2201
|
}
|
|
2016
2202
|
applyCaution(options) {
|
|
2017
|
-
return this.applyStyleToMatching(
|
|
2203
|
+
return this.applyStyleToMatching("Caution", options, (style) => /^(caution|warning|important|alert)$/i.test(style));
|
|
2018
2204
|
}
|
|
2019
2205
|
applyCellHeader(options) {
|
|
2020
2206
|
let count = 0;
|
|
@@ -2027,7 +2213,7 @@ class Document {
|
|
|
2027
2213
|
for (const para of cell.getParagraphs()) {
|
|
2028
2214
|
if (para.isPreserved())
|
|
2029
2215
|
continue;
|
|
2030
|
-
para.setStyle(
|
|
2216
|
+
para.setStyle("TableHeader");
|
|
2031
2217
|
if (options?.keepProperties && options.keepProperties.length > 0) {
|
|
2032
2218
|
this.clearFormattingExcept(para, options.keepProperties);
|
|
2033
2219
|
}
|
|
@@ -2062,13 +2248,13 @@ class Document {
|
|
|
2062
2248
|
if (options.color)
|
|
2063
2249
|
run.setColor(options.color);
|
|
2064
2250
|
if (options.emphasis) {
|
|
2065
|
-
options.emphasis.forEach(emp => {
|
|
2066
|
-
if (emp ===
|
|
2251
|
+
options.emphasis.forEach((emp) => {
|
|
2252
|
+
if (emp === "bold")
|
|
2067
2253
|
run.setBold(true);
|
|
2068
|
-
if (emp ===
|
|
2254
|
+
if (emp === "italic")
|
|
2069
2255
|
run.setItalic(true);
|
|
2070
|
-
if (emp ===
|
|
2071
|
-
run.setUnderline(
|
|
2256
|
+
if (emp === "underline")
|
|
2257
|
+
run.setUnderline("single");
|
|
2072
2258
|
});
|
|
2073
2259
|
}
|
|
2074
2260
|
}
|
|
@@ -2153,10 +2339,11 @@ class Document {
|
|
|
2153
2339
|
}
|
|
2154
2340
|
}
|
|
2155
2341
|
applyStyleToMatching(targetStyle, options, matcher) {
|
|
2156
|
-
const targets = options?.paragraphs ||
|
|
2157
|
-
|
|
2158
|
-
|
|
2159
|
-
|
|
2342
|
+
const targets = options?.paragraphs ||
|
|
2343
|
+
this.getAllParagraphs().filter((p) => {
|
|
2344
|
+
const style = p.getStyle();
|
|
2345
|
+
return style && matcher(style);
|
|
2346
|
+
});
|
|
2160
2347
|
let count = 0;
|
|
2161
2348
|
for (const para of targets) {
|
|
2162
2349
|
if (para.isPreserved())
|
|
@@ -2181,26 +2368,27 @@ class Document {
|
|
|
2181
2368
|
return true;
|
|
2182
2369
|
}
|
|
2183
2370
|
for (const item of content) {
|
|
2184
|
-
if (item.constructor.name ===
|
|
2371
|
+
if (item.constructor.name === "Hyperlink") {
|
|
2185
2372
|
return false;
|
|
2186
2373
|
}
|
|
2187
|
-
if (item.constructor.name ===
|
|
2374
|
+
if (item.constructor.name === "Shape") {
|
|
2188
2375
|
return false;
|
|
2189
2376
|
}
|
|
2190
|
-
if (item.constructor.name ===
|
|
2377
|
+
if (item.constructor.name === "TextBox") {
|
|
2191
2378
|
return false;
|
|
2192
2379
|
}
|
|
2193
|
-
if (item.constructor.name ===
|
|
2380
|
+
if (item.constructor.name === "Field") {
|
|
2194
2381
|
return false;
|
|
2195
2382
|
}
|
|
2196
2383
|
if (item.getText) {
|
|
2197
2384
|
const text = item.getText().trim();
|
|
2198
|
-
if (text !==
|
|
2385
|
+
if (text !== "") {
|
|
2199
2386
|
return false;
|
|
2200
2387
|
}
|
|
2201
2388
|
}
|
|
2202
2389
|
}
|
|
2203
|
-
if (para.getBookmarksStart().length > 0 ||
|
|
2390
|
+
if (para.getBookmarksStart().length > 0 ||
|
|
2391
|
+
para.getBookmarksEnd().length > 0) {
|
|
2204
2392
|
return false;
|
|
2205
2393
|
}
|
|
2206
2394
|
return true;
|
|
@@ -2222,7 +2410,10 @@ class Document {
|
|
|
2222
2410
|
const content = match[1];
|
|
2223
2411
|
if (!content)
|
|
2224
2412
|
continue;
|
|
2225
|
-
const parts = content
|
|
2413
|
+
const parts = content
|
|
2414
|
+
.split(",")
|
|
2415
|
+
.map((p) => p.trim())
|
|
2416
|
+
.filter((p) => p);
|
|
2226
2417
|
if (parts.length < 2)
|
|
2227
2418
|
continue;
|
|
2228
2419
|
const styleName = parts[0];
|
|
@@ -2253,20 +2444,20 @@ class Document {
|
|
|
2253
2444
|
const levelSet = new Set(levels);
|
|
2254
2445
|
try {
|
|
2255
2446
|
const parsed = XMLParser_1.XMLParser.parseToObject(docXml, { trimValues: false });
|
|
2256
|
-
const document = parsed[
|
|
2447
|
+
const document = parsed["w:document"];
|
|
2257
2448
|
if (!document) {
|
|
2258
2449
|
return headings;
|
|
2259
2450
|
}
|
|
2260
|
-
const body = document[
|
|
2451
|
+
const body = document["w:body"];
|
|
2261
2452
|
if (!body) {
|
|
2262
2453
|
return headings;
|
|
2263
2454
|
}
|
|
2264
2455
|
const extractHeading = (para) => {
|
|
2265
|
-
const pPr = para[
|
|
2266
|
-
if (!pPr || !pPr[
|
|
2456
|
+
const pPr = para["w:pPr"];
|
|
2457
|
+
if (!pPr || !pPr["w:pStyle"]) {
|
|
2267
2458
|
return;
|
|
2268
2459
|
}
|
|
2269
|
-
const styleVal = pPr[
|
|
2460
|
+
const styleVal = pPr["w:pStyle"]["@_w:val"];
|
|
2270
2461
|
if (!styleVal) {
|
|
2271
2462
|
return;
|
|
2272
2463
|
}
|
|
@@ -2278,30 +2469,32 @@ class Document {
|
|
|
2278
2469
|
if (!levelSet.has(headingLevel)) {
|
|
2279
2470
|
return;
|
|
2280
2471
|
}
|
|
2281
|
-
let bookmark =
|
|
2282
|
-
const bookmarkStart = para[
|
|
2472
|
+
let bookmark = "";
|
|
2473
|
+
const bookmarkStart = para["w:bookmarkStart"];
|
|
2283
2474
|
if (bookmarkStart) {
|
|
2284
|
-
const bookmarkArray = Array.isArray(bookmarkStart)
|
|
2475
|
+
const bookmarkArray = Array.isArray(bookmarkStart)
|
|
2476
|
+
? bookmarkStart
|
|
2477
|
+
: [bookmarkStart];
|
|
2285
2478
|
for (const bm of bookmarkArray) {
|
|
2286
|
-
const bmName = bm[
|
|
2287
|
-
if (bmName && bmName.toLowerCase().includes(
|
|
2479
|
+
const bmName = bm["@_w:name"];
|
|
2480
|
+
if (bmName && bmName.toLowerCase().includes("_heading")) {
|
|
2288
2481
|
bookmark = bmName;
|
|
2289
2482
|
break;
|
|
2290
2483
|
}
|
|
2291
2484
|
}
|
|
2292
2485
|
}
|
|
2293
|
-
let text =
|
|
2294
|
-
const runs = para[
|
|
2486
|
+
let text = "";
|
|
2487
|
+
const runs = para["w:r"];
|
|
2295
2488
|
if (runs) {
|
|
2296
2489
|
const runArray = Array.isArray(runs) ? runs : [runs];
|
|
2297
2490
|
for (const run of runArray) {
|
|
2298
|
-
const textElement = run[
|
|
2491
|
+
const textElement = run["w:t"];
|
|
2299
2492
|
if (textElement) {
|
|
2300
|
-
if (typeof textElement ===
|
|
2493
|
+
if (typeof textElement === "string") {
|
|
2301
2494
|
text += textElement;
|
|
2302
2495
|
}
|
|
2303
|
-
else if (textElement[
|
|
2304
|
-
text += textElement[
|
|
2496
|
+
else if (textElement["#text"]) {
|
|
2497
|
+
text += textElement["#text"];
|
|
2305
2498
|
}
|
|
2306
2499
|
}
|
|
2307
2500
|
}
|
|
@@ -2316,34 +2509,36 @@ class Document {
|
|
|
2316
2509
|
headings.push({
|
|
2317
2510
|
level: headingLevel,
|
|
2318
2511
|
text: text,
|
|
2319
|
-
bookmark: bookmark
|
|
2512
|
+
bookmark: bookmark,
|
|
2320
2513
|
});
|
|
2321
2514
|
};
|
|
2322
|
-
const paragraphs = body[
|
|
2515
|
+
const paragraphs = body["w:p"];
|
|
2323
2516
|
if (paragraphs) {
|
|
2324
2517
|
const paraArray = Array.isArray(paragraphs) ? paragraphs : [paragraphs];
|
|
2325
2518
|
for (const para of paraArray) {
|
|
2326
2519
|
extractHeading(para);
|
|
2327
2520
|
}
|
|
2328
2521
|
}
|
|
2329
|
-
const tables = body[
|
|
2522
|
+
const tables = body["w:tbl"];
|
|
2330
2523
|
if (tables) {
|
|
2331
2524
|
const tableArray = Array.isArray(tables) ? tables : [tables];
|
|
2332
2525
|
for (const table of tableArray) {
|
|
2333
|
-
const rows = table[
|
|
2526
|
+
const rows = table["w:tr"];
|
|
2334
2527
|
if (!rows)
|
|
2335
2528
|
continue;
|
|
2336
2529
|
const rowArray = Array.isArray(rows) ? rows : [rows];
|
|
2337
2530
|
for (const row of rowArray) {
|
|
2338
|
-
const cells = row[
|
|
2531
|
+
const cells = row["w:tc"];
|
|
2339
2532
|
if (!cells)
|
|
2340
2533
|
continue;
|
|
2341
2534
|
const cellArray = Array.isArray(cells) ? cells : [cells];
|
|
2342
2535
|
for (const cell of cellArray) {
|
|
2343
|
-
const cellParas = cell[
|
|
2536
|
+
const cellParas = cell["w:p"];
|
|
2344
2537
|
if (!cellParas)
|
|
2345
2538
|
continue;
|
|
2346
|
-
const cellParaArray = Array.isArray(cellParas)
|
|
2539
|
+
const cellParaArray = Array.isArray(cellParas)
|
|
2540
|
+
? cellParas
|
|
2541
|
+
: [cellParas];
|
|
2347
2542
|
for (const para of cellParaArray) {
|
|
2348
2543
|
extractHeading(para);
|
|
2349
2544
|
}
|
|
@@ -2353,7 +2548,9 @@ class Document {
|
|
|
2353
2548
|
}
|
|
2354
2549
|
}
|
|
2355
2550
|
catch (error) {
|
|
2356
|
-
logger_1.defaultLogger.error(
|
|
2551
|
+
logger_1.defaultLogger.error("Error parsing document.xml for headings:", error instanceof Error
|
|
2552
|
+
? { message: error.message, stack: error.stack }
|
|
2553
|
+
: { error: String(error) });
|
|
2357
2554
|
}
|
|
2358
2555
|
return headings;
|
|
2359
2556
|
}
|
|
@@ -2375,7 +2572,7 @@ class Document {
|
|
|
2375
2572
|
headings.push({
|
|
2376
2573
|
level: headingLevel,
|
|
2377
2574
|
text: text,
|
|
2378
|
-
bookmark: bookmark.getName()
|
|
2575
|
+
bookmark: bookmark.getName(),
|
|
2379
2576
|
});
|
|
2380
2577
|
}
|
|
2381
2578
|
}
|
|
@@ -2387,93 +2584,95 @@ class Document {
|
|
|
2387
2584
|
}
|
|
2388
2585
|
generateTOCXML(headings, originalInstrText) {
|
|
2389
2586
|
const sdtId = Math.floor(Math.random() * 2000000000) - 1000000000;
|
|
2390
|
-
let tocXml =
|
|
2391
|
-
tocXml +=
|
|
2587
|
+
let tocXml = "<w:sdt>";
|
|
2588
|
+
tocXml += "<w:sdtPr>";
|
|
2392
2589
|
tocXml += `<w:id w:val="${sdtId}"/>`;
|
|
2393
|
-
tocXml +=
|
|
2590
|
+
tocXml += "<w:docPartObj>";
|
|
2394
2591
|
tocXml += '<w:docPartGallery w:val="Table of Contents"/>';
|
|
2395
2592
|
tocXml += '<w:docPartUnique w:val="1"/>';
|
|
2396
|
-
tocXml +=
|
|
2397
|
-
tocXml +=
|
|
2398
|
-
tocXml +=
|
|
2399
|
-
const minLevel = headings.length > 0
|
|
2400
|
-
|
|
2401
|
-
|
|
2402
|
-
tocXml +=
|
|
2403
|
-
|
|
2404
|
-
tocXml += '<w:spacing w:after="0" w:before="0" w:line="240" w:lineRule="auto"/>';
|
|
2593
|
+
tocXml += "</w:docPartObj>";
|
|
2594
|
+
tocXml += "</w:sdtPr>";
|
|
2595
|
+
tocXml += "<w:sdtContent>";
|
|
2596
|
+
const minLevel = headings.length > 0 ? Math.min(...headings.map((h) => h.level)) : 1;
|
|
2597
|
+
tocXml += "<w:p>";
|
|
2598
|
+
tocXml += "<w:pPr>";
|
|
2599
|
+
tocXml +=
|
|
2600
|
+
'<w:spacing w:after="0" w:before="0" w:line="240" w:lineRule="auto"/>';
|
|
2405
2601
|
if (headings.length > 0 && headings[0]) {
|
|
2406
2602
|
const firstIndent = (headings[0].level - minLevel) * 360;
|
|
2407
2603
|
if (firstIndent > 0) {
|
|
2408
2604
|
tocXml += `<w:ind w:left="${firstIndent}"/>`;
|
|
2409
2605
|
}
|
|
2410
2606
|
}
|
|
2411
|
-
tocXml +=
|
|
2607
|
+
tocXml += "</w:pPr>";
|
|
2412
2608
|
tocXml += '<w:r><w:fldChar w:fldCharType="begin"/></w:r>';
|
|
2413
|
-
tocXml +=
|
|
2609
|
+
tocXml += "<w:r>";
|
|
2414
2610
|
tocXml += `<w:instrText xml:space="preserve">${this.escapeXml(originalInstrText)}</w:instrText>`;
|
|
2415
|
-
tocXml +=
|
|
2611
|
+
tocXml += "</w:r>";
|
|
2416
2612
|
tocXml += '<w:r><w:fldChar w:fldCharType="separate"/></w:r>';
|
|
2417
2613
|
if (headings.length > 0 && headings[0]) {
|
|
2418
2614
|
tocXml += this.buildTOCEntryXML(headings[0]);
|
|
2419
2615
|
}
|
|
2420
|
-
tocXml +=
|
|
2616
|
+
tocXml += "</w:p>";
|
|
2421
2617
|
for (let i = 1; i < headings.length; i++) {
|
|
2422
2618
|
const heading = headings[i];
|
|
2423
2619
|
if (!heading)
|
|
2424
2620
|
continue;
|
|
2425
|
-
tocXml +=
|
|
2426
|
-
tocXml +=
|
|
2427
|
-
tocXml +=
|
|
2621
|
+
tocXml += "<w:p>";
|
|
2622
|
+
tocXml += "<w:pPr>";
|
|
2623
|
+
tocXml +=
|
|
2624
|
+
'<w:spacing w:after="0" w:before="0" w:line="240" w:lineRule="auto"/>';
|
|
2428
2625
|
const indent = (heading.level - minLevel) * 360;
|
|
2429
2626
|
if (indent > 0) {
|
|
2430
2627
|
tocXml += `<w:ind w:left="${indent}"/>`;
|
|
2431
2628
|
}
|
|
2432
|
-
tocXml +=
|
|
2629
|
+
tocXml += "</w:pPr>";
|
|
2433
2630
|
tocXml += this.buildTOCEntryXML(heading);
|
|
2434
|
-
tocXml +=
|
|
2631
|
+
tocXml += "</w:p>";
|
|
2435
2632
|
}
|
|
2436
|
-
tocXml +=
|
|
2437
|
-
tocXml +=
|
|
2438
|
-
tocXml +=
|
|
2439
|
-
|
|
2633
|
+
tocXml += "<w:p>";
|
|
2634
|
+
tocXml += "<w:pPr>";
|
|
2635
|
+
tocXml +=
|
|
2636
|
+
'<w:spacing w:after="0" w:before="0" w:line="240" w:lineRule="auto"/>';
|
|
2637
|
+
tocXml += "</w:pPr>";
|
|
2440
2638
|
tocXml += '<w:r><w:fldChar w:fldCharType="end"/></w:r>';
|
|
2441
|
-
tocXml +=
|
|
2442
|
-
tocXml +=
|
|
2443
|
-
tocXml +=
|
|
2639
|
+
tocXml += "</w:p>";
|
|
2640
|
+
tocXml += "</w:sdtContent>";
|
|
2641
|
+
tocXml += "</w:sdt>";
|
|
2444
2642
|
return tocXml;
|
|
2445
2643
|
}
|
|
2446
2644
|
buildTOCEntryXML(heading) {
|
|
2447
2645
|
const escapedText = this.escapeXml(heading.text);
|
|
2448
|
-
let xml =
|
|
2646
|
+
let xml = "";
|
|
2449
2647
|
xml += `<w:hyperlink w:anchor="${this.escapeXml(heading.bookmark)}">`;
|
|
2450
|
-
xml +=
|
|
2451
|
-
xml +=
|
|
2452
|
-
xml +=
|
|
2648
|
+
xml += "<w:r>";
|
|
2649
|
+
xml += "<w:rPr>";
|
|
2650
|
+
xml +=
|
|
2651
|
+
'<w:rFonts w:ascii="Verdana" w:hAnsi="Verdana" w:cs="Verdana" w:eastAsia="Verdana"/>';
|
|
2453
2652
|
xml += '<w:color w:val="0000FF"/>';
|
|
2454
2653
|
xml += '<w:sz w:val="24"/>';
|
|
2455
2654
|
xml += '<w:szCs w:val="24"/>';
|
|
2456
2655
|
xml += '<w:u w:val="single"/>';
|
|
2457
|
-
xml +=
|
|
2656
|
+
xml += "</w:rPr>";
|
|
2458
2657
|
xml += `<w:t xml:space="preserve">${escapedText}</w:t>`;
|
|
2459
|
-
xml +=
|
|
2460
|
-
xml +=
|
|
2658
|
+
xml += "</w:r>";
|
|
2659
|
+
xml += "</w:hyperlink>";
|
|
2461
2660
|
return xml;
|
|
2462
2661
|
}
|
|
2463
2662
|
escapeXml(text) {
|
|
2464
2663
|
return text
|
|
2465
|
-
.replace(/&/g,
|
|
2466
|
-
.replace(/</g,
|
|
2467
|
-
.replace(/>/g,
|
|
2468
|
-
.replace(/"/g,
|
|
2469
|
-
.replace(/'/g,
|
|
2664
|
+
.replace(/&/g, "&")
|
|
2665
|
+
.replace(/</g, "<")
|
|
2666
|
+
.replace(/>/g, ">")
|
|
2667
|
+
.replace(/"/g, """)
|
|
2668
|
+
.replace(/'/g, "'");
|
|
2470
2669
|
}
|
|
2471
2670
|
async replaceTableOfContents(filePath) {
|
|
2472
2671
|
const handler = new ZipHandler_1.ZipHandler();
|
|
2473
2672
|
await handler.load(filePath);
|
|
2474
|
-
const docXml = handler.getFileAsString(
|
|
2673
|
+
const docXml = handler.getFileAsString("word/document.xml");
|
|
2475
2674
|
if (!docXml) {
|
|
2476
|
-
throw new Error(
|
|
2675
|
+
throw new Error("word/document.xml not found in document");
|
|
2477
2676
|
}
|
|
2478
2677
|
const tocRegex = /<w:sdt>[\s\S]*?<w:docPartGallery w:val="Table of Contents"[\s\S]*?<\/w:sdt>/g;
|
|
2479
2678
|
const tocMatches = Array.from(docXml.matchAll(tocRegex));
|
|
@@ -2491,9 +2690,9 @@ class Document {
|
|
|
2491
2690
|
}
|
|
2492
2691
|
let fieldInstruction = instrMatch[1];
|
|
2493
2692
|
fieldInstruction = fieldInstruction
|
|
2494
|
-
.replace(/&/g,
|
|
2495
|
-
.replace(/</g,
|
|
2496
|
-
.replace(/>/g,
|
|
2693
|
+
.replace(/&/g, "&")
|
|
2694
|
+
.replace(/</g, "<")
|
|
2695
|
+
.replace(/>/g, ">")
|
|
2497
2696
|
.replace(/"/g, '"')
|
|
2498
2697
|
.replace(/'/g, "'");
|
|
2499
2698
|
const levels = this.parseTOCFieldInstruction(fieldInstruction);
|
|
@@ -2511,7 +2710,7 @@ class Document {
|
|
|
2511
2710
|
}
|
|
2512
2711
|
}
|
|
2513
2712
|
if (replacedCount > 0) {
|
|
2514
|
-
handler.updateFile(
|
|
2713
|
+
handler.updateFile("word/document.xml", modifiedXml);
|
|
2515
2714
|
await handler.save(filePath);
|
|
2516
2715
|
}
|
|
2517
2716
|
return replacedCount;
|
|
@@ -2743,7 +2942,7 @@ class Document {
|
|
|
2743
2942
|
bookmark = new Bookmark_1.Bookmark({
|
|
2744
2943
|
id: 0,
|
|
2745
2944
|
name: BOOKMARK_NAME,
|
|
2746
|
-
skipNormalization: true
|
|
2945
|
+
skipNormalization: true,
|
|
2747
2946
|
});
|
|
2748
2947
|
this.bookmarkManager.register(bookmark);
|
|
2749
2948
|
const paragraphs = this.getParagraphs();
|
|
@@ -2761,7 +2960,7 @@ class Document {
|
|
|
2761
2960
|
anchor: BOOKMARK_NAME,
|
|
2762
2961
|
hyperlink: (text, formatting) => {
|
|
2763
2962
|
return Hyperlink_1.Hyperlink.createInternal(BOOKMARK_NAME, text, formatting);
|
|
2764
|
-
}
|
|
2963
|
+
},
|
|
2765
2964
|
};
|
|
2766
2965
|
}
|
|
2767
2966
|
getRevisionManager() {
|
|
@@ -2814,13 +3013,15 @@ class Document {
|
|
|
2814
3013
|
this.trackFormatting = options.trackFormatting;
|
|
2815
3014
|
}
|
|
2816
3015
|
if (options.showInsertionsAndDeletions !== undefined) {
|
|
2817
|
-
this.revisionViewSettings.showInsertionsAndDeletions =
|
|
3016
|
+
this.revisionViewSettings.showInsertionsAndDeletions =
|
|
3017
|
+
options.showInsertionsAndDeletions;
|
|
2818
3018
|
}
|
|
2819
3019
|
if (options.showFormatting !== undefined) {
|
|
2820
3020
|
this.revisionViewSettings.showFormatting = options.showFormatting;
|
|
2821
3021
|
}
|
|
2822
3022
|
if (options.showInkAnnotations !== undefined) {
|
|
2823
|
-
this.revisionViewSettings.showInkAnnotations =
|
|
3023
|
+
this.revisionViewSettings.showInkAnnotations =
|
|
3024
|
+
options.showInkAnnotations;
|
|
2824
3025
|
}
|
|
2825
3026
|
}
|
|
2826
3027
|
return this;
|
|
@@ -2840,7 +3041,7 @@ class Document {
|
|
|
2840
3041
|
}
|
|
2841
3042
|
setRsidRoot(rsidRoot) {
|
|
2842
3043
|
if (!/^[0-9A-Fa-f]{8}$/.test(rsidRoot)) {
|
|
2843
|
-
throw new Error(
|
|
3044
|
+
throw new Error("RSID must be an 8-character hexadecimal value");
|
|
2844
3045
|
}
|
|
2845
3046
|
this.rsidRoot = rsidRoot.toUpperCase();
|
|
2846
3047
|
this.rsids.add(this.rsidRoot);
|
|
@@ -2848,13 +3049,16 @@ class Document {
|
|
|
2848
3049
|
}
|
|
2849
3050
|
addRsid(rsid) {
|
|
2850
3051
|
if (!/^[0-9A-Fa-f]{8}$/.test(rsid)) {
|
|
2851
|
-
throw new Error(
|
|
3052
|
+
throw new Error("RSID must be an 8-character hexadecimal value");
|
|
2852
3053
|
}
|
|
2853
3054
|
this.rsids.add(rsid.toUpperCase());
|
|
2854
3055
|
return this;
|
|
2855
3056
|
}
|
|
2856
3057
|
generateRsid() {
|
|
2857
|
-
const rsid = Math.floor(Math.random() *
|
|
3058
|
+
const rsid = Math.floor(Math.random() * 0xffffffff)
|
|
3059
|
+
.toString(16)
|
|
3060
|
+
.toUpperCase()
|
|
3061
|
+
.padStart(8, "0");
|
|
2858
3062
|
this.rsids.add(rsid);
|
|
2859
3063
|
return rsid;
|
|
2860
3064
|
}
|
|
@@ -2875,9 +3079,11 @@ class Document {
|
|
|
2875
3079
|
cryptSpinCount: protection.cryptSpinCount,
|
|
2876
3080
|
};
|
|
2877
3081
|
if (protection.password) {
|
|
2878
|
-
const crypto = require(
|
|
2879
|
-
const salt = crypto.randomBytes(16).toString(
|
|
2880
|
-
const hash = crypto
|
|
3082
|
+
const crypto = require("crypto");
|
|
3083
|
+
const salt = crypto.randomBytes(16).toString("base64");
|
|
3084
|
+
const hash = crypto
|
|
3085
|
+
.pbkdf2Sync(protection.password, salt, protection.cryptSpinCount || 100000, 32, "sha512")
|
|
3086
|
+
.toString("base64");
|
|
2881
3087
|
this.documentProtection.hash = hash;
|
|
2882
3088
|
this.documentProtection.salt = salt;
|
|
2883
3089
|
}
|
|
@@ -2914,7 +3120,9 @@ class Document {
|
|
|
2914
3120
|
return this.revisionManager.register(revision);
|
|
2915
3121
|
}
|
|
2916
3122
|
trackMove(author, content, date) {
|
|
2917
|
-
const moveId = `move${Date.now()}_${Math.random()
|
|
3123
|
+
const moveId = `move${Date.now()}_${Math.random()
|
|
3124
|
+
.toString(36)
|
|
3125
|
+
.substr(2, 9)}`;
|
|
2918
3126
|
const moveName = `move${Date.now()}`;
|
|
2919
3127
|
const rangeIdStart = this.revisionManager.getStats().nextId;
|
|
2920
3128
|
const moveFromRangeStart = RangeMarker_1.RangeMarker.createMoveFromStart(rangeIdStart, moveName, author, date);
|
|
@@ -3065,7 +3273,7 @@ class Document {
|
|
|
3065
3273
|
}
|
|
3066
3274
|
let content = file.content;
|
|
3067
3275
|
if (!file.isBinary && Buffer.isBuffer(file.content)) {
|
|
3068
|
-
content = file.content.toString(
|
|
3276
|
+
content = file.content.toString("utf-8");
|
|
3069
3277
|
}
|
|
3070
3278
|
return {
|
|
3071
3279
|
name: partName,
|
|
@@ -3423,20 +3631,20 @@ class Document {
|
|
|
3423
3631
|
return replacementCount;
|
|
3424
3632
|
}
|
|
3425
3633
|
findAndReplaceAll(pattern, replacement, options) {
|
|
3426
|
-
const { caseSensitive = false, wholeWord = false, trackChanges = false, author =
|
|
3634
|
+
const { caseSensitive = false, wholeWord = false, trackChanges = false, author = "Unknown", } = options || {};
|
|
3427
3635
|
let count = 0;
|
|
3428
3636
|
const revisions = [];
|
|
3429
3637
|
let regex;
|
|
3430
|
-
if (typeof pattern ===
|
|
3431
|
-
const escaped = pattern.replace(/[.*+?^${}()|[\]\\]/g,
|
|
3638
|
+
if (typeof pattern === "string") {
|
|
3639
|
+
const escaped = pattern.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
3432
3640
|
const boundaryPattern = wholeWord ? `\\b${escaped}\\b` : escaped;
|
|
3433
|
-
const flags = caseSensitive ?
|
|
3641
|
+
const flags = caseSensitive ? "g" : "gi";
|
|
3434
3642
|
regex = new RegExp(boundaryPattern, flags);
|
|
3435
3643
|
}
|
|
3436
3644
|
else {
|
|
3437
|
-
const flags = pattern.flags.includes(
|
|
3645
|
+
const flags = pattern.flags.includes("g")
|
|
3438
3646
|
? pattern.flags
|
|
3439
|
-
: pattern.flags +
|
|
3647
|
+
: pattern.flags + "g";
|
|
3440
3648
|
regex = new RegExp(pattern.source, flags);
|
|
3441
3649
|
}
|
|
3442
3650
|
const runs = this.getAllRuns();
|
|
@@ -3568,14 +3776,14 @@ class Document {
|
|
|
3568
3776
|
}
|
|
3569
3777
|
validateParagraph(paragraph) {
|
|
3570
3778
|
if (!(paragraph instanceof Paragraph_1.Paragraph)) {
|
|
3571
|
-
throw new Error(
|
|
3779
|
+
throw new Error("insertParagraphAt: parameter must be a Paragraph instance");
|
|
3572
3780
|
}
|
|
3573
3781
|
const paraId = paragraph.getFormatting().paraId;
|
|
3574
3782
|
if (paraId) {
|
|
3575
3783
|
const existingIds = this.bodyElements
|
|
3576
3784
|
.filter((el) => el instanceof Paragraph_1.Paragraph)
|
|
3577
|
-
.map(p => p.getFormatting().paraId)
|
|
3578
|
-
.filter(id => id === paraId);
|
|
3785
|
+
.map((p) => p.getFormatting().paraId)
|
|
3786
|
+
.filter((id) => id === paraId);
|
|
3579
3787
|
if (existingIds.length > 0) {
|
|
3580
3788
|
throw new Error(`Duplicate paragraph ID detected: ${paraId}. Each paragraph must have a unique ID.`);
|
|
3581
3789
|
}
|
|
@@ -3591,15 +3799,15 @@ class Document {
|
|
|
3591
3799
|
}
|
|
3592
3800
|
validateTable(table) {
|
|
3593
3801
|
if (!(table instanceof Table_1.Table)) {
|
|
3594
|
-
throw new Error(
|
|
3802
|
+
throw new Error("insertTableAt: parameter must be a Table instance");
|
|
3595
3803
|
}
|
|
3596
3804
|
const rows = table.getRows();
|
|
3597
3805
|
if (rows.length === 0) {
|
|
3598
|
-
throw new Error(
|
|
3806
|
+
throw new Error("insertTableAt: table must have at least one row");
|
|
3599
3807
|
}
|
|
3600
3808
|
const firstRow = rows[0];
|
|
3601
3809
|
if (firstRow && firstRow.getCells().length === 0) {
|
|
3602
|
-
throw new Error(
|
|
3810
|
+
throw new Error("insertTableAt: table rows must have at least one cell");
|
|
3603
3811
|
}
|
|
3604
3812
|
const tableStyle = table.getFormatting().style;
|
|
3605
3813
|
if (tableStyle && !this.stylesManager.hasStyle(tableStyle)) {
|
|
@@ -3608,12 +3816,21 @@ class Document {
|
|
|
3608
3816
|
}
|
|
3609
3817
|
validateToc(toc) {
|
|
3610
3818
|
if (!(toc instanceof TableOfContentsElement_1.TableOfContentsElement)) {
|
|
3611
|
-
throw new Error(
|
|
3612
|
-
}
|
|
3613
|
-
const hasHeadings = [
|
|
3614
|
-
|
|
3819
|
+
throw new Error("insertTocAt: parameter must be a TableOfContentsElement instance");
|
|
3820
|
+
}
|
|
3821
|
+
const hasHeadings = [
|
|
3822
|
+
"Heading1",
|
|
3823
|
+
"Heading2",
|
|
3824
|
+
"Heading3",
|
|
3825
|
+
"Heading4",
|
|
3826
|
+
"Heading5",
|
|
3827
|
+
"Heading6",
|
|
3828
|
+
"Heading7",
|
|
3829
|
+
"Heading8",
|
|
3830
|
+
"Heading9",
|
|
3831
|
+
].some((style) => this.stylesManager.hasStyle(style));
|
|
3615
3832
|
if (!hasHeadings) {
|
|
3616
|
-
logger_1.defaultLogger.warn(
|
|
3833
|
+
logger_1.defaultLogger.warn("No heading styles found in document. Table of Contents may not display entries correctly.");
|
|
3617
3834
|
}
|
|
3618
3835
|
}
|
|
3619
3836
|
normalizeIndex(index) {
|
|
@@ -3835,7 +4052,7 @@ class Document {
|
|
|
3835
4052
|
const toRemove = [];
|
|
3836
4053
|
this.bodyElements.forEach((element, index) => {
|
|
3837
4054
|
if (element instanceof Paragraph_1.Paragraph) {
|
|
3838
|
-
const isEmpty = element.getText().trim() ===
|
|
4055
|
+
const isEmpty = element.getText().trim() === "";
|
|
3839
4056
|
if (isEmpty && lastWasEmpty) {
|
|
3840
4057
|
toRemove.push(index);
|
|
3841
4058
|
}
|
|
@@ -3862,7 +4079,7 @@ class Document {
|
|
|
3862
4079
|
}
|
|
3863
4080
|
}
|
|
3864
4081
|
if (standardLineSpacing !== undefined) {
|
|
3865
|
-
para.setLineSpacing(standardLineSpacing,
|
|
4082
|
+
para.setLineSpacing(standardLineSpacing, "auto");
|
|
3866
4083
|
normalized++;
|
|
3867
4084
|
}
|
|
3868
4085
|
if (removeTrailingSpaces) {
|
|
@@ -3896,7 +4113,7 @@ class Document {
|
|
|
3896
4113
|
}
|
|
3897
4114
|
}
|
|
3898
4115
|
if (standardLineSpacing !== undefined) {
|
|
3899
|
-
para.setLineSpacing(standardLineSpacing,
|
|
4116
|
+
para.setLineSpacing(standardLineSpacing, "auto");
|
|
3900
4117
|
normalized++;
|
|
3901
4118
|
}
|
|
3902
4119
|
if (removeTrailingSpaces) {
|