officeparser 7.1.0 → 7.2.1
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/README.md +152 -56
- package/dist/OfficeGenerator.d.ts +6 -2
- package/dist/OfficeGenerator.js +30 -9
- package/dist/OfficeParser.d.ts +1 -1
- package/dist/OfficeParser.js +1 -1
- package/dist/cli.d.ts +18 -12
- package/dist/cli.js +255 -81
- package/dist/defaults.js +12 -1
- package/dist/generators/BaseGenerator.d.ts +4 -3
- package/dist/generators/BaseGenerator.js +13 -1
- package/dist/generators/ChunkingGenerator.js +32 -5
- package/dist/generators/CsvGenerator.d.ts +1 -1
- package/dist/generators/HtmlGenerator.d.ts +2 -1
- package/dist/generators/HtmlGenerator.js +481 -42
- package/dist/generators/MarkdownGenerator.d.ts +1 -1
- package/dist/generators/MarkdownGenerator.js +35 -2
- package/dist/generators/PdfGenerator.d.ts +1 -1
- package/dist/generators/PdfGenerator.js +0 -6
- package/dist/generators/RtfGenerator.d.ts +2 -1
- package/dist/generators/RtfGenerator.js +49 -6
- package/dist/generators/TextGenerator.d.ts +1 -1
- package/dist/generators/TextGenerator.js +6 -0
- package/dist/officeparser.browser.d.ts +267 -54
- package/dist/officeparser.browser.iife.js +599 -187
- package/dist/officeparser.browser.mjs +599 -187
- package/dist/parsers/CsvParser.js +1 -1
- package/dist/parsers/ExcelParser.js +63 -19
- package/dist/parsers/HtmlParser.js +10 -1
- package/dist/parsers/MarkdownParser.js +13 -10
- package/dist/parsers/OpenOfficeParser.js +57 -34
- package/dist/parsers/PdfParser.js +28 -3
- package/dist/parsers/PowerPointParser.js +164 -40
- package/dist/parsers/RtfParser.js +28 -24
- package/dist/parsers/WordParser.js +154 -11
- package/dist/sbom.cdx.json +100 -100
- package/dist/types.d.ts +268 -53
- package/dist/types.js +4 -0
- package/dist/utils/astUtils.d.ts +2 -2
- package/dist/utils/astUtils.js +2 -1
- package/dist/utils/configUtils.d.ts +5 -0
- package/dist/utils/configUtils.js +55 -1
- package/dist/utils/errorUtils.js +3 -1
- package/dist/utils/moduleLoader.js +55 -11
- package/dist/utils/xmlUtils.d.ts +9 -0
- package/dist/utils/xmlUtils.js +53 -1
- package/package.json +6 -3
package/dist/types.d.ts
CHANGED
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
export declare enum OfficeErrorType {
|
|
6
6
|
/** Unsupported file extension */
|
|
7
7
|
EXTENSION_UNSUPPORTED = "EXTENSION_UNSUPPORTED",
|
|
8
|
+
/** Unsupported output generator format */
|
|
9
|
+
FORMAT_UNSUPPORTED = "FORMAT_UNSUPPORTED",
|
|
8
10
|
/** File appears to be corrupted or malformed */
|
|
9
11
|
FILE_CORRUPTED = "FILE_CORRUPTED",
|
|
10
12
|
/** File could not be found at the specified path */
|
|
@@ -68,7 +70,9 @@ export declare enum OfficeWarningType {
|
|
|
68
70
|
/** No chunks were generated for the document given the current strategy */
|
|
69
71
|
EMPTY_CHUNK_GENERATED = "EMPTY_CHUNK_GENERATED",
|
|
70
72
|
/** A node was skipped because it only contained whitespace */
|
|
71
|
-
WHITESPACE_NODE_SKIPPED = "WHITESPACE_NODE_SKIPPED"
|
|
73
|
+
WHITESPACE_NODE_SKIPPED = "WHITESPACE_NODE_SKIPPED",
|
|
74
|
+
/** The HTML generator containerWidth option is invalid */
|
|
75
|
+
INVALID_CONTAINER_WIDTH = "INVALID_CONTAINER_WIDTH"
|
|
72
76
|
}
|
|
73
77
|
/**
|
|
74
78
|
* Consolidated timeout settings for OCR operations.
|
|
@@ -221,10 +225,23 @@ export interface OfficeParserConfig {
|
|
|
221
225
|
*/
|
|
222
226
|
ignoreNotes?: boolean;
|
|
223
227
|
/**
|
|
224
|
-
* Flag
|
|
225
|
-
* Default is false.
|
|
226
|
-
|
|
227
|
-
|
|
228
|
+
* Flag to ignore comments from parsing.
|
|
229
|
+
* Default is false.
|
|
230
|
+
*/
|
|
231
|
+
ignoreComments?: boolean;
|
|
232
|
+
/**
|
|
233
|
+
* Flag to ignore headers and footers from parsing.
|
|
234
|
+
* Default is false.
|
|
235
|
+
*/
|
|
236
|
+
ignoreHeadersAndFooters?: boolean;
|
|
237
|
+
/**
|
|
238
|
+
* Flag to ignore slide masters from parsing in PowerPoint.
|
|
239
|
+
* Default is false.
|
|
240
|
+
*/
|
|
241
|
+
ignoreSlideMasters?: boolean;
|
|
242
|
+
/**
|
|
243
|
+
* @deprecated Notes are now structurally attached to the specific nodes they belong to via `node.notes`.
|
|
244
|
+
* This option is now completely ignored by all parsers.
|
|
228
245
|
*/
|
|
229
246
|
putNotesAtLast?: boolean;
|
|
230
247
|
/**
|
|
@@ -354,9 +371,10 @@ export interface OfficeIssue {
|
|
|
354
371
|
/**
|
|
355
372
|
* The result of a document conversion operation.
|
|
356
373
|
*/
|
|
357
|
-
|
|
374
|
+
type ConversionValue<D extends UniversalGeneratorFormat> = D extends 'pdf' ? Uint8Array | string : D extends 'chunks' ? OfficeChunk[] : D extends 'csv' ? string | Uint8Array : string;
|
|
375
|
+
export interface ConversionResult<D extends UniversalGeneratorFormat> {
|
|
358
376
|
/** The actual generated content (HTML, Markdown, Text, OfficeChunk[], etc.). */
|
|
359
|
-
value: D
|
|
377
|
+
value: ConversionValue<D>;
|
|
360
378
|
/** A collection of issues (warnings/infos) generated during the process. */
|
|
361
379
|
messages: OfficeIssue[];
|
|
362
380
|
}
|
|
@@ -384,7 +402,7 @@ export interface CommonGeneratorConfig {
|
|
|
384
402
|
* 1. **Filter/Remove Nodes**: Return `false` to skip a node and all its children.
|
|
385
403
|
* 2. **Override Rendering**: Return a `string` to use that exact text as the output, bypassing default logic and recursion.
|
|
386
404
|
* 3. **Mutate Nodes**: Modify the `node` object directly (e.g., changing `node.text`) and return `void` to let the generator proceed with your changes.
|
|
387
|
-
* 4. **Async Support**: The callback can be `async`, allowing you to
|
|
405
|
+
* 4. **Async Support**: The callback can be `async`, allowing you to load external data or perform complex logic during generation.
|
|
388
406
|
*/
|
|
389
407
|
onNode?: (node: OfficeContentNode) => string | false | Promise<string | false | void> | void;
|
|
390
408
|
/**
|
|
@@ -478,17 +496,31 @@ export interface CommonGeneratorConfig {
|
|
|
478
496
|
* Restricts format-specific configurations to their respective destinations.
|
|
479
497
|
*/
|
|
480
498
|
/**
|
|
481
|
-
*
|
|
499
|
+
* Maps a destination format string to its corresponding specific configuration object type.
|
|
482
500
|
*/
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
}
|
|
501
|
+
type GeneratorSpecificConfig<D extends string> = D extends 'html' ? {
|
|
502
|
+
htmlConfig?: HtmlGeneratorConfig;
|
|
503
|
+
} : D extends 'md' ? {
|
|
504
|
+
mdConfig?: MdGeneratorConfig;
|
|
505
|
+
} : D extends 'pdf' ? {
|
|
506
|
+
pdfConfig?: PdfGeneratorConfig;
|
|
507
|
+
} : D extends 'csv' ? {
|
|
508
|
+
csvConfig?: CsvGeneratorConfig;
|
|
509
|
+
} : D extends 'text' ? {
|
|
510
|
+
textConfig?: TextGeneratorConfig;
|
|
511
|
+
} : D extends 'rtf' ? {
|
|
512
|
+
rtfConfig?: RtfGeneratorConfig;
|
|
513
|
+
} : D extends 'chunks' ? {
|
|
514
|
+
chunksConfig?: ChunkingConfig;
|
|
515
|
+
} : Partial<{
|
|
516
|
+
htmlConfig: HtmlGeneratorConfig;
|
|
517
|
+
mdConfig: MdGeneratorConfig;
|
|
518
|
+
pdfConfig: PdfGeneratorConfig;
|
|
519
|
+
csvConfig: CsvGeneratorConfig;
|
|
520
|
+
textConfig: TextGeneratorConfig;
|
|
521
|
+
rtfConfig: RtfGeneratorConfig;
|
|
522
|
+
chunksConfig: ChunkingConfig;
|
|
523
|
+
}>;
|
|
492
524
|
/**
|
|
493
525
|
* Configuration options for document generators.
|
|
494
526
|
*
|
|
@@ -499,9 +531,7 @@ export interface GeneratorSubConfigMap {
|
|
|
499
531
|
*
|
|
500
532
|
* @template D The destination format string. Defaults to `string` for a general configuration.
|
|
501
533
|
*/
|
|
502
|
-
export type GeneratorConfig<D extends string = string> = CommonGeneratorConfig &
|
|
503
|
-
[K in keyof GeneratorSubConfigMap as `${K & string}Config`]?: string extends D ? GeneratorSubConfigMap[K] : (D extends K ? GeneratorSubConfigMap[K] : never);
|
|
504
|
-
};
|
|
534
|
+
export type GeneratorConfig<D extends string = string> = CommonGeneratorConfig & GeneratorSpecificConfig<D>;
|
|
505
535
|
/**
|
|
506
536
|
* Configuration options for the OfficeConverter.
|
|
507
537
|
* Combines relevant parser and generator settings for a seamless one-step conversion.
|
|
@@ -546,10 +576,28 @@ export type DeepRequired<T> = T extends Function | Date | Buffer | RegExp ? T :
|
|
|
546
576
|
* it is a discriminated union whose members cannot be uniformly deep-required.
|
|
547
577
|
*/
|
|
548
578
|
export type FullGeneratorConfig = DeepRequired<CommonGeneratorConfig & {
|
|
549
|
-
|
|
579
|
+
htmlConfig: HtmlGeneratorConfig;
|
|
580
|
+
mdConfig: MdGeneratorConfig;
|
|
581
|
+
pdfConfig: PdfGeneratorConfig;
|
|
582
|
+
csvConfig: CsvGeneratorConfig;
|
|
583
|
+
textConfig: TextGeneratorConfig;
|
|
584
|
+
rtfConfig: RtfGeneratorConfig;
|
|
550
585
|
}> & {
|
|
551
586
|
chunksConfig: ChunkingConfig;
|
|
552
587
|
};
|
|
588
|
+
/**
|
|
589
|
+
* Configuration options for granular raw HTML injections.
|
|
590
|
+
*/
|
|
591
|
+
export interface HtmlInjectionConfig {
|
|
592
|
+
/** Raw HTML injected immediately after the opening <head> tag */
|
|
593
|
+
headStart?: string;
|
|
594
|
+
/** Raw HTML injected immediately before the closing </head> tag */
|
|
595
|
+
headEnd?: string;
|
|
596
|
+
/** Raw HTML injected immediately after the opening <body> tag */
|
|
597
|
+
bodyStart?: string;
|
|
598
|
+
/** Raw HTML injected immediately before the closing </body> tag */
|
|
599
|
+
bodyEnd?: string;
|
|
600
|
+
}
|
|
553
601
|
/**
|
|
554
602
|
* Configuration options for HTML generation.
|
|
555
603
|
*/
|
|
@@ -564,6 +612,25 @@ export interface HtmlGeneratorConfig {
|
|
|
564
612
|
* Defaults to 'https://cdn.jsdelivr.net/npm/chart.js'.
|
|
565
613
|
*/
|
|
566
614
|
chartJsSrc?: string;
|
|
615
|
+
/**
|
|
616
|
+
* Custom container width for the generated HTML.
|
|
617
|
+
* Can be a number (pixels) or string (e.g., '900px', '100%').
|
|
618
|
+
* If not specified or set to 'auto', it defaults based on the content type:
|
|
619
|
+
* - Spreadsheet: '100%'
|
|
620
|
+
* - Presentation/Slides: '1100px'
|
|
621
|
+
* - Standard Document (PDF/DOCX/RTF/etc.): '900px'
|
|
622
|
+
*/
|
|
623
|
+
containerWidth?: string | number;
|
|
624
|
+
/**
|
|
625
|
+
* Custom CSS to append to the generated HTML document.
|
|
626
|
+
* This CSS will be included in the `<style>` block and can be used to style
|
|
627
|
+
* custom classes added during AST manipulation or override default styles.
|
|
628
|
+
*/
|
|
629
|
+
customCss?: string;
|
|
630
|
+
/**
|
|
631
|
+
* Granular injection points for custom HTML, scripts, and styles.
|
|
632
|
+
*/
|
|
633
|
+
injections?: HtmlInjectionConfig;
|
|
567
634
|
}
|
|
568
635
|
/**
|
|
569
636
|
* Configuration options for PDF generation.
|
|
@@ -639,7 +706,7 @@ export interface StructuredStyleMapping {
|
|
|
639
706
|
* The structural type of the node (e.g., 'paragraph', 'heading', 'text').
|
|
640
707
|
* Most style mappings target 'paragraph' nodes to convert them into headers or blocks.
|
|
641
708
|
*/
|
|
642
|
-
nodeType?:
|
|
709
|
+
nodeType?: OfficeContentNodeType;
|
|
643
710
|
/**
|
|
644
711
|
* A dictionary of attributes to match on the node.
|
|
645
712
|
*
|
|
@@ -705,7 +772,7 @@ export interface CsvGeneratorConfig {
|
|
|
705
772
|
/**
|
|
706
773
|
* Whether to merge all selected sheets into a single CSV.
|
|
707
774
|
* If false, returns a ZIP archive containing individual CSV files.
|
|
708
|
-
* Defaults to
|
|
775
|
+
* Defaults to true.
|
|
709
776
|
*/
|
|
710
777
|
mergeSheets?: boolean;
|
|
711
778
|
/**
|
|
@@ -964,11 +1031,16 @@ export type SupportedFileType = 'docx' | 'pptx' | 'xlsx' | 'odt' | 'odp' | 'ods'
|
|
|
964
1031
|
/**
|
|
965
1032
|
* Types of content nodes in the AST.
|
|
966
1033
|
*/
|
|
967
|
-
export type OfficeContentNodeType = 'paragraph' | 'heading' | 'table' | 'list' | 'text' | 'image' | 'chart' | 'drawing' | 'slide' | 'note' | 'sheet' | 'row' | 'cell' | 'page' | 'break' | 'code' | 'comment';
|
|
1034
|
+
export type OfficeContentNodeType = 'paragraph' | 'heading' | 'table' | 'list' | 'text' | 'image' | 'chart' | 'drawing' | 'slide' | 'note' | 'sheet' | 'row' | 'cell' | 'page' | 'break' | 'code' | 'comment' | 'header' | 'footer' | 'slideMaster';
|
|
968
1035
|
/**
|
|
969
1036
|
* Supported MIME types for attachments.
|
|
970
1037
|
*/
|
|
971
1038
|
export type OfficeMimeType = 'image/jpeg' | 'image/png' | 'image/gif' | 'image/bmp' | 'image/tiff' | 'image/svg+xml' | 'application/pdf' | 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' | 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' | 'application/vnd.openxmlformats-officedocument.presentationml.presentation' | 'application/vnd.oasis.opendocument.chart' | 'application/vnd.oasis.opendocument.spreadsheet' | 'application/vnd.oasis.opendocument.text' | 'application/vnd.oasis.opendocument.presentation' | 'application/rtf' | 'text/csv' | 'text/markdown' | 'text/html';
|
|
1039
|
+
/**
|
|
1040
|
+
* Text alignment options.
|
|
1041
|
+
* Common in spreadsheet cells, paragraph styles, and text elements.
|
|
1042
|
+
*/
|
|
1043
|
+
export type TextAlignment = 'left' | 'center' | 'right' | 'justify';
|
|
972
1044
|
/**
|
|
973
1045
|
* Text formatting options available for text content.
|
|
974
1046
|
* Represents common formatting attributes found in office documents (DOCX, RTF, PPTX, etc.).
|
|
@@ -1042,7 +1114,7 @@ export interface TextFormatting {
|
|
|
1042
1114
|
* Common in spreadsheet cells or paragraph styles.
|
|
1043
1115
|
* @example "center", "right"
|
|
1044
1116
|
*/
|
|
1045
|
-
alignment?:
|
|
1117
|
+
alignment?: TextAlignment;
|
|
1046
1118
|
}
|
|
1047
1119
|
/**
|
|
1048
1120
|
* Metadata for a slide in PowerPoint.
|
|
@@ -1092,7 +1164,7 @@ export interface HeadingMetadata {
|
|
|
1092
1164
|
/** The heading level (e.g., 1 for H1). */
|
|
1093
1165
|
level: number;
|
|
1094
1166
|
/** The alignment of the heading. */
|
|
1095
|
-
alignment?:
|
|
1167
|
+
alignment?: TextAlignment;
|
|
1096
1168
|
/** The style of the heading. */
|
|
1097
1169
|
style?: string;
|
|
1098
1170
|
/** Detailed indentation information. */
|
|
@@ -1105,7 +1177,7 @@ export interface HeadingMetadata {
|
|
|
1105
1177
|
*/
|
|
1106
1178
|
export interface ParagraphMetadata {
|
|
1107
1179
|
/** The alignment of the paragraph. */
|
|
1108
|
-
alignment?:
|
|
1180
|
+
alignment?: TextAlignment;
|
|
1109
1181
|
/** The style of the paragraph. */
|
|
1110
1182
|
style?: string;
|
|
1111
1183
|
/** Detailed indentation information. */
|
|
@@ -1133,7 +1205,7 @@ export interface ListMetadata {
|
|
|
1133
1205
|
* Text alignment of the list item.
|
|
1134
1206
|
* @example 'left', 'center', 'right', 'justify'
|
|
1135
1207
|
*/
|
|
1136
|
-
alignment:
|
|
1208
|
+
alignment: TextAlignment;
|
|
1137
1209
|
/**
|
|
1138
1210
|
* The list ID from the Word document's numbering definition.
|
|
1139
1211
|
* Used to identify which list definition this item belongs to.
|
|
@@ -1183,6 +1255,15 @@ export interface CellMetadata {
|
|
|
1183
1255
|
style?: string;
|
|
1184
1256
|
/** Unique anchor IDs for internal linking. */
|
|
1185
1257
|
anchorIds?: string[];
|
|
1258
|
+
/** Background color for this cell in hex format (e.g. #FFFFFF). */
|
|
1259
|
+
backgroundColor?: string;
|
|
1260
|
+
}
|
|
1261
|
+
/**
|
|
1262
|
+
* Metadata for a table.
|
|
1263
|
+
*/
|
|
1264
|
+
export interface TableMetadata {
|
|
1265
|
+
/** Unique anchor IDs for internal linking. */
|
|
1266
|
+
anchorIds?: string[];
|
|
1186
1267
|
}
|
|
1187
1268
|
/**
|
|
1188
1269
|
* Metadata for a chart node in the document.
|
|
@@ -1270,6 +1351,8 @@ export interface NoteMetadata {
|
|
|
1270
1351
|
noteId?: string;
|
|
1271
1352
|
/** Unique anchor IDs for internal linking. */
|
|
1272
1353
|
anchorIds?: string[];
|
|
1354
|
+
/** The slide number this note is associated with (used in PowerPoint). */
|
|
1355
|
+
slideNumber?: number;
|
|
1273
1356
|
}
|
|
1274
1357
|
/**
|
|
1275
1358
|
* Metadata for break nodes.
|
|
@@ -1305,10 +1388,25 @@ export interface CodeMetadata {
|
|
|
1305
1388
|
/** Unique anchor IDs for internal linking. */
|
|
1306
1389
|
anchorIds?: string[];
|
|
1307
1390
|
}
|
|
1391
|
+
/**
|
|
1392
|
+
* Metadata for a comment/annotation.
|
|
1393
|
+
*/
|
|
1394
|
+
export interface CommentMetadata {
|
|
1395
|
+
author?: string;
|
|
1396
|
+
initials?: string;
|
|
1397
|
+
date?: string;
|
|
1398
|
+
commentId?: string;
|
|
1399
|
+
}
|
|
1400
|
+
/**
|
|
1401
|
+
* Metadata for a header or footer.
|
|
1402
|
+
*/
|
|
1403
|
+
export interface HeaderFooterMetadata {
|
|
1404
|
+
type: 'default' | 'first' | 'even' | string;
|
|
1405
|
+
}
|
|
1308
1406
|
/**
|
|
1309
1407
|
* Union type for content metadata.
|
|
1310
1408
|
*/
|
|
1311
|
-
export type ContentMetadata = SlideMetadata | SheetMetadata | HeadingMetadata | ListMetadata | CellMetadata | ImageMetadata | ChartMetadata | PageMetadata | ParagraphMetadata | TextMetadata | NoteMetadata | BreakMetadata | CodeMetadata | undefined;
|
|
1409
|
+
export type ContentMetadata = SlideMetadata | SheetMetadata | HeadingMetadata | ListMetadata | CellMetadata | ImageMetadata | ChartMetadata | PageMetadata | ParagraphMetadata | TextMetadata | NoteMetadata | BreakMetadata | CodeMetadata | CommentMetadata | HeaderFooterMetadata | TableMetadata | undefined;
|
|
1312
1410
|
/**
|
|
1313
1411
|
* Represents a node in the document content tree.
|
|
1314
1412
|
* This is the core building block of the parsed document structure.
|
|
@@ -1335,13 +1433,10 @@ export type ContentMetadata = SlideMetadata | SheetMetadata | HeadingMetadata |
|
|
|
1335
1433
|
* children: [...]
|
|
1336
1434
|
* }
|
|
1337
1435
|
*/
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
* Common types: 'paragraph', 'heading', 'table', 'list', 'text', 'image', etc.
|
|
1343
|
-
*/
|
|
1344
|
-
type: OfficeContentNodeType;
|
|
1436
|
+
/**
|
|
1437
|
+
* Shared properties available on all document content nodes.
|
|
1438
|
+
*/
|
|
1439
|
+
export interface BaseContentNode {
|
|
1345
1440
|
/**
|
|
1346
1441
|
* The complete text content of the node and all its children combined.
|
|
1347
1442
|
* For container nodes (paragraph, heading), this is the concatenation of all child text.
|
|
@@ -1359,6 +1454,16 @@ export interface OfficeContentNode {
|
|
|
1359
1454
|
* @example [{ type: 'text', text: 'Hello', formatting: { bold: true } }]
|
|
1360
1455
|
*/
|
|
1361
1456
|
children?: OfficeContentNode[];
|
|
1457
|
+
/**
|
|
1458
|
+
* Comments attached to this specific node.
|
|
1459
|
+
* Keeps annotations completely separate from the actual content flow.
|
|
1460
|
+
*/
|
|
1461
|
+
comments?: OfficeContentNode[];
|
|
1462
|
+
/**
|
|
1463
|
+
* Notes (like footnotes or slide notes) attached to this specific node.
|
|
1464
|
+
* Keeps notes separate from the actual structural children.
|
|
1465
|
+
*/
|
|
1466
|
+
notes?: OfficeContentNode[];
|
|
1362
1467
|
/**
|
|
1363
1468
|
* Text formatting applied to this node.
|
|
1364
1469
|
* Only applicable to text-containing nodes.
|
|
@@ -1366,16 +1471,6 @@ export interface OfficeContentNode {
|
|
|
1366
1471
|
* @example { bold: true, size: "12", font: "Arial" }
|
|
1367
1472
|
*/
|
|
1368
1473
|
formatting?: TextFormatting;
|
|
1369
|
-
/**
|
|
1370
|
-
* Type-specific metadata providing additional context about the node.
|
|
1371
|
-
* The metadata structure depends on the node type:
|
|
1372
|
-
* - Headings: { level: 1 }
|
|
1373
|
-
* - Lists: { listType: 'ordered', indentation: 0 }
|
|
1374
|
-
* - Cells: { row: 0, col: 0 }
|
|
1375
|
-
* - Slides: { slideNumber: 1 }
|
|
1376
|
-
* @example { level: 1 } for a heading
|
|
1377
|
-
*/
|
|
1378
|
-
metadata?: ContentMetadata;
|
|
1379
1474
|
/**
|
|
1380
1475
|
* The raw source content for this node.
|
|
1381
1476
|
* - For XML-based formats (DOCX, XLSX, PPTX): contains the raw XML
|
|
@@ -1387,6 +1482,93 @@ export interface OfficeContentNode {
|
|
|
1387
1482
|
*/
|
|
1388
1483
|
rawContent?: string;
|
|
1389
1484
|
}
|
|
1485
|
+
/**
|
|
1486
|
+
* Represents a node in the document content tree.
|
|
1487
|
+
* This is the core building block of the parsed document structure.
|
|
1488
|
+
* Content nodes can be nested to represent hierarchical document structures
|
|
1489
|
+
* (e.g., paragraphs containing text runs, tables containing rows, rows containing cells).
|
|
1490
|
+
*
|
|
1491
|
+
* @example
|
|
1492
|
+
* // A simple paragraph with formatted text
|
|
1493
|
+
* {
|
|
1494
|
+
* type: 'paragraph',
|
|
1495
|
+
* text: 'Hello world',
|
|
1496
|
+
* children: [
|
|
1497
|
+
* { type: 'text', text: 'Hello ', formatting: { bold: true } },
|
|
1498
|
+
* { type: 'text', text: 'world', formatting: { italic: true } }
|
|
1499
|
+
* ]
|
|
1500
|
+
* }
|
|
1501
|
+
*
|
|
1502
|
+
* @example
|
|
1503
|
+
* // A heading with metadata
|
|
1504
|
+
* {
|
|
1505
|
+
* type: 'heading',
|
|
1506
|
+
* text: 'Chapter 1',
|
|
1507
|
+
* metadata: { level: 1 },
|
|
1508
|
+
* children: [...]
|
|
1509
|
+
* }
|
|
1510
|
+
*/
|
|
1511
|
+
export type OfficeContentNode = BaseContentNode & ({
|
|
1512
|
+
type: 'slide';
|
|
1513
|
+
metadata?: SlideMetadata;
|
|
1514
|
+
} | {
|
|
1515
|
+
type: 'sheet';
|
|
1516
|
+
metadata?: SheetMetadata;
|
|
1517
|
+
} | {
|
|
1518
|
+
type: 'heading';
|
|
1519
|
+
metadata?: HeadingMetadata;
|
|
1520
|
+
} | {
|
|
1521
|
+
type: 'list';
|
|
1522
|
+
metadata?: ListMetadata;
|
|
1523
|
+
} | {
|
|
1524
|
+
type: 'cell';
|
|
1525
|
+
metadata?: CellMetadata;
|
|
1526
|
+
} | {
|
|
1527
|
+
type: 'image';
|
|
1528
|
+
metadata?: ImageMetadata;
|
|
1529
|
+
} | {
|
|
1530
|
+
type: 'chart';
|
|
1531
|
+
metadata?: ChartMetadata;
|
|
1532
|
+
} | {
|
|
1533
|
+
type: 'page';
|
|
1534
|
+
metadata?: PageMetadata;
|
|
1535
|
+
} | {
|
|
1536
|
+
type: 'paragraph';
|
|
1537
|
+
metadata?: ParagraphMetadata;
|
|
1538
|
+
} | {
|
|
1539
|
+
type: 'text';
|
|
1540
|
+
metadata?: TextMetadata;
|
|
1541
|
+
} | {
|
|
1542
|
+
type: 'note';
|
|
1543
|
+
metadata?: NoteMetadata;
|
|
1544
|
+
} | {
|
|
1545
|
+
type: 'break';
|
|
1546
|
+
metadata?: BreakMetadata;
|
|
1547
|
+
} | {
|
|
1548
|
+
type: 'code';
|
|
1549
|
+
metadata?: CodeMetadata;
|
|
1550
|
+
} | {
|
|
1551
|
+
type: 'comment';
|
|
1552
|
+
metadata?: CommentMetadata;
|
|
1553
|
+
} | {
|
|
1554
|
+
type: 'header';
|
|
1555
|
+
metadata?: HeaderFooterMetadata;
|
|
1556
|
+
} | {
|
|
1557
|
+
type: 'footer';
|
|
1558
|
+
metadata?: HeaderFooterMetadata;
|
|
1559
|
+
} | {
|
|
1560
|
+
type: 'table';
|
|
1561
|
+
metadata?: TableMetadata;
|
|
1562
|
+
} | {
|
|
1563
|
+
type: 'row';
|
|
1564
|
+
metadata?: undefined;
|
|
1565
|
+
} | {
|
|
1566
|
+
type: 'drawing';
|
|
1567
|
+
metadata?: undefined;
|
|
1568
|
+
} | {
|
|
1569
|
+
type: 'slideMaster';
|
|
1570
|
+
metadata?: SlideMetadata;
|
|
1571
|
+
});
|
|
1390
1572
|
/**
|
|
1391
1573
|
* Structured information extracted from a chart.
|
|
1392
1574
|
*/
|
|
@@ -1526,14 +1708,40 @@ export interface OfficeMetadata {
|
|
|
1526
1708
|
* Values are typed as string, number, boolean, or Date where the source format provides type information.
|
|
1527
1709
|
*/
|
|
1528
1710
|
customProperties?: Record<string, string | number | boolean | Date>;
|
|
1711
|
+
/** Keywords associated with the document. */
|
|
1712
|
+
keywords?: string;
|
|
1713
|
+
/**
|
|
1714
|
+
* Contains all format-specific metadata fields extracted verbatim.
|
|
1715
|
+
* Consumers can use this to access properties not mapped to the standard OfficeMetadata fields.
|
|
1716
|
+
* Examples: all <meta> tags in HTML, app.xml properties in DOCX, XMP dicts in PDF.
|
|
1717
|
+
*/
|
|
1718
|
+
nativeProperties?: Record<string, any>;
|
|
1719
|
+
}
|
|
1720
|
+
/**
|
|
1721
|
+
* Contains out-of-band layout elements and templates that are not part of the main document flow.
|
|
1722
|
+
*/
|
|
1723
|
+
export interface OfficeAuxiliaryContent {
|
|
1724
|
+
/** Headers extracted from the document. */
|
|
1725
|
+
headers?: OfficeContentNode[];
|
|
1726
|
+
/** Footers extracted from the document. */
|
|
1727
|
+
footers?: OfficeContentNode[];
|
|
1728
|
+
/** Slide Masters extracted from presentations. */
|
|
1729
|
+
slideMasters?: OfficeContentNode[];
|
|
1529
1730
|
}
|
|
1530
1731
|
/**
|
|
1531
|
-
* The Abstract Syntax Tree (AST)
|
|
1532
|
-
* This is the
|
|
1732
|
+
* The Root Abstract Syntax Tree (AST) representing a parsed Office Document.
|
|
1733
|
+
* This is the ultimate output of `OfficeParser.parseOffice()`.
|
|
1734
|
+
*
|
|
1735
|
+
* DESIGN PHILOSOPHY:
|
|
1736
|
+
* The AST is designed to be a universal, format-agnostic representation of document content.
|
|
1737
|
+
* Whether the input was a PDF, DOCX, XLSX, Markdown, or HTML file, the resulting AST
|
|
1738
|
+
* uses the same consistent structure (`OfficeContentNode` trees).
|
|
1533
1739
|
*
|
|
1534
|
-
*
|
|
1535
|
-
*
|
|
1536
|
-
*
|
|
1740
|
+
* ### Key Top-Level Properties:
|
|
1741
|
+
* - `metadata`: Document-level properties (author, title, stats).
|
|
1742
|
+
* - `content`: The main sequential flow of the document (paragraphs, tables, slides, sheets).
|
|
1743
|
+
* - `attachments`: Extracted binary assets (images, embedded files).
|
|
1744
|
+
* - `auxiliary`: Out-of-band layout/template elements (headers, footers, slide masters).
|
|
1537
1745
|
*
|
|
1538
1746
|
* @example
|
|
1539
1747
|
* ```typescript
|
|
@@ -1583,6 +1791,12 @@ export interface OfficeParserAST {
|
|
|
1583
1791
|
* @example [{ type: 'paragraph', text: 'Hello' }, { type: 'heading', text: 'Chapter 1' }]
|
|
1584
1792
|
*/
|
|
1585
1793
|
content: OfficeContentNode[];
|
|
1794
|
+
/**
|
|
1795
|
+
* Out-of-band layout and template elements that are not part of the main text flow.
|
|
1796
|
+
* Extracted only if the respective `ignore...` config flags are false.
|
|
1797
|
+
* Contains elements like `headers`, `footers`, and `slideMasters`.
|
|
1798
|
+
*/
|
|
1799
|
+
auxiliary?: OfficeAuxiliaryContent;
|
|
1586
1800
|
/**
|
|
1587
1801
|
* Attachments extracted from the document (images, charts, embedded files).
|
|
1588
1802
|
* Only populated when `config.extractAttachments` is true.
|
|
@@ -1626,5 +1840,6 @@ export interface OfficeParserAST {
|
|
|
1626
1840
|
* const md = await ast.to('md');
|
|
1627
1841
|
* ```
|
|
1628
1842
|
*/
|
|
1629
|
-
to<T extends this, D extends SupportedDestination<T['type']>>(this: T, destination: D, config?: GeneratorConfig<D>): Promise<ConversionResult
|
|
1843
|
+
to<T extends this, D extends SupportedDestination<T['type']>>(this: T, destination: D, config?: GeneratorConfig<D>): Promise<ConversionResult<D>>;
|
|
1630
1844
|
}
|
|
1845
|
+
export {};
|
package/dist/types.js
CHANGED
|
@@ -9,6 +9,8 @@ var OfficeErrorType;
|
|
|
9
9
|
(function (OfficeErrorType) {
|
|
10
10
|
/** Unsupported file extension */
|
|
11
11
|
OfficeErrorType["EXTENSION_UNSUPPORTED"] = "EXTENSION_UNSUPPORTED";
|
|
12
|
+
/** Unsupported output generator format */
|
|
13
|
+
OfficeErrorType["FORMAT_UNSUPPORTED"] = "FORMAT_UNSUPPORTED";
|
|
12
14
|
/** File appears to be corrupted or malformed */
|
|
13
15
|
OfficeErrorType["FILE_CORRUPTED"] = "FILE_CORRUPTED";
|
|
14
16
|
/** File could not be found at the specified path */
|
|
@@ -74,4 +76,6 @@ var OfficeWarningType;
|
|
|
74
76
|
OfficeWarningType["EMPTY_CHUNK_GENERATED"] = "EMPTY_CHUNK_GENERATED";
|
|
75
77
|
/** A node was skipped because it only contained whitespace */
|
|
76
78
|
OfficeWarningType["WHITESPACE_NODE_SKIPPED"] = "WHITESPACE_NODE_SKIPPED";
|
|
79
|
+
/** The HTML generator containerWidth option is invalid */
|
|
80
|
+
OfficeWarningType["INVALID_CONTAINER_WIDTH"] = "INVALID_CONTAINER_WIDTH";
|
|
77
81
|
})(OfficeWarningType || (exports.OfficeWarningType = OfficeWarningType = {}));
|
package/dist/utils/astUtils.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { OfficeAttachment, OfficeAuxiliaryContent, OfficeContentNode, OfficeMetadata, OfficeParserAST, OfficeParserConfig, SupportedFileType } from '../types.js';
|
|
2
2
|
/**
|
|
3
3
|
* Creates a fully-featured OfficeParserAST object with conversion methods.
|
|
4
4
|
*
|
|
@@ -13,4 +13,4 @@ import { OfficeParserAST, OfficeContentNode, OfficeMetadata, OfficeAttachment, S
|
|
|
13
13
|
* @param toTextSync - Synchronous text extraction logic (for backward compatibility)
|
|
14
14
|
* @returns An object conforming to OfficeParserAST
|
|
15
15
|
*/
|
|
16
|
-
export declare function createAST(type: SupportedFileType, metadata: OfficeMetadata, content: OfficeContentNode[], attachments: OfficeAttachment[], config: OfficeParserConfig, toTextSync: () => string): OfficeParserAST;
|
|
16
|
+
export declare function createAST(type: SupportedFileType, metadata: OfficeMetadata, content: OfficeContentNode[], attachments: OfficeAttachment[], config: OfficeParserConfig, auxiliary: OfficeAuxiliaryContent | undefined, toTextSync: () => string): OfficeParserAST;
|
package/dist/utils/astUtils.js
CHANGED
|
@@ -16,13 +16,14 @@ const OfficeGenerator_js_1 = require("../OfficeGenerator.js");
|
|
|
16
16
|
* @param toTextSync - Synchronous text extraction logic (for backward compatibility)
|
|
17
17
|
* @returns An object conforming to OfficeParserAST
|
|
18
18
|
*/
|
|
19
|
-
function createAST(type, metadata, content, attachments, config, toTextSync) {
|
|
19
|
+
function createAST(type, metadata, content, attachments, config, auxiliary, toTextSync) {
|
|
20
20
|
return {
|
|
21
21
|
config,
|
|
22
22
|
type,
|
|
23
23
|
metadata,
|
|
24
24
|
content,
|
|
25
25
|
attachments,
|
|
26
|
+
auxiliary,
|
|
26
27
|
warnings: [],
|
|
27
28
|
toText: toTextSync,
|
|
28
29
|
async to(destination, genConfig) {
|
|
@@ -24,3 +24,8 @@ export declare function resolveParserConfig(userConfig?: OfficeParserConfig | Fu
|
|
|
24
24
|
* @returns A fully populated configuration object
|
|
25
25
|
*/
|
|
26
26
|
export declare function resolveGeneratorConfig<D extends string>(destination: D, astConfig?: OfficeParserConfig, userConfig?: GeneratorConfig<D> | FullGeneratorConfig): FullGeneratorConfig;
|
|
27
|
+
/**
|
|
28
|
+
* Validates the containerWidth option for HTML generation.
|
|
29
|
+
* Can be 'auto', a positive number, or a positive CSS length/percentage string.
|
|
30
|
+
*/
|
|
31
|
+
export declare function isValidContainerWidth(width: any): boolean;
|
|
@@ -4,7 +4,10 @@ exports.isFullGeneratorConfig = isFullGeneratorConfig;
|
|
|
4
4
|
exports.isFullParserConfig = isFullParserConfig;
|
|
5
5
|
exports.resolveParserConfig = resolveParserConfig;
|
|
6
6
|
exports.resolveGeneratorConfig = resolveGeneratorConfig;
|
|
7
|
+
exports.isValidContainerWidth = isValidContainerWidth;
|
|
7
8
|
const defaults_js_1 = require("../defaults.js");
|
|
9
|
+
const types_js_1 = require("../types.js");
|
|
10
|
+
const errorUtils_js_1 = require("./errorUtils.js");
|
|
8
11
|
/**
|
|
9
12
|
* Deep clones an object, specifically handling arrays and plain objects.
|
|
10
13
|
*/
|
|
@@ -100,6 +103,7 @@ function resolveGeneratorConfig(destination, astConfig, userConfig) {
|
|
|
100
103
|
// If it's already a full config and we don't need to merge AST config, return it as is.
|
|
101
104
|
// We assume FullGeneratorConfig is already "safe" (references resolved).
|
|
102
105
|
if (isFullGeneratorConfig(userConfig) && !astConfig) {
|
|
106
|
+
validateHtmlConfigWidth(userConfig.htmlConfig, userConfig);
|
|
103
107
|
return userConfig;
|
|
104
108
|
}
|
|
105
109
|
// 1. Start with full defaults (deep cloned to avoid reference sharing)
|
|
@@ -115,7 +119,22 @@ function resolveGeneratorConfig(destination, astConfig, userConfig) {
|
|
|
115
119
|
return;
|
|
116
120
|
for (const key in source) {
|
|
117
121
|
if (source[key] !== undefined) {
|
|
118
|
-
|
|
122
|
+
// Deep merge plain objects (like injections or margin)
|
|
123
|
+
if (typeof source[key] === 'object' &&
|
|
124
|
+
source[key] !== null &&
|
|
125
|
+
!Array.isArray(source[key]) &&
|
|
126
|
+
!(source[key] instanceof Function) &&
|
|
127
|
+
!(source[key] instanceof Date) &&
|
|
128
|
+
!(source[key] instanceof RegExp) &&
|
|
129
|
+
!(source[key] instanceof Buffer)) {
|
|
130
|
+
if (!target[key] || typeof target[key] !== 'object') {
|
|
131
|
+
target[key] = {};
|
|
132
|
+
}
|
|
133
|
+
mergeSubConfig(target[key], source[key]);
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
target[key] = source[key];
|
|
137
|
+
}
|
|
119
138
|
}
|
|
120
139
|
}
|
|
121
140
|
};
|
|
@@ -149,5 +168,40 @@ function resolveGeneratorConfig(destination, astConfig, userConfig) {
|
|
|
149
168
|
// Since FullGeneratorConfig doesn't have an 'mdConfig', we rely on the generator implementation.
|
|
150
169
|
}
|
|
151
170
|
}
|
|
171
|
+
validateHtmlConfigWidth(config.htmlConfig, config);
|
|
152
172
|
return config;
|
|
153
173
|
}
|
|
174
|
+
/**
|
|
175
|
+
* Validates the containerWidth option for HTML generation.
|
|
176
|
+
* Can be 'auto', a positive number, or a positive CSS length/percentage string.
|
|
177
|
+
*/
|
|
178
|
+
function isValidContainerWidth(width) {
|
|
179
|
+
if (width === 'auto')
|
|
180
|
+
return true;
|
|
181
|
+
if (typeof width === 'number') {
|
|
182
|
+
return Number.isFinite(width) && width > 0;
|
|
183
|
+
}
|
|
184
|
+
if (typeof width === 'string') {
|
|
185
|
+
const val = width.trim().toLowerCase();
|
|
186
|
+
if (val === 'auto')
|
|
187
|
+
return true;
|
|
188
|
+
const match = val.match(/^((?:\d*\.)?\d+)(px|%|em|rem|vw|vh|vmin|vmax|ch|in|cm|mm|pt|pc)?$/);
|
|
189
|
+
if (!match)
|
|
190
|
+
return false;
|
|
191
|
+
const numericValue = parseFloat(match[1]);
|
|
192
|
+
return numericValue > 0;
|
|
193
|
+
}
|
|
194
|
+
return false;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Emits a warning and falls back to 'auto' if the HTML containerWidth is invalid.
|
|
198
|
+
*/
|
|
199
|
+
function validateHtmlConfigWidth(htmlConfig, config) {
|
|
200
|
+
if (htmlConfig?.containerWidth !== undefined) {
|
|
201
|
+
const width = htmlConfig.containerWidth;
|
|
202
|
+
if (!isValidContainerWidth(width)) {
|
|
203
|
+
(0, errorUtils_js_1.logWarning)(types_js_1.OfficeWarningType.INVALID_CONTAINER_WIDTH, config, width);
|
|
204
|
+
htmlConfig.containerWidth = 'auto';
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
package/dist/utils/errorUtils.js
CHANGED
|
@@ -17,6 +17,7 @@ const ERRORHEADER = "[OfficeParser]: ";
|
|
|
17
17
|
*/
|
|
18
18
|
const ERROR_MESSAGES = {
|
|
19
19
|
[types_js_1.OfficeErrorType.EXTENSION_UNSUPPORTED]: (ext) => `Sorry, OfficeParser currently supports docx, pptx, xlsx, odt, odp, ods, pdf, rtf, md, html, csv files only. Create a ticket in Issues on github to add support for ${ext} files. Stay tuned for further updates.`,
|
|
20
|
+
[types_js_1.OfficeErrorType.FORMAT_UNSUPPORTED]: (format) => `Sorry, OfficeGenerator does not support generating '${format}' files. Supported formats: json, text, md, html, csv, rtf, pdf, chunks.`,
|
|
20
21
|
[types_js_1.OfficeErrorType.FILE_CORRUPTED]: (filepath) => `Your file ${filepath} seems to be corrupted. If you are sure it is fine, please create a ticket in Issues on github with the file to reproduce error.`,
|
|
21
22
|
[types_js_1.OfficeErrorType.FILE_DOES_NOT_EXIST]: (filepath) => `File ${filepath} could not be found! Check if the file exists or verify if the relative path to the file is correct from your terminal's location.`,
|
|
22
23
|
[types_js_1.OfficeErrorType.LOCATION_NOT_FOUND]: (location) => `Entered location ${location} is not reachable! Please make sure that the entered directory location exists. Check relative paths and reenter.`,
|
|
@@ -50,7 +51,8 @@ const WARNING_MESSAGES = {
|
|
|
50
51
|
[types_js_1.OfficeWarningType.BUFFER_TYPE_MISMATCH]: (info) => `File content type mismatch: Detected '${info.detected}' but expected/provided '${info.expected}'. Parsing will proceed with '${info.expected}' as requested.`,
|
|
51
52
|
[types_js_1.OfficeWarningType.FILE_TYPE_DETECTION_FAILED]: `Auto-detection of file type failed. This can happen on older Node.js versions with modern file-type versions. Please provide the 'fileType' hint in the configuration if parsing fails.`,
|
|
52
53
|
[types_js_1.OfficeWarningType.EMPTY_CHUNK_GENERATED]: (strategy) => `No chunks generated for document. Check if the document content is compatible with the '${strategy}' strategy.`,
|
|
53
|
-
[types_js_1.OfficeWarningType.WHITESPACE_NODE_SKIPPED]: (nodeType) => `Skipped whitespace-only node of type: ${nodeType}
|
|
54
|
+
[types_js_1.OfficeWarningType.WHITESPACE_NODE_SKIPPED]: (nodeType) => `Skipped whitespace-only node of type: ${nodeType}`,
|
|
55
|
+
[types_js_1.OfficeWarningType.INVALID_CONTAINER_WIDTH]: (val) => `Invalid HTML containerWidth: ${JSON.stringify(val)}. Falling back to "auto". Width must be a positive number, a valid CSS length string (e.g., "900px", "100%", "50vw"), or "auto".`
|
|
54
56
|
};
|
|
55
57
|
/**
|
|
56
58
|
* Creates a formatted warning message for a specific warning type.
|