@elisra-devops/docgen-skins 0.10.8 → 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/index.js +3 -3
- package/bin/index.js.map +1 -1
- package/bin/models/json/JSONRun.js +39 -35
- package/bin/models/json/JSONRun.js.map +1 -1
- package/bin/models/json/default.js +12 -9
- package/bin/models/json/default.js.map +1 -1
- package/bin/models/json/paragraph/JSONRichTextParagraph.d.ts +134 -2
- package/bin/models/json/paragraph/JSONRichTextParagraph.js +402 -53
- package/bin/models/json/paragraph/JSONRichTextParagraph.js.map +1 -1
- package/bin/models/json/wordJsonModels.d.ts +41 -4
- package/package.json +1 -1
- package/src/index.ts +3 -9
- package/src/models/json/JSONRun.ts +36 -32
- package/src/models/json/default.ts +12 -9
- package/src/models/json/paragraph/JSONRichTextParagraph.ts +428 -405
- package/src/models/json/wordJsonModels.ts +49 -4
|
@@ -1,8 +1,16 @@
|
|
|
1
|
-
import { Run, WIProperty, StyleOptions } from '../wordJsonModels';
|
|
2
|
-
import JSONHtml from '../html/JSONHtml';
|
|
3
|
-
import JSONParagraph from './JSONParagraph';
|
|
4
|
-
import JSONTable from '../table/JSONTable';
|
|
5
1
|
import logger from '../../../services/logger';
|
|
2
|
+
import {
|
|
3
|
+
Run,
|
|
4
|
+
WIProperty,
|
|
5
|
+
StyleOptions,
|
|
6
|
+
RichNode,
|
|
7
|
+
Paragraph,
|
|
8
|
+
Table,
|
|
9
|
+
TableRow,
|
|
10
|
+
TableCell,
|
|
11
|
+
List,
|
|
12
|
+
ListItem,
|
|
13
|
+
} from '../wordJsonModels';
|
|
6
14
|
|
|
7
15
|
export default class JSONRichTextParagraph {
|
|
8
16
|
paragraphStyles: StyleOptions;
|
|
@@ -15,413 +23,428 @@ export default class JSONRichTextParagraph {
|
|
|
15
23
|
headingLevel: number,
|
|
16
24
|
skipImageFormat = false
|
|
17
25
|
) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
this.paragraphTemplate = this.generateJsonRichTextParagraphs(
|
|
21
|
-
field,
|
|
22
|
-
this.paragraphStyles,
|
|
23
|
-
skipImageFormat
|
|
24
|
-
);
|
|
25
|
-
if (headingLevel && field.name === 'Title: ') {
|
|
26
|
-
this.paragraphTemplate.headingLevel = headingLevel;
|
|
26
|
+
if (!field || !field.richTextNodes) {
|
|
27
|
+
throw new Error('Invalid field or missing richTextNodes');
|
|
27
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
|
+
};
|
|
40
|
+
this.wiId = wiId;
|
|
41
|
+
this.paragraphTemplate = this.buildDocFromNodes(field.richTextNodes);
|
|
28
42
|
} //constructor
|
|
29
43
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
44
|
+
getJSONRichTextParagraph(): any[] {
|
|
45
|
+
return this.paragraphTemplate;
|
|
46
|
+
} //getJSONTable
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Parses a given RichNode and returns a corresponding Paragraph, Table, List,
|
|
50
|
+
* an array of these types, or null if the node type is not recognized.
|
|
51
|
+
*
|
|
52
|
+
* @param node - The RichNode to be parsed.
|
|
53
|
+
* @returns A Paragraph, Table, List, an array of these types, or null.
|
|
54
|
+
*
|
|
55
|
+
* The function handles different node types as follows:
|
|
56
|
+
* - 'paragraph': Parses the node as a paragraph.
|
|
57
|
+
* - 'table': Parses the node as a table.
|
|
58
|
+
* - 'list': Parses the node as a list.
|
|
59
|
+
* - 'text', 'image', 'break': Converts the node to a paragraph with a single run.
|
|
60
|
+
* - 'other': Recursively parses child nodes and returns an array of results.
|
|
61
|
+
* - Default: Returns null for unrecognized node types.
|
|
62
|
+
*/
|
|
63
|
+
private parseNode(node: RichNode): Paragraph | Table | List | Array<Paragraph | Table | List> | null {
|
|
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)}`);
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
switch (node.type) {
|
|
73
|
+
case 'paragraph':
|
|
74
|
+
return this.parseParagraphNode(node);
|
|
75
|
+
case 'table':
|
|
76
|
+
return this.parseTableNode(node);
|
|
77
|
+
case 'list':
|
|
78
|
+
return this.parseList(node);
|
|
79
|
+
case 'text':
|
|
80
|
+
case 'image':
|
|
81
|
+
case 'break':
|
|
82
|
+
return { type: 'paragraph', runs: [this.nodeToRun(node)] };
|
|
83
|
+
case 'other':
|
|
84
|
+
let childrenResults: Array<Paragraph | Table | List> = [];
|
|
85
|
+
for (const child of node.children) {
|
|
86
|
+
const childResult = this.parseNode(child);
|
|
87
|
+
if (Array.isArray(childResult)) {
|
|
88
|
+
childrenResults.push(...childResult);
|
|
89
|
+
} else if (childResult) {
|
|
90
|
+
childrenResults.push(childResult);
|
|
91
|
+
}
|
|
70
92
|
}
|
|
93
|
+
return childrenResults.length > 0 ? childrenResults : null;
|
|
94
|
+
default:
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Parses a paragraph node and converts it into a Paragraph object.
|
|
101
|
+
*
|
|
102
|
+
* @param node - The node to parse, expected to have a `children` property.
|
|
103
|
+
* @returns A Paragraph object containing the parsed runs.
|
|
104
|
+
*/
|
|
105
|
+
private parseParagraphNode(node: any): Paragraph {
|
|
106
|
+
if (!node || !Array.isArray(node.children)) {
|
|
107
|
+
return { type: 'paragraph', runs: [] };
|
|
108
|
+
}
|
|
109
|
+
|
|
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
|
+
},
|
|
71
126
|
});
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return { type: 'paragraph', runs };
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Gathers runs from the provided children nodes.
|
|
134
|
+
*
|
|
135
|
+
* This method processes an array of child nodes and converts them into an array of `Run` objects.
|
|
136
|
+
* It handles different types of nodes such as text, image, break, paragraph, table, list, and other.
|
|
137
|
+
*
|
|
138
|
+
* - For `text`, `image`, and `break` nodes, it directly converts them to `Run` objects.
|
|
139
|
+
* - For `paragraph` nodes, it recursively gathers runs from the child paragraphs.
|
|
140
|
+
* - For `table` and `list` nodes, it adds a placeholder `Run` object indicating the embedded type.
|
|
141
|
+
* - For `other` nodes, it recursively gathers runs from the child nodes.
|
|
142
|
+
*
|
|
143
|
+
* @param children - An array of child nodes to process.
|
|
144
|
+
* @returns An array of `Run` objects representing the gathered runs.
|
|
145
|
+
*/
|
|
146
|
+
private gatherRuns(children: any[]): Run[] {
|
|
147
|
+
if (!Array.isArray(children)) return [];
|
|
148
|
+
|
|
149
|
+
const runs: Run[] = [];
|
|
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;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
return runs;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Converts a node object to a Run object based on its type and text styling.
|
|
181
|
+
*
|
|
182
|
+
* @param node - The node object to be converted. It can be of type 'text', 'image', or 'break'.
|
|
183
|
+
* @returns A Run object with the appropriate type and properties based on the input node.
|
|
184
|
+
*
|
|
185
|
+
* The Run object can have the following structure:
|
|
186
|
+
* - For 'text' nodes: { type: 'text', value: string, textStyling: object }
|
|
187
|
+
* - For 'image' nodes: { type: 'image', value: '', src: string }
|
|
188
|
+
* - For 'break' nodes: { type: 'break' }
|
|
189
|
+
* - For other types: { type: 'other', value: '' }
|
|
190
|
+
*
|
|
191
|
+
* The textStyling object includes:
|
|
192
|
+
* - Bold: boolean
|
|
193
|
+
* - Italic: boolean
|
|
194
|
+
* - Underline: boolean
|
|
195
|
+
* - Size: number
|
|
196
|
+
* - Uri: string | null
|
|
197
|
+
* - Font: string
|
|
198
|
+
* - InsertLineBreak: boolean
|
|
199
|
+
* - InsertSpace: boolean
|
|
200
|
+
*/
|
|
201
|
+
private nodeToRun(node: any): Run {
|
|
202
|
+
const textStyling = {
|
|
203
|
+
Bold: node.textStyling?.Bold || this.paragraphStyles.isBold,
|
|
204
|
+
Italic: node.textStyling?.Italic || this.paragraphStyles.IsItalic,
|
|
205
|
+
Underline: node.textStyling?.Underline || this.paragraphStyles.IsUnderline,
|
|
206
|
+
Size: this.paragraphStyles.Size || 12,
|
|
207
|
+
Uri: this.paragraphStyles.Uri || null,
|
|
208
|
+
Font: this.paragraphStyles.Font || 'Arial',
|
|
209
|
+
InsertLineBreak: this.paragraphStyles.InsertLineBreak || false,
|
|
210
|
+
InsertSpace: node.textStyling?.InsertSpace || this.paragraphStyles.InsertSpace,
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
if (node.type === 'text') {
|
|
214
|
+
return { type: 'text', value: node.value, textStyling: textStyling };
|
|
215
|
+
} else if (node.type === 'image') {
|
|
216
|
+
return { type: 'image', value: '', src: node.src || '' };
|
|
217
|
+
} else if (node.type === 'break') {
|
|
218
|
+
return { type: 'break' };
|
|
219
|
+
} else {
|
|
220
|
+
return { type: 'other', value: '' };
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Converts a given node object into a Table structure.
|
|
226
|
+
*
|
|
227
|
+
* @remarks
|
|
228
|
+
* This function processes the children of the specified node and
|
|
229
|
+
* transforms them into a table representation, adding default properties
|
|
230
|
+
* such as heading level, page break insertion, and an empty rows array.
|
|
231
|
+
*
|
|
232
|
+
* @param node - The node object containing the structural details needed
|
|
233
|
+
* to create a table.
|
|
234
|
+
* @returns A fully constructed Table object with the parsed node data.
|
|
235
|
+
*/
|
|
236
|
+
private parseTableNode(node: any): Table {
|
|
237
|
+
const table: Table = {
|
|
238
|
+
type: 'table',
|
|
239
|
+
headingLevel: 0,
|
|
240
|
+
insertPageBreak: false,
|
|
241
|
+
Rows: [],
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
if (!node || !Array.isArray(node.children)) {
|
|
245
|
+
logger.warn('Invalid table node structure');
|
|
246
|
+
return table;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
this.parseTableChildren(node.children, table);
|
|
250
|
+
|
|
251
|
+
if (!this.validateTableStructure(table)) {
|
|
252
|
+
logger.warn('Inconsistent table structure detected');
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
return table;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Parses child elements of a table structure and updates the provided Table instance accordingly.
|
|
260
|
+
*
|
|
261
|
+
* @param children - An array of child nodes to be evaluated for table content.
|
|
262
|
+
* @param table - The Table object to which parsed rows and nested structures will be appended.
|
|
263
|
+
*
|
|
264
|
+
* @remarks
|
|
265
|
+
* This method recursively traverses and identifies relevant table elements (e.g., <tr>, <thead>, <tbody>, etc.)
|
|
266
|
+
* to generate the final row data for the given Table instance.
|
|
267
|
+
*/
|
|
268
|
+
private parseTableChildren(children: any[], table: Table): void {
|
|
269
|
+
for (const child of children || []) {
|
|
270
|
+
if (child.type === 'other') {
|
|
271
|
+
const tn = (child.tagName || '').toLowerCase();
|
|
272
|
+
if (tn === 'tr') {
|
|
273
|
+
// Parse a table row
|
|
274
|
+
const row = this.parseTableRow(child);
|
|
275
|
+
table.Rows.push(row);
|
|
276
|
+
} else if (tn === 'thead' || tn === 'tbody' || tn === 'tfoot') {
|
|
277
|
+
// Recurse into thead/tbody/tfoot
|
|
278
|
+
this.parseTableChildren(child.children, table);
|
|
279
|
+
} else {
|
|
280
|
+
// Recurse into other nodes
|
|
281
|
+
this.parseTableChildren(child.children, table);
|
|
282
|
+
}
|
|
283
|
+
} else if (child.type === 'table') {
|
|
284
|
+
this.parseTableChildren(child.children, table);
|
|
285
|
+
} else {
|
|
286
|
+
//skip
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* Parses the given table row node and extracts its cells by iterating through its child elements.
|
|
294
|
+
* Each child element that is recognized as a table cell (td or th) is converted into a corresponding cell object.
|
|
295
|
+
*
|
|
296
|
+
* @param trNode - An AST node representing the table row, containing possible table cell or header elements.
|
|
297
|
+
* @returns A TableRow object containing an array of processed cells.
|
|
298
|
+
*/
|
|
299
|
+
private parseTableRow(trNode: any): TableRow {
|
|
300
|
+
const row: TableRow = { Cells: [] };
|
|
301
|
+
|
|
302
|
+
// parse <td> or <th> inside this <tr>
|
|
303
|
+
for (const child of trNode.children || []) {
|
|
304
|
+
if (child.type === 'other' && (child.tagName === 'td' || child.tagName === 'th')) {
|
|
305
|
+
row.Cells.push(this.parseTableCell(child));
|
|
306
|
+
} else {
|
|
307
|
+
// skip or recurse
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
return row;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/**
|
|
315
|
+
* Build one TableCell from <td> node
|
|
316
|
+
*/
|
|
317
|
+
private parseTableCell(tdNode: any): TableCell {
|
|
318
|
+
// We'll collect everything in one paragraph
|
|
319
|
+
const cellParagraphRuns = this.gatherRunsFromAllParagraphs(tdNode.children);
|
|
320
|
+
|
|
321
|
+
return {
|
|
322
|
+
Paragraphs: [
|
|
323
|
+
{
|
|
324
|
+
Runs: cellParagraphRuns,
|
|
325
|
+
},
|
|
326
|
+
],
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Gather runs from any text/image/paragraph in <td>
|
|
332
|
+
* (including nested <other> tags).
|
|
333
|
+
*/
|
|
334
|
+
private gatherRunsFromAllParagraphs(children: any[]): Run[] {
|
|
335
|
+
const runs: Run[] = [];
|
|
336
|
+
for (const child of children || []) {
|
|
337
|
+
if (child.type === 'text' || child.type === 'image') {
|
|
338
|
+
runs.push(this.nodeToRun(child));
|
|
339
|
+
} else if (child.type === 'paragraph') {
|
|
340
|
+
runs.push(...this.gatherRuns(child.children));
|
|
341
|
+
} else if (child.type === 'other' || child.type === 'table' || child.type === 'list') {
|
|
342
|
+
// recursively gather
|
|
343
|
+
runs.push(...this.gatherRunsFromAllParagraphs(child.children));
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
return runs;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/**
|
|
350
|
+
* Parses a given node to create a List object.
|
|
351
|
+
*
|
|
352
|
+
* @param node - The node to parse, expected to have properties `isOrdered` and `children`.
|
|
353
|
+
* @returns A List object with the parsed data.
|
|
354
|
+
*/
|
|
355
|
+
private parseList(node: any): List {
|
|
356
|
+
const list: List = {
|
|
357
|
+
type: 'list',
|
|
358
|
+
isOrdered: node.isOrdered,
|
|
359
|
+
listItems: [],
|
|
360
|
+
};
|
|
361
|
+
|
|
362
|
+
this.parseListChildren(node.children, list);
|
|
363
|
+
return list;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Parses the children of a list and adds them to the provided list object.
|
|
368
|
+
*
|
|
369
|
+
* @param children - An array of child nodes to be parsed.
|
|
370
|
+
* @param list - The list object to which parsed items will be added.
|
|
371
|
+
* @param level - The current depth level of the list (default is 0).
|
|
372
|
+
*/
|
|
373
|
+
private parseListChildren(children: any[], list: List, level: number = 0): void {
|
|
374
|
+
for (const child of children || []) {
|
|
375
|
+
if (child.type === 'other') {
|
|
376
|
+
const tn = (child.tagName || '').toLowerCase();
|
|
377
|
+
if (tn === 'li') {
|
|
378
|
+
// Parse a list item
|
|
379
|
+
const item = this.parseListItem(child, level);
|
|
380
|
+
list.listItems.push(item);
|
|
381
|
+
} else {
|
|
382
|
+
// Recurse into other nodes
|
|
383
|
+
this.parseListChildren(child.children, list, level + 1);
|
|
384
|
+
}
|
|
385
|
+
} else {
|
|
386
|
+
//skip
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Parses a list item node and converts it into a `ListItem` object.
|
|
394
|
+
*
|
|
395
|
+
* @param liNode - The list item node to parse.
|
|
396
|
+
* @param level - The nesting level of the list item.
|
|
397
|
+
* @returns A `ListItem` object containing the parsed list item data.
|
|
398
|
+
*/
|
|
399
|
+
private parseListItem(liNode: any, level: number): ListItem {
|
|
400
|
+
const items = this.gatherRunsFromAllParagraphs(liNode.children);
|
|
401
|
+
|
|
402
|
+
return {
|
|
403
|
+
Runs: items,
|
|
404
|
+
level: level,
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/**
|
|
409
|
+
* Builds an array of document elements (Paragraph, Table, or List) from an array of RichNode objects.
|
|
410
|
+
*
|
|
411
|
+
* @param nodes - An array of RichNode objects to be parsed into document elements.
|
|
412
|
+
* @returns An array of Paragraph, Table, or List elements.
|
|
413
|
+
*/
|
|
414
|
+
private buildDocFromNodes(nodes: RichNode[]): Array<Paragraph | Table | List> {
|
|
415
|
+
if (!Array.isArray(nodes)) {
|
|
416
|
+
logger.error('Invalid nodes array');
|
|
75
417
|
return [];
|
|
76
418
|
}
|
|
77
|
-
} //generateJsonRichTextParagraphs
|
|
78
419
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
// case 'list':
|
|
110
|
-
// return this.parseList(node);
|
|
111
|
-
// case 'text':
|
|
112
|
-
// case 'image':
|
|
113
|
-
// case 'break':
|
|
114
|
-
// return { type: 'paragraph', runs: [this.nodeToRun(node)] };
|
|
115
|
-
// case 'other':
|
|
116
|
-
// let childrenResults: Array<Paragraph | Table | List> = [];
|
|
117
|
-
// for (const child of node.children) {
|
|
118
|
-
// const childResult = this.parseNode(child);
|
|
119
|
-
// if (Array.isArray(childResult)) {
|
|
120
|
-
// childrenResults.push(...childResult);
|
|
121
|
-
// } else if (childResult) {
|
|
122
|
-
// childrenResults.push(childResult);
|
|
123
|
-
// }
|
|
124
|
-
// }
|
|
125
|
-
// return childrenResults.length > 0 ? childrenResults : null;
|
|
126
|
-
// default:
|
|
127
|
-
// return null;
|
|
128
|
-
// }
|
|
129
|
-
// }
|
|
130
|
-
|
|
131
|
-
// /**
|
|
132
|
-
// * Parses a paragraph node and converts it into a Paragraph object.
|
|
133
|
-
// *
|
|
134
|
-
// * @param node - The node to parse, expected to have a `children` property.
|
|
135
|
-
// * @returns A Paragraph object containing the parsed runs.
|
|
136
|
-
// */
|
|
137
|
-
// private parseParagraphNode(node: any): Paragraph {
|
|
138
|
-
// const runs: Run[] = this.gatherRuns(node.children);
|
|
139
|
-
// return { type: 'paragraph', runs };
|
|
140
|
-
// }
|
|
141
|
-
|
|
142
|
-
// /**
|
|
143
|
-
// * Gathers runs from the provided children nodes.
|
|
144
|
-
// *
|
|
145
|
-
// * This method processes an array of child nodes and converts them into an array of `Run` objects.
|
|
146
|
-
// * It handles different types of nodes such as text, image, break, paragraph, table, list, and other.
|
|
147
|
-
// *
|
|
148
|
-
// * - For `text`, `image`, and `break` nodes, it directly converts them to `Run` objects.
|
|
149
|
-
// * - For `paragraph` nodes, it recursively gathers runs from the child paragraphs.
|
|
150
|
-
// * - For `table` and `list` nodes, it adds a placeholder `Run` object indicating the embedded type.
|
|
151
|
-
// * - For `other` nodes, it recursively gathers runs from the child nodes.
|
|
152
|
-
// *
|
|
153
|
-
// * @param children - An array of child nodes to process.
|
|
154
|
-
// * @returns An array of `Run` objects representing the gathered runs.
|
|
155
|
-
// */
|
|
156
|
-
// private gatherRuns(children: any[]): Run[] {
|
|
157
|
-
// const runs: Run[] = [];
|
|
158
|
-
// if (!Array.isArray(children)) {
|
|
159
|
-
// logger.warn(`Invalid children structure: ${JSON.stringify(children)}`);
|
|
160
|
-
// return runs;
|
|
161
|
-
// }
|
|
162
|
-
// for (const child of children || []) {
|
|
163
|
-
// if (child.type === 'text' || child.type === 'image' || child.type === 'break') {
|
|
164
|
-
// runs.push(this.nodeToRun(child));
|
|
165
|
-
// } else if (child.type === 'paragraph') {
|
|
166
|
-
// // Recursively gather runs from child paragraphs
|
|
167
|
-
// runs.push(...this.gatherRuns(child.children));
|
|
168
|
-
// } else if (child.type === 'table' || child.type === 'list') {
|
|
169
|
-
// // Not supported in this context, so we'll just add a placeholder
|
|
170
|
-
// runs.push({ type: 'other', value: `[Embedded ${child.type}]` });
|
|
171
|
-
// } else if (child.type === 'other') {
|
|
172
|
-
// // Recursively gather runs from inside <span> or <b>, etc.
|
|
173
|
-
// // TODO: Handle b and i, u tags and more relevant tags for text styling
|
|
174
|
-
// runs.push(...this.gatherRuns(child.children));
|
|
175
|
-
// }
|
|
176
|
-
// }
|
|
177
|
-
// return runs;
|
|
178
|
-
// }
|
|
179
|
-
|
|
180
|
-
// /**
|
|
181
|
-
// * Converts a node object to a Run object based on its type and text styling.
|
|
182
|
-
// *
|
|
183
|
-
// * @param node - The node object to be converted. It can be of type 'text', 'image', or 'break'.
|
|
184
|
-
// * @returns A Run object with the appropriate type and properties based on the input node.
|
|
185
|
-
// *
|
|
186
|
-
// * The Run object can have the following structure:
|
|
187
|
-
// * - For 'text' nodes: { type: 'text', value: string, textStyling: object }
|
|
188
|
-
// * - For 'image' nodes: { type: 'image', value: '', src: string }
|
|
189
|
-
// * - For 'break' nodes: { type: 'break' }
|
|
190
|
-
// * - For other types: { type: 'other', value: '' }
|
|
191
|
-
// *
|
|
192
|
-
// * The textStyling object includes:
|
|
193
|
-
// * - Bold: boolean
|
|
194
|
-
// * - Italic: boolean
|
|
195
|
-
// * - Underline: boolean
|
|
196
|
-
// * - Size: number
|
|
197
|
-
// * - Uri: string | null
|
|
198
|
-
// * - Font: string
|
|
199
|
-
// * - InsertLineBreak: boolean
|
|
200
|
-
// * - InsertSpace: boolean
|
|
201
|
-
// */
|
|
202
|
-
// private nodeToRun(node: any): Run {
|
|
203
|
-
// const textStyling = {
|
|
204
|
-
// ...this.paragraphStyles, // Base styles
|
|
205
|
-
// ...node.textStyling, // Override with node specific styling
|
|
206
|
-
// Size: this.paragraphStyles.Size || 12,
|
|
207
|
-
// Uri: this.paragraphStyles.Uri || null,
|
|
208
|
-
// Font: this.paragraphStyles.Font || 'Arial',
|
|
209
|
-
// InsertLineBreak: this.paragraphStyles.InsertLineBreak || false,
|
|
210
|
-
// };
|
|
211
|
-
|
|
212
|
-
// if (node.type === 'text') {
|
|
213
|
-
// return { type: 'text', value: node.value, textStyling: textStyling };
|
|
214
|
-
// } else if (node.type === 'image') {
|
|
215
|
-
// return { type: 'image', value: '', src: node.src || '' };
|
|
216
|
-
// } else if (node.type === 'break') {
|
|
217
|
-
// return { type: 'break' };
|
|
218
|
-
// } else {
|
|
219
|
-
// return { type: 'other', value: '' };
|
|
220
|
-
// }
|
|
221
|
-
// }
|
|
222
|
-
|
|
223
|
-
// /**
|
|
224
|
-
// * Converts a given node object into a Table structure.
|
|
225
|
-
// *
|
|
226
|
-
// * @remarks
|
|
227
|
-
// * This function processes the children of the specified node and
|
|
228
|
-
// * transforms them into a table representation, adding default properties
|
|
229
|
-
// * such as heading level, page break insertion, and an empty rows array.
|
|
230
|
-
// *
|
|
231
|
-
// * @param node - The node object containing the structural details needed
|
|
232
|
-
// * to create a table.
|
|
233
|
-
// * @returns A fully constructed Table object with the parsed node data.
|
|
234
|
-
// */
|
|
235
|
-
// private parseTableNode(node: any): Table {
|
|
236
|
-
// const table: Table = {
|
|
237
|
-
// type: 'table',
|
|
238
|
-
// headingLevel: 0,
|
|
239
|
-
// insertPageBreak: false,
|
|
240
|
-
// Rows: [],
|
|
241
|
-
// };
|
|
242
|
-
|
|
243
|
-
// this.parseTableChildren(node.children, table);
|
|
244
|
-
// return table;
|
|
245
|
-
// }
|
|
246
|
-
|
|
247
|
-
// /**
|
|
248
|
-
// * Parses child elements of a table structure and updates the provided Table instance accordingly.
|
|
249
|
-
// *
|
|
250
|
-
// * @param children - An array of child nodes to be evaluated for table content.
|
|
251
|
-
// * @param table - The Table object to which parsed rows and nested structures will be appended.
|
|
252
|
-
// *
|
|
253
|
-
// * @remarks
|
|
254
|
-
// * This method recursively traverses and identifies relevant table elements (e.g., <tr>, <thead>, <tbody>, etc.)
|
|
255
|
-
// * to generate the final row data for the given Table instance.
|
|
256
|
-
// */
|
|
257
|
-
// private parseTableChildren(children: any[], table: Table): void {
|
|
258
|
-
// for (const child of children || []) {
|
|
259
|
-
// if (child.type === 'other') {
|
|
260
|
-
// const tn = (child.tagName || '').toLowerCase();
|
|
261
|
-
// if (tn === 'tr') {
|
|
262
|
-
// // Parse a table row
|
|
263
|
-
// const row = this.parseTableRow(child);
|
|
264
|
-
// table.Rows.push(row);
|
|
265
|
-
// } else if (tn === 'thead' || tn === 'tbody' || tn === 'tfoot') {
|
|
266
|
-
// // Recurse into thead/tbody/tfoot
|
|
267
|
-
// this.parseTableChildren(child.children, table);
|
|
268
|
-
// } else {
|
|
269
|
-
// // Recurse into other nodes
|
|
270
|
-
// this.parseTableChildren(child.children, table);
|
|
271
|
-
// }
|
|
272
|
-
// } else if (child.type === 'table') {
|
|
273
|
-
// this.parseTableChildren(child.children, table);
|
|
274
|
-
// } else {
|
|
275
|
-
// //skip
|
|
276
|
-
// continue;
|
|
277
|
-
// }
|
|
278
|
-
// }
|
|
279
|
-
// }
|
|
280
|
-
|
|
281
|
-
// /**
|
|
282
|
-
// * Parses the given table row node and extracts its cells by iterating through its child elements.
|
|
283
|
-
// * Each child element that is recognized as a table cell (td or th) is converted into a corresponding cell object.
|
|
284
|
-
// *
|
|
285
|
-
// * @param trNode - An AST node representing the table row, containing possible table cell or header elements.
|
|
286
|
-
// * @returns A TableRow object containing an array of processed cells.
|
|
287
|
-
// */
|
|
288
|
-
// private parseTableRow(trNode: any): TableRow {
|
|
289
|
-
// const row: TableRow = { Cells: [] };
|
|
290
|
-
|
|
291
|
-
// // parse <td> or <th> inside this <tr>
|
|
292
|
-
// for (const child of trNode.children || []) {
|
|
293
|
-
// if (child.type === 'other' && (child.tagName === 'td' || child.tagName === 'th')) {
|
|
294
|
-
// row.Cells.push(this.parseTableCell(child));
|
|
295
|
-
// } else {
|
|
296
|
-
// // skip or recurse
|
|
297
|
-
// }
|
|
298
|
-
// }
|
|
299
|
-
|
|
300
|
-
// return row;
|
|
301
|
-
// }
|
|
302
|
-
|
|
303
|
-
// /**
|
|
304
|
-
// * Build one TableCell from <td> node
|
|
305
|
-
// */
|
|
306
|
-
// private parseTableCell(tdNode: any): TableCell {
|
|
307
|
-
// // We'll collect everything in one paragraph
|
|
308
|
-
// const cellParagraphRuns = this.gatherRunsFromAllParagraphs(tdNode.children);
|
|
309
|
-
|
|
310
|
-
// return {
|
|
311
|
-
// Paragraphs: [
|
|
312
|
-
// {
|
|
313
|
-
// Runs: cellParagraphRuns,
|
|
314
|
-
// },
|
|
315
|
-
// ],
|
|
316
|
-
// };
|
|
317
|
-
// }
|
|
318
|
-
|
|
319
|
-
// /**
|
|
320
|
-
// * Gather runs from any text/image/paragraph in <td>
|
|
321
|
-
// * (including nested <other> tags).
|
|
322
|
-
// */
|
|
323
|
-
// private gatherRunsFromAllParagraphs(children: any[]): Run[] {
|
|
324
|
-
// const runs: Run[] = [];
|
|
325
|
-
// for (const child of children || []) {
|
|
326
|
-
// if (child.type === 'text' || child.type === 'image') {
|
|
327
|
-
// runs.push(this.nodeToRun(child));
|
|
328
|
-
// } else if (child.type === 'paragraph') {
|
|
329
|
-
// runs.push(...this.gatherRuns(child.children));
|
|
330
|
-
// } else if (child.type === 'other' || child.type === 'table' || child.type === 'list') {
|
|
331
|
-
// // recursively gather
|
|
332
|
-
// runs.push(...this.gatherRunsFromAllParagraphs(child.children));
|
|
333
|
-
// }
|
|
334
|
-
// }
|
|
335
|
-
// return runs;
|
|
336
|
-
// }
|
|
337
|
-
|
|
338
|
-
// /**
|
|
339
|
-
// * Parses a given node to create a List object.
|
|
340
|
-
// *
|
|
341
|
-
// * @param node - The node to parse, expected to have properties `isOrdered` and `children`.
|
|
342
|
-
// * @returns A List object with the parsed data.
|
|
343
|
-
// */
|
|
344
|
-
// private parseList(node: any): List {
|
|
345
|
-
// const list: List = {
|
|
346
|
-
// type: 'list',
|
|
347
|
-
// isOrdered: node.isOrdered,
|
|
348
|
-
// listItems: [],
|
|
349
|
-
// };
|
|
350
|
-
|
|
351
|
-
// this.parseListChildren(node.children, list);
|
|
352
|
-
// return list;
|
|
353
|
-
// }
|
|
354
|
-
|
|
355
|
-
// /**
|
|
356
|
-
// * Parses the children of a list and adds them to the provided list object.
|
|
357
|
-
// *
|
|
358
|
-
// * @param children - An array of child nodes to be parsed.
|
|
359
|
-
// * @param list - The list object to which parsed items will be added.
|
|
360
|
-
// * @param level - The current depth level of the list (default is 0).
|
|
361
|
-
// */
|
|
362
|
-
// private parseListChildren(children: any[], list: List, level: number = 0): void {
|
|
363
|
-
// if (level > 10) {
|
|
364
|
-
// // Prevent infinite recursion
|
|
365
|
-
// logger.warn('Maximum list nesting level reached');
|
|
366
|
-
// return;
|
|
367
|
-
// }
|
|
368
|
-
|
|
369
|
-
// for (const child of children || []) {
|
|
370
|
-
// if (child.type === 'other') {
|
|
371
|
-
// const tn = (child.tagName || '').toLowerCase();
|
|
372
|
-
// if (tn === 'li') {
|
|
373
|
-
// // Parse a list item
|
|
374
|
-
// const item = this.parseListItem(child, level);
|
|
375
|
-
// list.listItems.push(item);
|
|
376
|
-
// } else {
|
|
377
|
-
// // Recurse into other nodes
|
|
378
|
-
// this.parseListChildren(child.children, list, level + 1);
|
|
379
|
-
// }
|
|
380
|
-
// } else {
|
|
381
|
-
// //skip
|
|
382
|
-
// continue;
|
|
383
|
-
// }
|
|
384
|
-
// }
|
|
385
|
-
// }
|
|
386
|
-
|
|
387
|
-
// /**
|
|
388
|
-
// * Parses a list item node and converts it into a `ListItem` object.
|
|
389
|
-
// *
|
|
390
|
-
// * @param liNode - The list item node to parse.
|
|
391
|
-
// * @param level - The nesting level of the list item.
|
|
392
|
-
// * @returns A `ListItem` object containing the parsed list item data.
|
|
393
|
-
// */
|
|
394
|
-
// private parseListItem(liNode: any, level: number): ListItem {
|
|
395
|
-
// const items = this.gatherRunsFromAllParagraphs(liNode.children);
|
|
396
|
-
|
|
397
|
-
// return {
|
|
398
|
-
// Runs: items,
|
|
399
|
-
// level: level,
|
|
400
|
-
// };
|
|
401
|
-
// }
|
|
402
|
-
|
|
403
|
-
// /**
|
|
404
|
-
// * Builds an array of document elements (Paragraph, Table, or List) from an array of RichNode objects.
|
|
405
|
-
// *
|
|
406
|
-
// * @param nodes - An array of RichNode objects to be parsed into document elements.
|
|
407
|
-
// * @returns An array of Paragraph, Table, or List elements.
|
|
408
|
-
// */
|
|
409
|
-
// private buildDocFromNodes(nodes: RichNode[]): Array<Paragraph | Table | List> {
|
|
410
|
-
// const results: Array<Paragraph | Table | List> = [];
|
|
411
|
-
// for (const node of nodes) {
|
|
412
|
-
// try {
|
|
413
|
-
// const items = this.parseNode(node);
|
|
414
|
-
// if (Array.isArray(items)) {
|
|
415
|
-
// results.push(...items);
|
|
416
|
-
// } else if (items) {
|
|
417
|
-
// results.push(items);
|
|
418
|
-
// }
|
|
419
|
-
// } catch (err: any) {
|
|
420
|
-
// logger.error(`Error parsing node: ${err.message}`);
|
|
421
|
-
// // Continue processing other nodes
|
|
422
|
-
// continue;
|
|
423
|
-
// }
|
|
424
|
-
// }
|
|
425
|
-
// return results;
|
|
426
|
-
// }
|
|
420
|
+
const results: Array<Paragraph | Table | List> = [];
|
|
421
|
+
|
|
422
|
+
for (const node of nodes) {
|
|
423
|
+
try {
|
|
424
|
+
const items = this.parseNode(node);
|
|
425
|
+
if (Array.isArray(items)) {
|
|
426
|
+
results.push(...items.filter(Boolean));
|
|
427
|
+
} else if (items) {
|
|
428
|
+
results.push(items);
|
|
429
|
+
}
|
|
430
|
+
} catch (error) {
|
|
431
|
+
logger.error('Error processing node:', error);
|
|
432
|
+
continue;
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// Ensure there's at least one paragraph
|
|
437
|
+
if (results.length === 0) {
|
|
438
|
+
results.push({ type: 'paragraph', runs: [] });
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
return results;
|
|
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
|
+
}
|
|
427
450
|
} //class
|