@elisra-devops/docgen-skins 0.10.10 → 0.10.11

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