officeparser 7.0.0 → 7.0.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 +86 -4
- package/dist/OfficeParser.js +26 -9
- package/dist/officeparser.browser.d.ts +2 -0
- package/dist/officeparser.browser.iife.js +28 -28
- package/dist/officeparser.browser.mjs +28 -28
- package/dist/sbom.cdx.json +98 -98
- package/dist/types.d.ts +2 -0
- package/dist/types.js +2 -0
- package/dist/utils/envUtils.d.ts +8 -3
- package/dist/utils/envUtils.js +113 -84
- package/dist/utils/errorUtils.js +2 -1
- package/dist/utils/moduleLoader.js +4 -2
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -28,6 +28,30 @@ A robust, strictly-typed Node.js and Browser library for parsing and generating
|
|
|
28
28
|
|
|
29
29
|
---
|
|
30
30
|
|
|
31
|
+
## Table of Contents
|
|
32
|
+
- [Install via npm](#install-via-npm)
|
|
33
|
+
- [Command Line Usage](#command-line-usage)
|
|
34
|
+
- [Library Usage](#library-usage)
|
|
35
|
+
- [Using the OfficeGenerator](#using-the-officegenerator)
|
|
36
|
+
- [The New "One-Step" API: OfficeConverter](#the-new-one-step-api-officeconverter)
|
|
37
|
+
- [Native RAG Chunking](#native-rag-chunking)
|
|
38
|
+
- [The AST Structure](#the-ast-structure)
|
|
39
|
+
- [Deep Dive: Document Components](#deep-dive-document-components)
|
|
40
|
+
- [Performance & Fidelity Highlights (v7.0.0)](#performance--fidelity-highlights-v700)
|
|
41
|
+
- [Advanced AST Usage](#advanced-ast-usage)
|
|
42
|
+
- [Configuration Object: OfficeParserConfig](#configuration-object-officeparserconfig)
|
|
43
|
+
- [Generator Configuration: GeneratorConfig](#generator-configuration-generatorconfig)
|
|
44
|
+
- [Format-Specific Generator Configuration](#format-specific-generator-configuration)
|
|
45
|
+
- [One-Step Conversion: OfficeConverterConfig](#one-step-conversion-officeconverterconfig)
|
|
46
|
+
- [Chunking Configuration: ChunkingConfig](#chunking-configuration-chunkingconfig)
|
|
47
|
+
- [OCR Scheduler & Resource Management](#ocr-scheduler--resource-management)
|
|
48
|
+
- [Examples](#examples)
|
|
49
|
+
- [Browser Usage](#browser-usage)
|
|
50
|
+
- [Troubleshooting & Common Issues](#troubleshooting--common-issues)
|
|
51
|
+
- [Known Limitations](#known-limitations)
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
|
|
31
55
|
## Install via npm
|
|
32
56
|
|
|
33
57
|
```bash
|
|
@@ -537,11 +561,23 @@ Configuration options for `OfficeGenerator.generate`.
|
|
|
537
561
|
|
|
538
562
|
| Flag | DataType | Default | Explanation |
|
|
539
563
|
|------|----------|---------|-------------|
|
|
540
|
-
| `includeFormatting` | boolean | `
|
|
541
|
-
| `
|
|
564
|
+
| `includeFormatting` | boolean | `true` | Whether to include semantic styles (bold, italic, colors, sizes) in output. |
|
|
565
|
+
| `generateIds` | boolean | `true` | Automatically generates unique slug-based IDs for heading nodes. |
|
|
566
|
+
| `renderMetadata` | boolean | `false` | Renders document metadata (Title, Author) as a visible header block. |
|
|
567
|
+
| `includeImages` | boolean | `true` | Whether to include image nodes in the generated output. |
|
|
568
|
+
| `includeCharts` | boolean | `true` | Whether to include interactive charts (HTML only). |
|
|
569
|
+
| `ignoreInternalLinks`| boolean | `false` | Suppresses all internal bookmarks and anchor references. |
|
|
542
570
|
| `ignoreDefaultStyleMap`| boolean | `false` | Ignore the library's default style mappings. |
|
|
543
|
-
| `
|
|
544
|
-
| `onNode` | function | `undefined` | Callback to
|
|
571
|
+
| `styleMap` | string[] \| array | `[]` | Array of style mappings (DSL strings or structured objects). |
|
|
572
|
+
| `onNode` | function | `undefined` | Callback to filter, override, or mutate any node during generation. |
|
|
573
|
+
| `onWarning` | function | `undefined` | Callback for generation-phase warnings. |
|
|
574
|
+
| `htmlConfig` | object | `{}` | Format-specific settings for HTML generation. |
|
|
575
|
+
| `mdConfig` | object | `{}` | Format-specific settings for Markdown generation. |
|
|
576
|
+
| `pdfConfig` | object | `{}` | Format-specific settings for PDF generation. |
|
|
577
|
+
| `csvConfig` | object | `{}` | Format-specific settings for CSV generation. |
|
|
578
|
+
| `textConfig` | object | `{}` | Format-specific settings for Plain Text generation. |
|
|
579
|
+
| `rtfConfig` | object | `{}` | Format-specific settings for RTF generation. |
|
|
580
|
+
| `chunksConfig` | object | `(doc-struct)` | Settings for RAG chunking strategies. |
|
|
545
581
|
|
|
546
582
|
### 🛠️ Advanced Node Manipulation (Pro Users)
|
|
547
583
|
The `onNode` callback is a powerful tool that gives you complete control over the generation process. It is called for **every single node** in the AST before it is rendered.
|
|
@@ -608,6 +644,52 @@ The library also maintains support for a simple string-based DSL, highly compati
|
|
|
608
644
|
- **Regex-like Matching**: `"p[style~='Title'] => h2"`
|
|
609
645
|
- **Attribute Filters**: `"p[style-name='Quote'][lang='en'] => blockquote"`
|
|
610
646
|
|
|
647
|
+
## Format-Specific Generator Configuration
|
|
648
|
+
Each destination format has its own specialized sub-configuration object nested within the main `GeneratorConfig`.
|
|
649
|
+
|
|
650
|
+
### 1. HtmlGeneratorConfig (`htmlConfig`)
|
|
651
|
+
| Flag | DataType | Default | Explanation |
|
|
652
|
+
|------|----------|---------|-------------|
|
|
653
|
+
| `standalone` | boolean | `true` | Wraps output in a full `<html>` document with CSS and metadata. |
|
|
654
|
+
| `chartJsSrc` | string | `(CDN)` | URL for the Chart.js library used for interactive charts. |
|
|
655
|
+
|
|
656
|
+
### 2. MdGeneratorConfig (`mdConfig`)
|
|
657
|
+
| Flag | DataType | Default | Explanation |
|
|
658
|
+
|------|----------|---------|-------------|
|
|
659
|
+
| `fallbackToHtml` | boolean | `true` | Uses HTML tags for features not supported by Markdown (underlines, complex tables). |
|
|
660
|
+
|
|
661
|
+
### 3. PdfGeneratorConfig (`pdfConfig`)
|
|
662
|
+
| Flag | DataType | Default | Explanation |
|
|
663
|
+
|------|----------|---------|-------------|
|
|
664
|
+
| `format` | string | `'A4'` | Paper format (e.g., 'Letter', 'A4', 'Legal'). |
|
|
665
|
+
| `landscape` | boolean | `false` | Page orientation. |
|
|
666
|
+
| `margin` | object | `{0,0,0,0}` | Top, right, bottom, left margins. |
|
|
667
|
+
| `displayHeaderFooter`| boolean | `false` | Whether to display print headers and footers. |
|
|
668
|
+
| `headerTemplate` | string | `''` | HTML template for the print header. |
|
|
669
|
+
| `footerTemplate` | string | `''` | HTML template for the print footer. |
|
|
670
|
+
|
|
671
|
+
### 4. CsvGeneratorConfig (`csvConfig`)
|
|
672
|
+
| Flag | DataType | Default | Explanation |
|
|
673
|
+
|------|----------|---------|-------------|
|
|
674
|
+
| `sheets` | string | `''` | Range of sheets to export (e.g., "1", "1-3", "1,3"). |
|
|
675
|
+
| `mergeSheets` | boolean | `true` | Merges all sheets into one CSV string. If false, returns a ZIP. |
|
|
676
|
+
| `columnDelimiter` | string | `','` | Custom delimiter for the CSV output. |
|
|
677
|
+
|
|
678
|
+
### 5. TextGeneratorConfig (`textConfig`)
|
|
679
|
+
| Flag | DataType | Default | Explanation |
|
|
680
|
+
|------|----------|---------|-------------|
|
|
681
|
+
| `newlineDelimiter` | string | `\n` | String inserted between structural blocks. |
|
|
682
|
+
| `preserveLayout` | boolean | `false` | Attempts to maintain table structures using whitespace. |
|
|
683
|
+
|
|
684
|
+
## One-Step Conversion: OfficeConverterConfig
|
|
685
|
+
Configuration for the `OfficeConverter.convert()` API.
|
|
686
|
+
|
|
687
|
+
| Flag | DataType | Default | Explanation |
|
|
688
|
+
|------|----------|---------|-------------|
|
|
689
|
+
| `parseConfig` | object | `{}` | Settings for the `OfficeParser` phase. |
|
|
690
|
+
| `generatorConfig` | object | `{}` | Settings for the `OfficeGenerator` phase. |
|
|
691
|
+
| `onWarning` | function | `undefined` | Global callback for issues in either phase. Overrides phase-specific callbacks. |
|
|
692
|
+
|
|
611
693
|
## Chunking Configuration: ChunkingConfig
|
|
612
694
|
Specific options when using `format: 'chunks'`.
|
|
613
695
|
|
package/dist/OfficeParser.js
CHANGED
|
@@ -158,12 +158,13 @@ class OfficeParser {
|
|
|
158
158
|
else {
|
|
159
159
|
throw (0, errorUtils_js_1.getOfficeError)(types_js_1.OfficeErrorType.INVALID_INPUT, internalConfig);
|
|
160
160
|
}
|
|
161
|
-
//
|
|
162
|
-
//
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
161
|
+
// Attempt to detect file type from buffer only if extension is unknown.
|
|
162
|
+
// This matches v6 behavior and prevents crashes in older Node environments
|
|
163
|
+
// where file-type 22.x might be incompatible.
|
|
164
|
+
if (buffer.length > 0 && !ext) {
|
|
165
|
+
try {
|
|
166
|
+
const { fileTypeFromBuffer } = await (0, moduleLoader_js_1.loadFileType)();
|
|
167
|
+
const type = await fileTypeFromBuffer(buffer);
|
|
167
168
|
if (type) {
|
|
168
169
|
ext = type.ext;
|
|
169
170
|
}
|
|
@@ -173,9 +174,25 @@ class OfficeParser {
|
|
|
173
174
|
// lack magic bytes. We'll let the switch default handle it.
|
|
174
175
|
}
|
|
175
176
|
}
|
|
176
|
-
|
|
177
|
-
//
|
|
178
|
-
(0, errorUtils_js_1.logWarning)(types_js_1.OfficeWarningType.
|
|
177
|
+
catch (error) {
|
|
178
|
+
// Log warning but don't crash; the switch below will handle unsupported/missing ext
|
|
179
|
+
(0, errorUtils_js_1.logWarning)(types_js_1.OfficeWarningType.FILE_TYPE_DETECTION_FAILED, internalConfig, { error });
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
else if (buffer.length > 0 && ext) {
|
|
183
|
+
// If extension is known, we can optionally verify it, but we wrap it
|
|
184
|
+
// in a try-catch to avoid breaking Node 18 if file-type fails to load.
|
|
185
|
+
try {
|
|
186
|
+
const { fileTypeFromBuffer } = await (0, moduleLoader_js_1.loadFileType)();
|
|
187
|
+
const type = await fileTypeFromBuffer(buffer);
|
|
188
|
+
if (type && type.ext.toLowerCase() !== ext.toLowerCase()) {
|
|
189
|
+
// Mismatch found between authoritative extension and detected content
|
|
190
|
+
(0, errorUtils_js_1.logWarning)(types_js_1.OfficeWarningType.BUFFER_TYPE_MISMATCH, internalConfig, { detected: type.ext, expected: ext });
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
catch (error) {
|
|
194
|
+
// Log warning so user knows verification could not be performed
|
|
195
|
+
(0, errorUtils_js_1.logWarning)(types_js_1.OfficeWarningType.FILE_TYPE_DETECTION_FAILED, internalConfig, { error });
|
|
179
196
|
}
|
|
180
197
|
}
|
|
181
198
|
if (!ext) {
|
|
@@ -63,6 +63,8 @@ export declare enum OfficeWarningType {
|
|
|
63
63
|
SHEET_RANGE_NOT_FOUND = "SHEET_RANGE_NOT_FOUND",
|
|
64
64
|
/** Buffer content type does not match the provided or expected file extension */
|
|
65
65
|
BUFFER_TYPE_MISMATCH = "BUFFER_TYPE_MISMATCH",
|
|
66
|
+
/** Failed to detect file type from buffer due to library error or incompatibility */
|
|
67
|
+
FILE_TYPE_DETECTION_FAILED = "FILE_TYPE_DETECTION_FAILED",
|
|
66
68
|
/** No chunks were generated for the document given the current strategy */
|
|
67
69
|
EMPTY_CHUNK_GENERATED = "EMPTY_CHUNK_GENERATED",
|
|
68
70
|
/** A node was skipped because it only contained whitespace */
|