docxmlater 3.2.0 → 4.0.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.
Files changed (35) hide show
  1. package/README.md +85 -54
  2. package/dist/core/Document.d.ts +4 -7
  3. package/dist/core/Document.d.ts.map +1 -1
  4. package/dist/core/Document.js +21 -111
  5. package/dist/core/Document.js.map +1 -1
  6. package/dist/core/DocumentParser.d.ts.map +1 -1
  7. package/dist/core/DocumentParser.js +28 -2
  8. package/dist/core/DocumentParser.js.map +1 -1
  9. package/dist/elements/Paragraph.d.ts +31 -27
  10. package/dist/elements/Paragraph.d.ts.map +1 -1
  11. package/dist/elements/Paragraph.js +183 -127
  12. package/dist/elements/Paragraph.js.map +1 -1
  13. package/dist/elements/Run.d.ts +14 -14
  14. package/dist/elements/Run.d.ts.map +1 -1
  15. package/dist/elements/Run.js +137 -133
  16. package/dist/elements/Run.js.map +1 -1
  17. package/dist/elements/StructuredDocumentTag.d.ts +10 -7
  18. package/dist/elements/StructuredDocumentTag.d.ts.map +1 -1
  19. package/dist/elements/StructuredDocumentTag.js +87 -70
  20. package/dist/elements/StructuredDocumentTag.js.map +1 -1
  21. package/dist/elements/Table.d.ts +13 -13
  22. package/dist/elements/Table.d.ts.map +1 -1
  23. package/dist/elements/Table.js +61 -51
  24. package/dist/elements/Table.js.map +1 -1
  25. package/dist/elements/TableCell.d.ts +9 -6
  26. package/dist/elements/TableCell.d.ts.map +1 -1
  27. package/dist/elements/TableCell.js +70 -32
  28. package/dist/elements/TableCell.js.map +1 -1
  29. package/dist/formatting/StylesManager.d.ts +21 -1
  30. package/dist/formatting/StylesManager.d.ts.map +1 -1
  31. package/dist/formatting/StylesManager.js +187 -74
  32. package/dist/formatting/StylesManager.js.map +1 -1
  33. package/dist/types/styleConfig.d.ts +4 -3
  34. package/dist/types/styleConfig.d.ts.map +1 -1
  35. package/package.json +3 -2
@@ -418,6 +418,7 @@ class Document {
418
418
  images: sizeInfo.images,
419
419
  });
420
420
  }
421
+ this.clearAllPreserveFlags();
421
422
  this.processHyperlinks();
422
423
  this.updateDocumentXml();
423
424
  this.updateStylesXml();
@@ -466,6 +467,7 @@ class Document {
466
467
  images: sizeInfo.images,
467
468
  });
468
469
  }
470
+ this.clearAllPreserveFlags();
469
471
  this.processHyperlinks();
470
472
  this.updateDocumentXml();
471
473
  this.updateStylesXml();
@@ -516,6 +518,9 @@ class Document {
516
518
  getStylesManager() {
517
519
  return this.stylesManager;
518
520
  }
521
+ styles() {
522
+ return this.stylesManager;
523
+ }
519
524
  addStyle(style) {
520
525
  this.stylesManager.addStyle(style);
521
526
  this.updateStylesXml();
@@ -1225,113 +1230,6 @@ class Document {
1225
1230
  this.bodyElements.splice(paraIndex, 0, table);
1226
1231
  return table;
1227
1232
  }
1228
- applyCustomStylesToDocument() {
1229
- const counts = { heading1: 0, heading2: 0, normal: 0 };
1230
- const header1Style = Style_1.Style.create({
1231
- styleId: "CustomHeader1",
1232
- name: "Custom Header 1",
1233
- type: "paragraph",
1234
- basedOn: "Normal",
1235
- runFormatting: {
1236
- font: "Verdana",
1237
- size: 18,
1238
- bold: true,
1239
- color: "000000",
1240
- },
1241
- paragraphFormatting: {
1242
- alignment: "left",
1243
- spacing: {
1244
- before: 0,
1245
- after: 240,
1246
- },
1247
- },
1248
- });
1249
- const header2Style = Style_1.Style.create({
1250
- styleId: "CustomHeader2",
1251
- name: "Custom Header 2",
1252
- type: "paragraph",
1253
- basedOn: "Normal",
1254
- runFormatting: {
1255
- font: "Verdana",
1256
- size: 14,
1257
- bold: true,
1258
- color: "000000",
1259
- },
1260
- paragraphFormatting: {
1261
- alignment: "left",
1262
- spacing: {
1263
- before: 120,
1264
- after: 120,
1265
- },
1266
- },
1267
- });
1268
- const normalStyle = Style_1.Style.create({
1269
- styleId: "CustomNormal",
1270
- name: "Custom Normal",
1271
- type: "paragraph",
1272
- basedOn: "Normal",
1273
- runFormatting: {
1274
- font: "Verdana",
1275
- size: 12,
1276
- color: "000000",
1277
- },
1278
- paragraphFormatting: {
1279
- alignment: "left",
1280
- spacing: {
1281
- before: 60,
1282
- after: 60,
1283
- },
1284
- },
1285
- });
1286
- this.addStyle(header1Style);
1287
- this.addStyle(header2Style);
1288
- this.addStyle(normalStyle);
1289
- const allParagraphs = this.getAllParagraphs();
1290
- for (const para of allParagraphs) {
1291
- const currentStyle = para.getStyle();
1292
- if (currentStyle === "Heading1" || currentStyle === "Heading 1") {
1293
- para.setStyle("CustomHeader1");
1294
- counts.heading1++;
1295
- }
1296
- else if (currentStyle === "Heading2" || currentStyle === "Heading 2") {
1297
- para.setStyle("CustomHeader2");
1298
- const { inTable, cell } = this.isParagraphInTable(para);
1299
- if (inTable && cell) {
1300
- cell.setShading({ fill: "BFBFBF" });
1301
- cell.setMargins({ top: 0, bottom: 0, left: 101, right: 101 });
1302
- const table = this.getAllTables().find((t) => {
1303
- for (const row of t.getRows()) {
1304
- for (const c of row.getCells()) {
1305
- if (c === cell)
1306
- return true;
1307
- }
1308
- }
1309
- return false;
1310
- });
1311
- if (table) {
1312
- table.setWidth(5000);
1313
- table.setWidthType("pct");
1314
- }
1315
- }
1316
- else {
1317
- this.wrapParagraphInTable(para, {
1318
- shading: "BFBFBF",
1319
- marginTop: 0,
1320
- marginBottom: 0,
1321
- marginLeft: 101,
1322
- marginRight: 101,
1323
- tableWidthPercent: 5000,
1324
- });
1325
- }
1326
- counts.heading2++;
1327
- }
1328
- else if (currentStyle === "Normal" || currentStyle === undefined) {
1329
- para.setStyle("CustomNormal");
1330
- counts.normal++;
1331
- }
1332
- }
1333
- return counts;
1334
- }
1335
1233
  static DEFAULT_HEADING1_CONFIG = {
1336
1234
  run: {
1337
1235
  font: "Verdana",
@@ -1400,7 +1298,7 @@ class Document {
1400
1298
  contextualSpacing: true,
1401
1299
  },
1402
1300
  };
1403
- applyCustomFormattingToExistingStyles(options) {
1301
+ applyStyles(options) {
1404
1302
  const results = {
1405
1303
  heading1: false,
1406
1304
  heading2: false,
@@ -1871,7 +1769,7 @@ class Document {
1871
1769
  break;
1872
1770
  }
1873
1771
  }
1874
- return this.applyCustomFormattingToExistingStyles(options);
1772
+ return this.applyStyles(options);
1875
1773
  }
1876
1774
  removeExtraBlankParagraphs(options) {
1877
1775
  const keepOne = options?.keepOne ?? true;
@@ -2006,6 +1904,7 @@ class Document {
2006
1904
  const nextElement = this.bodyElements[tableIndex + 1];
2007
1905
  if (nextElement instanceof Paragraph_1.Paragraph) {
2008
1906
  if (this.isParagraphBlank(nextElement)) {
1907
+ nextElement.setStyle(style);
2009
1908
  if (markAsPreserved && !nextElement.isPreserved()) {
2010
1909
  nextElement.setPreserved(true);
2011
1910
  existingLinesMarked++;
@@ -2067,6 +1966,7 @@ class Document {
2067
1966
  const nextElement = this.bodyElements[tableIndex + 1];
2068
1967
  if (nextElement instanceof Paragraph_1.Paragraph) {
2069
1968
  if (this.isParagraphBlank(nextElement)) {
1969
+ nextElement.setStyle(style);
2070
1970
  if (markAsPreserved && !nextElement.isPreserved()) {
2071
1971
  nextElement.setPreserved(true);
2072
1972
  existingLinesMarked++;
@@ -2424,11 +2324,21 @@ class Document {
2424
2324
  }
2425
2325
  return true;
2426
2326
  }
2327
+ clearAllPreserveFlags() {
2328
+ let cleared = 0;
2329
+ for (const para of this.getAllParagraphs()) {
2330
+ if (para.isPreserved()) {
2331
+ para.setPreserved(false);
2332
+ cleared++;
2333
+ }
2334
+ }
2335
+ return cleared;
2336
+ }
2427
2337
  parseTOCFieldInstruction(instrText) {
2428
2338
  const levels = new Set();
2429
2339
  let hasOutlineSwitch = false;
2430
2340
  let hasTableSwitch = false;
2431
- const normalizedText = instrText.replace(/"/g, '"');
2341
+ const normalizedText = instrText.trim().replace(/"/g, '"');
2432
2342
  const outlineMatch = normalizedText.match(/\\o\s+"(\d+)-(\d+)"/);
2433
2343
  if (outlineMatch?.[1] && outlineMatch?.[2]) {
2434
2344
  hasOutlineSwitch = true;
@@ -2439,7 +2349,7 @@ class Document {
2439
2349
  levels.add(i);
2440
2350
  }
2441
2351
  }
2442
- if (/\bu\b/.test(normalizedText)) {
2352
+ if (/\\u(?:\s|\\|$)/.test(normalizedText)) {
2443
2353
  const hasTSwitch = /\\t\s+"/.test(normalizedText);
2444
2354
  if (!hasOutlineSwitch && !hasTSwitch) {
2445
2355
  for (let i = 1; i <= 9; i++)