@elisra-devops/docgen-skins 0.10.7 → 0.10.9
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/bin/models/json/paragraph/JSONRichTextParagraph.d.ts +1 -0
- package/bin/models/json/paragraph/JSONRichTextParagraph.js +99 -31
- package/bin/models/json/paragraph/JSONRichTextParagraph.js.map +1 -1
- package/bin/models/json/wordJsonModels.d.ts +1 -0
- package/package.json +1 -1
- package/src/models/json/paragraph/JSONRichTextParagraph.ts +103 -32
- package/src/models/json/wordJsonModels.ts +1 -0
|
@@ -6,7 +6,19 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
const logger_1 = __importDefault(require("../../../services/logger"));
|
|
7
7
|
class JSONRichTextParagraph {
|
|
8
8
|
constructor(field, paragraphStyles, wiId, headingLevel, skipImageFormat = false) {
|
|
9
|
-
|
|
9
|
+
if (!field || !field.richTextNodes) {
|
|
10
|
+
throw new Error('Invalid field or missing richTextNodes');
|
|
11
|
+
}
|
|
12
|
+
this.paragraphStyles = paragraphStyles || {
|
|
13
|
+
isBold: false,
|
|
14
|
+
IsItalic: false,
|
|
15
|
+
IsUnderline: false,
|
|
16
|
+
Size: 12,
|
|
17
|
+
Uri: null,
|
|
18
|
+
Font: 'Arial',
|
|
19
|
+
InsertLineBreak: false,
|
|
20
|
+
InsertSpace: false,
|
|
21
|
+
};
|
|
10
22
|
this.wiId = wiId;
|
|
11
23
|
this.paragraphTemplate = this.buildDocFromNodes(field.richTextNodes);
|
|
12
24
|
} //constructor
|
|
@@ -29,8 +41,11 @@ class JSONRichTextParagraph {
|
|
|
29
41
|
* - Default: Returns null for unrecognized node types.
|
|
30
42
|
*/
|
|
31
43
|
parseNode(node) {
|
|
32
|
-
if (!node
|
|
33
|
-
|
|
44
|
+
if (!node)
|
|
45
|
+
return null;
|
|
46
|
+
// Ensure node has valid type
|
|
47
|
+
if (typeof node.type !== 'string') {
|
|
48
|
+
logger_1.default.warn(`Invalid node type encountered: ${JSON.stringify(node)}`);
|
|
34
49
|
return null;
|
|
35
50
|
}
|
|
36
51
|
switch (node.type) {
|
|
@@ -67,7 +82,27 @@ class JSONRichTextParagraph {
|
|
|
67
82
|
* @returns A Paragraph object containing the parsed runs.
|
|
68
83
|
*/
|
|
69
84
|
parseParagraphNode(node) {
|
|
85
|
+
if (!node || !Array.isArray(node.children)) {
|
|
86
|
+
return { type: 'paragraph', runs: [] };
|
|
87
|
+
}
|
|
70
88
|
const runs = this.gatherRuns(node.children);
|
|
89
|
+
// Ensure at least one run exists
|
|
90
|
+
if (runs.length === 0) {
|
|
91
|
+
runs.push({
|
|
92
|
+
type: 'text',
|
|
93
|
+
value: '',
|
|
94
|
+
textStyling: {
|
|
95
|
+
Bold: this.paragraphStyles.isBold,
|
|
96
|
+
Italic: this.paragraphStyles.IsItalic,
|
|
97
|
+
Size: this.paragraphStyles.Size,
|
|
98
|
+
Font: this.paragraphStyles.Font,
|
|
99
|
+
Uri: this.paragraphStyles.Uri,
|
|
100
|
+
InsertLineBreak: this.paragraphStyles.InsertLineBreak,
|
|
101
|
+
InsertSpace: this.paragraphStyles.InsertSpace,
|
|
102
|
+
Underline: this.paragraphStyles.IsUnderline,
|
|
103
|
+
},
|
|
104
|
+
});
|
|
105
|
+
}
|
|
71
106
|
return { type: 'paragraph', runs };
|
|
72
107
|
}
|
|
73
108
|
/**
|
|
@@ -85,27 +120,34 @@ class JSONRichTextParagraph {
|
|
|
85
120
|
* @returns An array of `Run` objects representing the gathered runs.
|
|
86
121
|
*/
|
|
87
122
|
gatherRuns(children) {
|
|
123
|
+
if (!Array.isArray(children))
|
|
124
|
+
return [];
|
|
88
125
|
const runs = [];
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
for (const child of children || []) {
|
|
94
|
-
if (child.type === 'text' || child.type === 'image' || child.type === 'break') {
|
|
95
|
-
runs.push(this.nodeToRun(child));
|
|
96
|
-
}
|
|
97
|
-
else if (child.type === 'paragraph') {
|
|
98
|
-
// Recursively gather runs from child paragraphs
|
|
99
|
-
runs.push(...this.gatherRuns(child.children));
|
|
126
|
+
for (const child of children) {
|
|
127
|
+
if (!child || typeof child.type !== 'string') {
|
|
128
|
+
logger_1.default.warn('Invalid child node:', child);
|
|
129
|
+
continue;
|
|
100
130
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
131
|
+
try {
|
|
132
|
+
if (child.type === 'text' || child.type === 'image' || child.type === 'break') {
|
|
133
|
+
runs.push(this.nodeToRun(child));
|
|
134
|
+
}
|
|
135
|
+
else if (child.type === 'paragraph') {
|
|
136
|
+
// Recursively gather runs from child paragraphs
|
|
137
|
+
runs.push(...this.gatherRuns(child.children));
|
|
138
|
+
}
|
|
139
|
+
else if (child.type === 'table' || child.type === 'list') {
|
|
140
|
+
// Not supported in this context, so we'll just add a placeholder
|
|
141
|
+
runs.push({ type: 'other', value: `[Embedded ${child.type}]` });
|
|
142
|
+
}
|
|
143
|
+
else if (child.type === 'other') {
|
|
144
|
+
// Recursively gather runs from inside <span> or <b>, etc.
|
|
145
|
+
runs.push(...this.gatherRuns(child.children));
|
|
146
|
+
}
|
|
104
147
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
runs.push(...this.gatherRuns(child.children));
|
|
148
|
+
catch (error) {
|
|
149
|
+
logger_1.default.error('Error gathering runs:', error);
|
|
150
|
+
continue;
|
|
109
151
|
}
|
|
110
152
|
}
|
|
111
153
|
return runs;
|
|
@@ -133,7 +175,17 @@ class JSONRichTextParagraph {
|
|
|
133
175
|
* - InsertSpace: boolean
|
|
134
176
|
*/
|
|
135
177
|
nodeToRun(node) {
|
|
136
|
-
|
|
178
|
+
var _a, _b, _c, _d;
|
|
179
|
+
const textStyling = {
|
|
180
|
+
Bold: ((_a = node.textStyling) === null || _a === void 0 ? void 0 : _a.Bold) || this.paragraphStyles.isBold,
|
|
181
|
+
Italic: ((_b = node.textStyling) === null || _b === void 0 ? void 0 : _b.Italic) || this.paragraphStyles.IsItalic,
|
|
182
|
+
Underline: ((_c = node.textStyling) === null || _c === void 0 ? void 0 : _c.Underline) || this.paragraphStyles.IsUnderline,
|
|
183
|
+
Size: this.paragraphStyles.Size || 12,
|
|
184
|
+
Uri: this.paragraphStyles.Uri || null,
|
|
185
|
+
Font: this.paragraphStyles.Font || 'Arial',
|
|
186
|
+
InsertLineBreak: this.paragraphStyles.InsertLineBreak || false,
|
|
187
|
+
InsertSpace: ((_d = node.textStyling) === null || _d === void 0 ? void 0 : _d.InsertSpace) || this.paragraphStyles.InsertSpace,
|
|
188
|
+
};
|
|
137
189
|
if (node.type === 'text') {
|
|
138
190
|
return { type: 'text', value: node.value, textStyling: textStyling };
|
|
139
191
|
}
|
|
@@ -166,7 +218,14 @@ class JSONRichTextParagraph {
|
|
|
166
218
|
insertPageBreak: false,
|
|
167
219
|
Rows: [],
|
|
168
220
|
};
|
|
221
|
+
if (!node || !Array.isArray(node.children)) {
|
|
222
|
+
logger_1.default.warn('Invalid table node structure');
|
|
223
|
+
return table;
|
|
224
|
+
}
|
|
169
225
|
this.parseTableChildren(node.children, table);
|
|
226
|
+
if (!this.validateTableStructure(table)) {
|
|
227
|
+
logger_1.default.warn('Inconsistent table structure detected');
|
|
228
|
+
}
|
|
170
229
|
return table;
|
|
171
230
|
}
|
|
172
231
|
/**
|
|
@@ -283,11 +342,6 @@ class JSONRichTextParagraph {
|
|
|
283
342
|
* @param level - The current depth level of the list (default is 0).
|
|
284
343
|
*/
|
|
285
344
|
parseListChildren(children, list, level = 0) {
|
|
286
|
-
if (level > 10) {
|
|
287
|
-
// Prevent infinite recursion
|
|
288
|
-
logger_1.default.warn('Maximum list nesting level reached');
|
|
289
|
-
return;
|
|
290
|
-
}
|
|
291
345
|
for (const child of children || []) {
|
|
292
346
|
if (child.type === 'other') {
|
|
293
347
|
const tn = (child.tagName || '').toLowerCase();
|
|
@@ -328,25 +382,39 @@ class JSONRichTextParagraph {
|
|
|
328
382
|
* @returns An array of Paragraph, Table, or List elements.
|
|
329
383
|
*/
|
|
330
384
|
buildDocFromNodes(nodes) {
|
|
385
|
+
if (!Array.isArray(nodes)) {
|
|
386
|
+
logger_1.default.error('Invalid nodes array');
|
|
387
|
+
return [];
|
|
388
|
+
}
|
|
331
389
|
const results = [];
|
|
332
390
|
for (const node of nodes) {
|
|
333
391
|
try {
|
|
334
392
|
const items = this.parseNode(node);
|
|
335
393
|
if (Array.isArray(items)) {
|
|
336
|
-
results.push(...items);
|
|
394
|
+
results.push(...items.filter(Boolean));
|
|
337
395
|
}
|
|
338
396
|
else if (items) {
|
|
339
397
|
results.push(items);
|
|
340
398
|
}
|
|
341
399
|
}
|
|
342
|
-
catch (
|
|
343
|
-
logger_1.default.error(
|
|
344
|
-
// Continue processing other nodes
|
|
400
|
+
catch (error) {
|
|
401
|
+
logger_1.default.error('Error processing node:', error);
|
|
345
402
|
continue;
|
|
346
403
|
}
|
|
347
404
|
}
|
|
405
|
+
// Ensure there's at least one paragraph
|
|
406
|
+
if (results.length === 0) {
|
|
407
|
+
results.push({ type: 'paragraph', runs: [] });
|
|
408
|
+
}
|
|
348
409
|
return results;
|
|
349
410
|
}
|
|
411
|
+
validateTableStructure(table) {
|
|
412
|
+
var _a, _b;
|
|
413
|
+
if (!table.Rows)
|
|
414
|
+
return false;
|
|
415
|
+
const firstRowCellCount = ((_b = (_a = table.Rows[0]) === null || _a === void 0 ? void 0 : _a.Cells) === null || _b === void 0 ? void 0 : _b.length) || 0;
|
|
416
|
+
return table.Rows.every((row) => row.Cells.length === firstRowCellCount);
|
|
417
|
+
}
|
|
350
418
|
} //class
|
|
351
419
|
exports.default = JSONRichTextParagraph;
|
|
352
420
|
//# sourceMappingURL=JSONRichTextParagraph.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"JSONRichTextParagraph.js","sourceRoot":"","sources":["../../../../src/models/json/paragraph/JSONRichTextParagraph.ts"],"names":[],"mappings":";;;;;AAAA,sEAA8C;AAc9C,MAAqB,qBAAqB;IAIxC,YACE,KAAiB,EACjB,eAA6B,EAC7B,IAAY,EACZ,YAAoB,EACpB,eAAe,GAAG,KAAK;QAEvB,IAAI,CAAC,eAAe,GAAG,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"JSONRichTextParagraph.js","sourceRoot":"","sources":["../../../../src/models/json/paragraph/JSONRichTextParagraph.ts"],"names":[],"mappings":";;;;;AAAA,sEAA8C;AAc9C,MAAqB,qBAAqB;IAIxC,YACE,KAAiB,EACjB,eAA6B,EAC7B,IAAY,EACZ,YAAoB,EACpB,eAAe,GAAG,KAAK;QAEvB,IAAI,CAAC,KAAK,IAAI,CAAC,KAAK,CAAC,aAAa,EAAE;YAClC,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;SAC3D;QAED,IAAI,CAAC,eAAe,GAAG,eAAe,IAAI;YACxC,MAAM,EAAE,KAAK;YACb,QAAQ,EAAE,KAAK;YACf,WAAW,EAAE,KAAK;YAClB,IAAI,EAAE,EAAE;YACR,GAAG,EAAE,IAAI;YACT,IAAI,EAAE,OAAO;YACb,eAAe,EAAE,KAAK;YACtB,WAAW,EAAE,KAAK;SACnB,CAAC;QACF,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,iBAAiB,GAAG,IAAI,CAAC,iBAAiB,CAAC,KAAK,CAAC,aAAa,CAAC,CAAC;IACvE,CAAC,CAAC,aAAa;IAEf,wBAAwB;QACtB,OAAO,IAAI,CAAC,iBAAiB,CAAC;IAChC,CAAC,CAAC,cAAc;IAEhB;;;;;;;;;;;;;;OAcG;IACK,SAAS,CAAC,IAAc;QAC9B,IAAI,CAAC,IAAI;YAAE,OAAO,IAAI,CAAC;QAEvB,6BAA6B;QAC7B,IAAI,OAAO,IAAI,CAAC,IAAI,KAAK,QAAQ,EAAE;YACjC,gBAAM,CAAC,IAAI,CAAC,kCAAkC,IAAI,CAAC,SAAS,CAAC,IAAI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,CAAC,MAAM,KAAK,iBAAiB,CAAC,CAAC;IAC3E,CAAC;CACF,CAAC,OAAO;AAnbT,wCAmbC"}
|
package/package.json
CHANGED
|
@@ -23,7 +23,20 @@ export default class JSONRichTextParagraph {
|
|
|
23
23
|
headingLevel: number,
|
|
24
24
|
skipImageFormat = false
|
|
25
25
|
) {
|
|
26
|
-
|
|
26
|
+
if (!field || !field.richTextNodes) {
|
|
27
|
+
throw new Error('Invalid field or missing richTextNodes');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
this.paragraphStyles = paragraphStyles || {
|
|
31
|
+
isBold: false,
|
|
32
|
+
IsItalic: false,
|
|
33
|
+
IsUnderline: false,
|
|
34
|
+
Size: 12,
|
|
35
|
+
Uri: null,
|
|
36
|
+
Font: 'Arial',
|
|
37
|
+
InsertLineBreak: false,
|
|
38
|
+
InsertSpace: false,
|
|
39
|
+
};
|
|
27
40
|
this.wiId = wiId;
|
|
28
41
|
this.paragraphTemplate = this.buildDocFromNodes(field.richTextNodes);
|
|
29
42
|
} //constructor
|
|
@@ -48,8 +61,11 @@ export default class JSONRichTextParagraph {
|
|
|
48
61
|
* - Default: Returns null for unrecognized node types.
|
|
49
62
|
*/
|
|
50
63
|
private parseNode(node: RichNode): Paragraph | Table | List | Array<Paragraph | Table | List> | null {
|
|
51
|
-
if (!node
|
|
52
|
-
|
|
64
|
+
if (!node) return null;
|
|
65
|
+
|
|
66
|
+
// Ensure node has valid type
|
|
67
|
+
if (typeof node.type !== 'string') {
|
|
68
|
+
logger.warn(`Invalid node type encountered: ${JSON.stringify(node)}`);
|
|
53
69
|
return null;
|
|
54
70
|
}
|
|
55
71
|
|
|
@@ -87,7 +103,29 @@ export default class JSONRichTextParagraph {
|
|
|
87
103
|
* @returns A Paragraph object containing the parsed runs.
|
|
88
104
|
*/
|
|
89
105
|
private parseParagraphNode(node: any): Paragraph {
|
|
106
|
+
if (!node || !Array.isArray(node.children)) {
|
|
107
|
+
return { type: 'paragraph', runs: [] };
|
|
108
|
+
}
|
|
109
|
+
|
|
90
110
|
const runs: Run[] = this.gatherRuns(node.children);
|
|
111
|
+
// Ensure at least one run exists
|
|
112
|
+
if (runs.length === 0) {
|
|
113
|
+
runs.push({
|
|
114
|
+
type: 'text',
|
|
115
|
+
value: '',
|
|
116
|
+
textStyling: {
|
|
117
|
+
Bold: this.paragraphStyles.isBold,
|
|
118
|
+
Italic: this.paragraphStyles.IsItalic,
|
|
119
|
+
Size: this.paragraphStyles.Size,
|
|
120
|
+
Font: this.paragraphStyles.Font,
|
|
121
|
+
Uri: this.paragraphStyles.Uri,
|
|
122
|
+
InsertLineBreak: this.paragraphStyles.InsertLineBreak,
|
|
123
|
+
InsertSpace: this.paragraphStyles.InsertSpace,
|
|
124
|
+
Underline: this.paragraphStyles.IsUnderline,
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
91
129
|
return { type: 'paragraph', runs };
|
|
92
130
|
}
|
|
93
131
|
|
|
@@ -106,26 +144,35 @@ export default class JSONRichTextParagraph {
|
|
|
106
144
|
* @returns An array of `Run` objects representing the gathered runs.
|
|
107
145
|
*/
|
|
108
146
|
private gatherRuns(children: any[]): Run[] {
|
|
147
|
+
if (!Array.isArray(children)) return [];
|
|
148
|
+
|
|
109
149
|
const runs: Run[] = [];
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
150
|
+
|
|
151
|
+
for (const child of children) {
|
|
152
|
+
if (!child || typeof child.type !== 'string') {
|
|
153
|
+
logger.warn('Invalid child node:', child);
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
try {
|
|
158
|
+
if (child.type === 'text' || child.type === 'image' || child.type === 'break') {
|
|
159
|
+
runs.push(this.nodeToRun(child));
|
|
160
|
+
} else if (child.type === 'paragraph') {
|
|
161
|
+
// Recursively gather runs from child paragraphs
|
|
162
|
+
runs.push(...this.gatherRuns(child.children));
|
|
163
|
+
} else if (child.type === 'table' || child.type === 'list') {
|
|
164
|
+
// Not supported in this context, so we'll just add a placeholder
|
|
165
|
+
runs.push({ type: 'other', value: `[Embedded ${child.type}]` });
|
|
166
|
+
} else if (child.type === 'other') {
|
|
167
|
+
// Recursively gather runs from inside <span> or <b>, etc.
|
|
168
|
+
runs.push(...this.gatherRuns(child.children));
|
|
169
|
+
}
|
|
170
|
+
} catch (error) {
|
|
171
|
+
logger.error('Error gathering runs:', error);
|
|
172
|
+
continue;
|
|
127
173
|
}
|
|
128
174
|
}
|
|
175
|
+
|
|
129
176
|
return runs;
|
|
130
177
|
}
|
|
131
178
|
|
|
@@ -153,12 +200,14 @@ export default class JSONRichTextParagraph {
|
|
|
153
200
|
*/
|
|
154
201
|
private nodeToRun(node: any): Run {
|
|
155
202
|
const textStyling = {
|
|
156
|
-
|
|
157
|
-
|
|
203
|
+
Bold: node.textStyling?.Bold || this.paragraphStyles.isBold,
|
|
204
|
+
Italic: node.textStyling?.Italic || this.paragraphStyles.IsItalic,
|
|
205
|
+
Underline: node.textStyling?.Underline || this.paragraphStyles.IsUnderline,
|
|
158
206
|
Size: this.paragraphStyles.Size || 12,
|
|
159
207
|
Uri: this.paragraphStyles.Uri || null,
|
|
160
208
|
Font: this.paragraphStyles.Font || 'Arial',
|
|
161
209
|
InsertLineBreak: this.paragraphStyles.InsertLineBreak || false,
|
|
210
|
+
InsertSpace: node.textStyling?.InsertSpace || this.paragraphStyles.InsertSpace,
|
|
162
211
|
};
|
|
163
212
|
|
|
164
213
|
if (node.type === 'text') {
|
|
@@ -192,7 +241,17 @@ export default class JSONRichTextParagraph {
|
|
|
192
241
|
Rows: [],
|
|
193
242
|
};
|
|
194
243
|
|
|
244
|
+
if (!node || !Array.isArray(node.children)) {
|
|
245
|
+
logger.warn('Invalid table node structure');
|
|
246
|
+
return table;
|
|
247
|
+
}
|
|
248
|
+
|
|
195
249
|
this.parseTableChildren(node.children, table);
|
|
250
|
+
|
|
251
|
+
if (!this.validateTableStructure(table)) {
|
|
252
|
+
logger.warn('Inconsistent table structure detected');
|
|
253
|
+
}
|
|
254
|
+
|
|
196
255
|
return table;
|
|
197
256
|
}
|
|
198
257
|
|
|
@@ -312,12 +371,6 @@ export default class JSONRichTextParagraph {
|
|
|
312
371
|
* @param level - The current depth level of the list (default is 0).
|
|
313
372
|
*/
|
|
314
373
|
private parseListChildren(children: any[], list: List, level: number = 0): void {
|
|
315
|
-
if (level > 10) {
|
|
316
|
-
// Prevent infinite recursion
|
|
317
|
-
logger.warn('Maximum list nesting level reached');
|
|
318
|
-
return;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
374
|
for (const child of children || []) {
|
|
322
375
|
if (child.type === 'other') {
|
|
323
376
|
const tn = (child.tagName || '').toLowerCase();
|
|
@@ -359,21 +412,39 @@ export default class JSONRichTextParagraph {
|
|
|
359
412
|
* @returns An array of Paragraph, Table, or List elements.
|
|
360
413
|
*/
|
|
361
414
|
private buildDocFromNodes(nodes: RichNode[]): Array<Paragraph | Table | List> {
|
|
415
|
+
if (!Array.isArray(nodes)) {
|
|
416
|
+
logger.error('Invalid nodes array');
|
|
417
|
+
return [];
|
|
418
|
+
}
|
|
419
|
+
|
|
362
420
|
const results: Array<Paragraph | Table | List> = [];
|
|
421
|
+
|
|
363
422
|
for (const node of nodes) {
|
|
364
423
|
try {
|
|
365
424
|
const items = this.parseNode(node);
|
|
366
425
|
if (Array.isArray(items)) {
|
|
367
|
-
results.push(...items);
|
|
426
|
+
results.push(...items.filter(Boolean));
|
|
368
427
|
} else if (items) {
|
|
369
428
|
results.push(items);
|
|
370
429
|
}
|
|
371
|
-
} catch (
|
|
372
|
-
logger.error(
|
|
373
|
-
// Continue processing other nodes
|
|
430
|
+
} catch (error) {
|
|
431
|
+
logger.error('Error processing node:', error);
|
|
374
432
|
continue;
|
|
375
433
|
}
|
|
376
434
|
}
|
|
435
|
+
|
|
436
|
+
// Ensure there's at least one paragraph
|
|
437
|
+
if (results.length === 0) {
|
|
438
|
+
results.push({ type: 'paragraph', runs: [] });
|
|
439
|
+
}
|
|
440
|
+
|
|
377
441
|
return results;
|
|
378
442
|
}
|
|
443
|
+
|
|
444
|
+
private validateTableStructure(table: Table): boolean {
|
|
445
|
+
if (!table.Rows) return false;
|
|
446
|
+
|
|
447
|
+
const firstRowCellCount = table.Rows[0]?.Cells?.length || 0;
|
|
448
|
+
return table.Rows.every((row) => row.Cells.length === firstRowCellCount);
|
|
449
|
+
}
|
|
379
450
|
} //class
|