@cj-tech-master/excelts 7.6.0 → 8.0.0
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 +99 -577
- package/README_zh.md +101 -577
- package/dist/browser/index.browser.d.ts +3 -0
- package/dist/browser/index.browser.js +2 -0
- package/dist/browser/index.d.ts +3 -0
- package/dist/browser/index.js +2 -0
- package/dist/browser/modules/archive/compression/compress.browser.js +4 -4
- package/dist/browser/modules/archive/compression/deflate-fallback.d.ts +24 -22
- package/dist/browser/modules/archive/compression/deflate-fallback.js +664 -360
- package/dist/browser/modules/archive/compression/streaming-compress.browser.d.ts +7 -0
- package/dist/browser/modules/archive/compression/streaming-compress.browser.js +15 -3
- package/dist/browser/modules/archive/compression/streaming-compress.d.ts +5 -0
- package/dist/browser/modules/archive/compression/streaming-compress.js +7 -0
- package/dist/browser/modules/archive/zip/stream.js +27 -3
- package/dist/browser/modules/excel/workbook.browser.d.ts +72 -0
- package/dist/browser/modules/excel/workbook.browser.js +226 -0
- package/dist/browser/modules/excel/workbook.d.ts +32 -1
- package/dist/browser/modules/excel/workbook.js +47 -2
- package/dist/browser/modules/excel/xlsx/xlsx.browser.js +42 -4
- package/dist/browser/modules/markdown/constants.d.ts +30 -0
- package/dist/browser/modules/markdown/constants.js +30 -0
- package/dist/browser/modules/markdown/errors.d.ts +21 -0
- package/dist/browser/modules/markdown/errors.js +23 -0
- package/dist/browser/modules/markdown/format/index.d.ts +54 -0
- package/dist/browser/modules/markdown/format/index.js +307 -0
- package/dist/browser/modules/markdown/index.d.ts +15 -0
- package/dist/browser/modules/markdown/index.js +22 -0
- package/dist/browser/modules/markdown/parse/index.d.ts +70 -0
- package/dist/browser/modules/markdown/parse/index.js +428 -0
- package/dist/browser/modules/markdown/types.d.ts +130 -0
- package/dist/browser/modules/markdown/types.js +6 -0
- package/dist/cjs/index.js +5 -1
- package/dist/cjs/modules/archive/compression/compress.browser.js +4 -4
- package/dist/cjs/modules/archive/compression/deflate-fallback.js +664 -360
- package/dist/cjs/modules/archive/compression/streaming-compress.browser.js +15 -2
- package/dist/cjs/modules/archive/compression/streaming-compress.js +8 -0
- package/dist/cjs/modules/archive/zip/stream.js +26 -2
- package/dist/cjs/modules/excel/workbook.browser.js +226 -0
- package/dist/cjs/modules/excel/workbook.js +46 -1
- package/dist/cjs/modules/excel/xlsx/xlsx.browser.js +42 -4
- package/dist/cjs/modules/markdown/constants.js +33 -0
- package/dist/cjs/modules/markdown/errors.js +28 -0
- package/dist/cjs/modules/markdown/format/index.js +310 -0
- package/dist/cjs/modules/markdown/index.js +30 -0
- package/dist/cjs/modules/markdown/parse/index.js +432 -0
- package/dist/cjs/modules/markdown/types.js +7 -0
- package/dist/esm/index.browser.js +2 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/modules/archive/compression/compress.browser.js +4 -4
- package/dist/esm/modules/archive/compression/deflate-fallback.js +664 -360
- package/dist/esm/modules/archive/compression/streaming-compress.browser.js +15 -3
- package/dist/esm/modules/archive/compression/streaming-compress.js +7 -0
- package/dist/esm/modules/archive/zip/stream.js +27 -3
- package/dist/esm/modules/excel/workbook.browser.js +226 -0
- package/dist/esm/modules/excel/workbook.js +47 -2
- package/dist/esm/modules/excel/xlsx/xlsx.browser.js +42 -4
- package/dist/esm/modules/markdown/constants.js +30 -0
- package/dist/esm/modules/markdown/errors.js +23 -0
- package/dist/esm/modules/markdown/format/index.js +307 -0
- package/dist/esm/modules/markdown/index.js +22 -0
- package/dist/esm/modules/markdown/parse/index.js +428 -0
- package/dist/esm/modules/markdown/types.js +6 -0
- package/dist/iife/excelts.iife.js +1342 -283
- package/dist/iife/excelts.iife.js.map +1 -1
- package/dist/iife/excelts.iife.min.js +38 -34
- package/dist/types/index.browser.d.ts +3 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/modules/archive/compression/deflate-fallback.d.ts +24 -22
- package/dist/types/modules/archive/compression/streaming-compress.browser.d.ts +7 -0
- package/dist/types/modules/archive/compression/streaming-compress.d.ts +5 -0
- package/dist/types/modules/excel/workbook.browser.d.ts +72 -0
- package/dist/types/modules/excel/workbook.d.ts +32 -1
- package/dist/types/modules/markdown/constants.d.ts +30 -0
- package/dist/types/modules/markdown/errors.d.ts +21 -0
- package/dist/types/modules/markdown/format/index.d.ts +54 -0
- package/dist/types/modules/markdown/index.d.ts +15 -0
- package/dist/types/modules/markdown/parse/index.d.ts +70 -0
- package/dist/types/modules/markdown/types.d.ts +130 -0
- package/package.json +56 -32
|
@@ -23,6 +23,8 @@ export { WorksheetWriter } from "./modules/excel/stream/worksheet-writer.js";
|
|
|
23
23
|
export { WorksheetReader } from "./modules/excel/stream/worksheet-reader.js";
|
|
24
24
|
export type { CsvOptions, CsvInput } from "./modules/excel/workbook.js";
|
|
25
25
|
export { CsvParserStream, CsvFormatterStream, createCsvParserStream, createCsvFormatterStream } from "./modules/csv/stream/index.js";
|
|
26
|
+
export type { MarkdownOptions } from "./modules/markdown/types.js";
|
|
27
|
+
export type { MarkdownAlignment, MarkdownParseResult, MarkdownParseOptions, MarkdownColumnConfig, MarkdownFormatOptions } from "./modules/markdown/types.js";
|
|
26
28
|
export { DefinedNames, type DefinedNameModel } from "./modules/excel/defined-names.js";
|
|
27
29
|
export type { CheckboxState } from "./modules/excel/form-control.js";
|
|
28
30
|
export type { ColumnDefn, ColumnHeaderValue } from "./modules/excel/column.js";
|
|
@@ -40,3 +42,4 @@ export { concatUint8Arrays, toUint8Array, stringToUint8Array, uint8ArrayToString
|
|
|
40
42
|
export { pdf, excelToPdf, PageSizes, PdfError, PdfRenderError, PdfFontError, PdfStructureError, isPdfError } from "./modules/pdf/index.js";
|
|
41
43
|
export type { PdfExportOptions, PdfPageSize, PdfOrientation, PdfMargins, PageSizeName, PdfColor } from "./modules/pdf/index.js";
|
|
42
44
|
export { ExcelError, isExcelError, ExcelFileError, ExcelDownloadError, ExcelNotSupportedError, ExcelStreamStateError, InvalidAddressError, ColumnOutOfBoundsError, RowOutOfBoundsError, MergeConflictError, InvalidValueTypeError, XmlParseError, WorksheetNameError, PivotTableError, TableError, ImageError, MaxItemsExceededError } from "./modules/excel/errors.js";
|
|
45
|
+
export { MarkdownError, MarkdownParseError } from "./modules/markdown/errors.js";
|
package/dist/types/index.d.ts
CHANGED
|
@@ -22,6 +22,8 @@ export type { WorksheetReaderOptions, WorksheetEvent, RowEvent, HyperlinkEvent,
|
|
|
22
22
|
export type { WorkbookWriterOptions, WorkbookZipOptions, ZlibOptions } from "./modules/excel/stream/workbook-writer.js";
|
|
23
23
|
export type { CsvOptions, CsvInput } from "./modules/excel/workbook.js";
|
|
24
24
|
export { CsvParserStream, CsvFormatterStream, createCsvParserStream, createCsvFormatterStream } from "./modules/csv/stream/index.js";
|
|
25
|
+
export type { MarkdownOptions } from "./modules/markdown/types.js";
|
|
26
|
+
export type { MarkdownAlignment, MarkdownParseResult, MarkdownParseOptions, MarkdownColumnConfig, MarkdownFormatOptions } from "./modules/markdown/types.js";
|
|
25
27
|
export { DefinedNames, type DefinedNameModel } from "./modules/excel/defined-names.js";
|
|
26
28
|
export type { CheckboxState } from "./modules/excel/form-control.js";
|
|
27
29
|
export type { ColumnDefn, ColumnHeaderValue } from "./modules/excel/column.js";
|
|
@@ -40,3 +42,4 @@ export { concatUint8Arrays, toUint8Array, stringToUint8Array, uint8ArrayToString
|
|
|
40
42
|
export { pdf, excelToPdf, PageSizes, PdfError, PdfRenderError, PdfFontError, PdfStructureError, isPdfError } from "./modules/pdf/index.js";
|
|
41
43
|
export type { PdfExportOptions, PdfPageSize, PdfOrientation, PdfMargins, PageSizeName, PdfColor } from "./modules/pdf/index.js";
|
|
42
44
|
export { ExcelError, isExcelError, ExcelFileError, ExcelDownloadError, ExcelNotSupportedError, ExcelStreamStateError, InvalidAddressError, ColumnOutOfBoundsError, RowOutOfBoundsError, MergeConflictError, InvalidValueTypeError, XmlParseError, WorksheetNameError, PivotTableError, TableError, ImageError, MaxItemsExceededError } from "./modules/excel/errors.js";
|
|
45
|
+
export { MarkdownError, MarkdownParseError } from "./modules/markdown/errors.js";
|
|
@@ -28,18 +28,18 @@ export declare function inflateRaw(data: Uint8Array): Uint8Array;
|
|
|
28
28
|
*/
|
|
29
29
|
export declare function deflateRawStore(data: Uint8Array): Uint8Array;
|
|
30
30
|
/**
|
|
31
|
-
* Compress data using DEFLATE with
|
|
31
|
+
* Compress data using DEFLATE with Dynamic Huffman codes (BTYPE=2).
|
|
32
32
|
*
|
|
33
|
-
* Uses LZ77 with hash chains and lazy matching for
|
|
34
|
-
*
|
|
35
|
-
* after zlib's "fast" and "slow" deflate strategies.
|
|
33
|
+
* Uses LZ77 with hash chains and lazy matching for match finding, then builds
|
|
34
|
+
* optimal Huffman trees from the symbol frequencies for entropy coding.
|
|
36
35
|
*
|
|
37
36
|
* @param data - Data to compress
|
|
37
|
+
* @param level - Compression level (1-9, default 6)
|
|
38
38
|
* @returns Compressed data in deflate-raw format
|
|
39
39
|
*/
|
|
40
|
-
export declare function deflateRawCompressed(data: Uint8Array): Uint8Array;
|
|
40
|
+
export declare function deflateRawCompressed(data: Uint8Array, level?: number): Uint8Array;
|
|
41
41
|
/**
|
|
42
|
-
* Stateful synchronous DEFLATE compressor.
|
|
42
|
+
* Stateful synchronous DEFLATE compressor with Dynamic Huffman encoding.
|
|
43
43
|
*
|
|
44
44
|
* Unlike `deflateRawCompressed` (which is a one-shot function), this class
|
|
45
45
|
* maintains state across multiple `write()` calls:
|
|
@@ -47,32 +47,29 @@ export declare function deflateRawCompressed(data: Uint8Array): Uint8Array;
|
|
|
47
47
|
* - **LZ77 sliding window**: back-references can span across chunks.
|
|
48
48
|
* - **Hash chains**: match positions persist across chunks with typed-array
|
|
49
49
|
* hash tables for fast lookup.
|
|
50
|
-
* - **Lazy matching**:
|
|
51
|
-
*
|
|
50
|
+
* - **Lazy matching**: configurable per compression level.
|
|
51
|
+
* - **Dynamic Huffman**: each block builds optimal Huffman trees from
|
|
52
|
+
* actual symbol frequencies (BTYPE=2), producing significantly smaller
|
|
53
|
+
* output than fixed Huffman (BTYPE=1).
|
|
52
54
|
* - **Bit writer**: bit position is preserved, so consecutive blocks form
|
|
53
55
|
* a single valid DEFLATE bit-stream without alignment issues.
|
|
54
56
|
*
|
|
55
|
-
* Each `write()` emits one non-final
|
|
56
|
-
* `finish()` emits a final empty block (BFINAL=1)
|
|
57
|
+
* Each `write()` emits one non-final Dynamic Huffman block (BFINAL=0).
|
|
58
|
+
* `finish()` emits a final empty fixed-Huffman block (BFINAL=1).
|
|
57
59
|
*
|
|
58
60
|
* This is the pure-JS equivalent of Node.js `zlib.deflateRawSync` with
|
|
59
61
|
* `Z_SYNC_FLUSH`, used by the streaming ZIP writer (`pushSync`) to achieve
|
|
60
62
|
* constant-memory streaming in both Node.js and browsers.
|
|
63
|
+
*
|
|
64
|
+
* @param level - Compression level (0-9). Level 0 emits STORE blocks.
|
|
65
|
+
* Default: 6 (matching zlib default).
|
|
61
66
|
*/
|
|
62
67
|
export declare class SyncDeflater {
|
|
63
68
|
private _output;
|
|
64
|
-
private
|
|
65
|
-
private
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
/** Number of valid bytes currently in the window. */
|
|
69
|
-
private _windowLen;
|
|
70
|
-
/** Total bytes written so far (monotonically increasing; used for hash offsets). */
|
|
71
|
-
private _totalIn;
|
|
72
|
-
private _hasPrevMatch;
|
|
73
|
-
private _prevMatchLen;
|
|
74
|
-
private _prevMatchDist;
|
|
75
|
-
private _prevLiteral;
|
|
69
|
+
private _config;
|
|
70
|
+
private _level;
|
|
71
|
+
private _state;
|
|
72
|
+
constructor(level?: number);
|
|
76
73
|
/**
|
|
77
74
|
* Compress a chunk and return the compressed bytes produced so far.
|
|
78
75
|
* The output is a valid prefix of a DEFLATE stream (one or more non-final blocks).
|
|
@@ -83,4 +80,9 @@ export declare class SyncDeflater {
|
|
|
83
80
|
* and returns any remaining bytes (including partial-byte padding).
|
|
84
81
|
*/
|
|
85
82
|
finish(): Uint8Array;
|
|
83
|
+
/**
|
|
84
|
+
* Write STORE (uncompressed) blocks for level=0.
|
|
85
|
+
* Each block is non-final (BFINAL=0); the final block is emitted by finish().
|
|
86
|
+
*/
|
|
87
|
+
private _writeStore;
|
|
86
88
|
}
|
|
@@ -44,3 +44,10 @@ export declare function createUnzlibStream(options?: StreamCompressOptions): Unz
|
|
|
44
44
|
* bit-stream state across `write()` calls.
|
|
45
45
|
*/
|
|
46
46
|
export { PureJsSyncDeflater as SyncDeflater };
|
|
47
|
+
/**
|
|
48
|
+
* Returns true when the browser supports native `CompressionStream("deflate-raw")`,
|
|
49
|
+
* signalling that `push()` should prefer the async path over `SyncDeflater`.
|
|
50
|
+
*
|
|
51
|
+
* Only checks for compression support — decompression is not needed for writing.
|
|
52
|
+
*/
|
|
53
|
+
export declare function hasNativeAsyncDeflate(): boolean;
|
|
@@ -75,3 +75,8 @@ export declare class SyncDeflater implements SyncDeflaterLike {
|
|
|
75
75
|
finish(): Uint8Array;
|
|
76
76
|
private _flushBatch;
|
|
77
77
|
}
|
|
78
|
+
/**
|
|
79
|
+
* On Node.js, `zlib.deflateRawSync` is native and fast — no need to detour
|
|
80
|
+
* through the async streaming path. Always returns false.
|
|
81
|
+
*/
|
|
82
|
+
export declare function hasNativeAsyncDeflate(): boolean;
|
|
@@ -16,6 +16,7 @@ import { WorkbookWriter, type WorkbookWriterOptions } from "./stream/workbook-wr
|
|
|
16
16
|
import { WorkbookReader, type WorkbookReaderOptions } from "./stream/workbook-reader.js";
|
|
17
17
|
import { type DateFormat } from "../../utils/datetime.js";
|
|
18
18
|
import type { CsvParseOptions, CsvFormatOptions } from "../csv/types.js";
|
|
19
|
+
import type { MarkdownOptions } from "../markdown/types.js";
|
|
19
20
|
import type { Readable } from "../stream/index.js";
|
|
20
21
|
import type { IReadable, IWritable } from "../stream/types.js";
|
|
21
22
|
import type { PivotTable } from "./pivot-table.js";
|
|
@@ -253,6 +254,77 @@ declare class Workbook {
|
|
|
253
254
|
private _readCsvFile;
|
|
254
255
|
private _readCsvBlob;
|
|
255
256
|
private _writeCsvString;
|
|
257
|
+
/**
|
|
258
|
+
* Populate a worksheet from a parsed Markdown table result.
|
|
259
|
+
* Shared by readMarkdown and readMarkdownAll.
|
|
260
|
+
*/
|
|
261
|
+
private _populateMarkdownWorksheet;
|
|
262
|
+
/**
|
|
263
|
+
* Read a Markdown table and add as worksheet.
|
|
264
|
+
*
|
|
265
|
+
* @example
|
|
266
|
+
* ```ts
|
|
267
|
+
* // From a Markdown string
|
|
268
|
+
* workbook.readMarkdown("| Name | Age |\n| --- | --- |\n| Alice | 30 |");
|
|
269
|
+
*
|
|
270
|
+
* // With options
|
|
271
|
+
* workbook.readMarkdown(markdownString, { sheetName: "Data", map: (v, col) => Number(v) || v });
|
|
272
|
+
* ```
|
|
273
|
+
*/
|
|
274
|
+
readMarkdown(input: string, options?: MarkdownOptions): Worksheet;
|
|
275
|
+
/**
|
|
276
|
+
* Read all Markdown tables from a document, each becoming a separate worksheet.
|
|
277
|
+
*
|
|
278
|
+
* @param input - Markdown string containing one or more tables
|
|
279
|
+
* @param options - Parse options (sheetName is used as prefix: "sheetName", "sheetName_2", ...)
|
|
280
|
+
* @returns Array of created worksheets (empty if no tables found)
|
|
281
|
+
*
|
|
282
|
+
* @example
|
|
283
|
+
* ```ts
|
|
284
|
+
* // Parse a document with multiple tables
|
|
285
|
+
* const sheets = workbook.readMarkdownAll(markdownDoc);
|
|
286
|
+
* console.log(`Created ${sheets.length} worksheets`);
|
|
287
|
+
*
|
|
288
|
+
* // With a naming prefix
|
|
289
|
+
* const sheets = workbook.readMarkdownAll(markdownDoc, { sheetName: "Table" });
|
|
290
|
+
* // Creates "Table", "Table_2", "Table_3", ...
|
|
291
|
+
* ```
|
|
292
|
+
*/
|
|
293
|
+
readMarkdownAll(input: string, options?: MarkdownOptions): Worksheet[];
|
|
294
|
+
/**
|
|
295
|
+
* Write worksheet as a Markdown table string.
|
|
296
|
+
*
|
|
297
|
+
* @example
|
|
298
|
+
* ```ts
|
|
299
|
+
* // Write first worksheet
|
|
300
|
+
* const markdownText = workbook.writeMarkdown();
|
|
301
|
+
*
|
|
302
|
+
* // Write specific worksheet with options
|
|
303
|
+
* const markdownText = workbook.writeMarkdown({ sheetName: "Data", padding: true });
|
|
304
|
+
* ```
|
|
305
|
+
*/
|
|
306
|
+
writeMarkdown(options?: MarkdownOptions): string;
|
|
307
|
+
/**
|
|
308
|
+
* Write worksheet to Markdown buffer (Uint8Array).
|
|
309
|
+
*
|
|
310
|
+
* @example
|
|
311
|
+
* ```ts
|
|
312
|
+
* const buffer = workbook.writeMarkdownBuffer();
|
|
313
|
+
* ```
|
|
314
|
+
*/
|
|
315
|
+
writeMarkdownBuffer(options?: MarkdownOptions): Uint8Array;
|
|
316
|
+
/**
|
|
317
|
+
* Read Markdown from file (Node.js only - throws in browser)
|
|
318
|
+
*/
|
|
319
|
+
readMarkdownFile(_filename: string, _options?: MarkdownOptions): Promise<Worksheet>;
|
|
320
|
+
/**
|
|
321
|
+
* Read all Markdown tables from file (Node.js only - throws in browser)
|
|
322
|
+
*/
|
|
323
|
+
readMarkdownAllFile(_filename: string, _options?: MarkdownOptions): Promise<Worksheet[]>;
|
|
324
|
+
/**
|
|
325
|
+
* Write Markdown to file (Node.js only - throws in browser)
|
|
326
|
+
*/
|
|
327
|
+
writeMarkdownFile(_filename: string, _options?: MarkdownOptions): Promise<void>;
|
|
256
328
|
/**
|
|
257
329
|
* Create a streaming workbook writer for large files.
|
|
258
330
|
* This is more memory-efficient than using Workbook for large datasets.
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Workbook - Node.js Version
|
|
3
3
|
*
|
|
4
|
-
* Extends browser Workbook with Node.js file system support for CSV operations.
|
|
4
|
+
* Extends browser Workbook with Node.js file system support for CSV and Markdown operations.
|
|
5
5
|
*/
|
|
6
6
|
import { Workbook as WorkbookBrowser, type CsvOptions } from "./workbook.browser.js";
|
|
7
7
|
import type { Worksheet } from "./worksheet.js";
|
|
8
|
+
import type { MarkdownOptions } from "../markdown/types.js";
|
|
8
9
|
declare class Workbook extends WorkbookBrowser {
|
|
9
10
|
/**
|
|
10
11
|
* Read CSV from file (Node.js only)
|
|
@@ -27,6 +28,36 @@ declare class Workbook extends WorkbookBrowser {
|
|
|
27
28
|
* ```
|
|
28
29
|
*/
|
|
29
30
|
writeCsvFile(filename: string, options?: CsvOptions): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Read Markdown table from file (Node.js only)
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* await workbook.readMarkdownFile("table.md");
|
|
37
|
+
* await workbook.readMarkdownFile("table.md", { sheetName: "Data" });
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
readMarkdownFile(filename: string, options?: MarkdownOptions): Promise<Worksheet>;
|
|
41
|
+
/**
|
|
42
|
+
* Read all Markdown tables from file, each as a separate worksheet (Node.js only)
|
|
43
|
+
*
|
|
44
|
+
* @example
|
|
45
|
+
* ```ts
|
|
46
|
+
* await workbook.readMarkdownAllFile("doc.md");
|
|
47
|
+
* await workbook.readMarkdownAllFile("doc.md", { sheetName: "Table" });
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
readMarkdownAllFile(filename: string, options?: MarkdownOptions): Promise<Worksheet[]>;
|
|
51
|
+
/**
|
|
52
|
+
* Write Markdown table to file (Node.js only)
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* await workbook.writeMarkdownFile("output.md");
|
|
57
|
+
* await workbook.writeMarkdownFile("output.md", { sheetName: "Data", padding: true });
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
writeMarkdownFile(filename: string, options?: MarkdownOptions): Promise<void>;
|
|
30
61
|
}
|
|
31
62
|
export { Workbook };
|
|
32
63
|
export type { CsvOptions, CsvInput, WorkbookModel, WorkbookMedia } from "./workbook.browser.js";
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Markdown Module Constants
|
|
3
|
+
*
|
|
4
|
+
* Shared constants used across the Markdown module.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Pre-compiled regex for line splitting (matches CR, LF, or CRLF)
|
|
8
|
+
*/
|
|
9
|
+
export declare const LINEBREAK_REGEX: RegExp;
|
|
10
|
+
/**
|
|
11
|
+
* Characters that need escaping in Markdown table cells.
|
|
12
|
+
* Also matches CRLF/CR/LF so that escaping + newline conversion
|
|
13
|
+
* can be done in a single `replace()` call.
|
|
14
|
+
* Note: `\r\n` must come before `\r` to match CRLF as a single unit.
|
|
15
|
+
*/
|
|
16
|
+
export declare const ESCAPE_AND_NEWLINE: RegExp;
|
|
17
|
+
/**
|
|
18
|
+
* Regex to unescape Markdown table cell content (`\|` → `|`, `\\` → `\`)
|
|
19
|
+
*/
|
|
20
|
+
export declare const UNESCAPE_REGEX: RegExp;
|
|
21
|
+
/**
|
|
22
|
+
* Regex to match `<br>`, `<br/>`, or `<br />` tags (case-insensitive).
|
|
23
|
+
* Used to convert multiline cell representations back to newlines during parsing.
|
|
24
|
+
*/
|
|
25
|
+
export declare const BR_TAG_REGEX: RegExp;
|
|
26
|
+
/**
|
|
27
|
+
* Regex to match literal newlines (CR, LF, or CRLF) in cell content.
|
|
28
|
+
* Used when escaping is disabled but newline conversion is still needed.
|
|
29
|
+
*/
|
|
30
|
+
export declare const NEWLINE_IN_CELL: RegExp;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Markdown module error types.
|
|
3
|
+
*/
|
|
4
|
+
import { BaseError } from "../../utils/errors.js";
|
|
5
|
+
/**
|
|
6
|
+
* Base class for all Markdown-related errors.
|
|
7
|
+
*/
|
|
8
|
+
export declare class MarkdownError extends BaseError {
|
|
9
|
+
name: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Error thrown when Markdown parsing fails.
|
|
13
|
+
*/
|
|
14
|
+
export declare class MarkdownParseError extends MarkdownError {
|
|
15
|
+
name: string;
|
|
16
|
+
/** 1-based line number where the error occurred */
|
|
17
|
+
readonly line: number;
|
|
18
|
+
constructor(message: string, line: number, options?: {
|
|
19
|
+
cause?: unknown;
|
|
20
|
+
});
|
|
21
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Markdown Table Formatter
|
|
3
|
+
*
|
|
4
|
+
* Formats data into well-formed Markdown table strings.
|
|
5
|
+
*
|
|
6
|
+
* Features:
|
|
7
|
+
* - Auto column width calculation with padding
|
|
8
|
+
* - Column alignment (left, center, right, none)
|
|
9
|
+
* - Proper escaping of pipe characters and backslashes
|
|
10
|
+
* - Compact mode (disable column-width alignment) for minimal output
|
|
11
|
+
* - Configurable column definitions
|
|
12
|
+
* - Multiline cell content (newlines converted to `<br>`)
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* // Simple array data with headers
|
|
17
|
+
* formatMarkdown(["Name", "Age"], [["Alice", "30"], ["Bob", "25"]]);
|
|
18
|
+
* // | Name | Age |
|
|
19
|
+
* // | ----- | --- |
|
|
20
|
+
* // | Alice | 30 |
|
|
21
|
+
* // | Bob | 25 |
|
|
22
|
+
*
|
|
23
|
+
* // With alignment
|
|
24
|
+
* formatMarkdown(["Left", "Center", "Right"], data, {
|
|
25
|
+
* alignment: "left",
|
|
26
|
+
* columns: [
|
|
27
|
+
* { header: "Left", alignment: "left" },
|
|
28
|
+
* { header: "Center", alignment: "center" },
|
|
29
|
+
* { header: "Right", alignment: "right" }
|
|
30
|
+
* ]
|
|
31
|
+
* });
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
import type { MarkdownFormatOptions } from "../types.js";
|
|
35
|
+
/**
|
|
36
|
+
* Format data as a Markdown table string.
|
|
37
|
+
*
|
|
38
|
+
* @param headers - Column header strings
|
|
39
|
+
* @param rows - Data rows (each row is an array of cell values)
|
|
40
|
+
* @param options - Formatting options
|
|
41
|
+
* @returns Formatted Markdown table string
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* ```ts
|
|
45
|
+
* formatMarkdown(
|
|
46
|
+
* ["Name", "Age", "City"],
|
|
47
|
+
* [
|
|
48
|
+
* ["Alice", 30, "New York"],
|
|
49
|
+
* ["Bob", 25, "London"]
|
|
50
|
+
* ]
|
|
51
|
+
* );
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export declare function formatMarkdown(headers: string[], rows: unknown[][], options?: MarkdownFormatOptions): string;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Markdown Module - Public API
|
|
3
|
+
*
|
|
4
|
+
* Pure Markdown table parsing/formatting functionality with no Excel dependencies.
|
|
5
|
+
* For Markdown-Worksheet integration, use Workbook.readMarkdown/writeMarkdown methods instead.
|
|
6
|
+
*
|
|
7
|
+
* Design principles:
|
|
8
|
+
* - Only export types and functions that are part of the PUBLIC API
|
|
9
|
+
* - Internal utilities are used internally but not exported
|
|
10
|
+
* - This reduces bundle size and simplifies the public interface
|
|
11
|
+
*/
|
|
12
|
+
export type { MarkdownAlignment, MarkdownParseResult, MarkdownParseOptions, MarkdownColumnConfig, MarkdownFormatOptions, MarkdownOptions } from "./types.js";
|
|
13
|
+
export { parseMarkdown, parseMarkdownAll } from "./parse/index.js";
|
|
14
|
+
export { formatMarkdown } from "./format/index.js";
|
|
15
|
+
export { MarkdownError, MarkdownParseError } from "./errors.js";
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Markdown Table Parser
|
|
3
|
+
*
|
|
4
|
+
* Parses Markdown tables into structured data.
|
|
5
|
+
*
|
|
6
|
+
* Supports:
|
|
7
|
+
* - Standard GFM (GitHub Flavored Markdown) table syntax
|
|
8
|
+
* - Column alignment detection via separator row
|
|
9
|
+
* - Escaped pipes (`\|`) in cell content
|
|
10
|
+
* - Tables with or without leading/trailing pipes
|
|
11
|
+
* - Tolerant parsing (mismatched column counts, extra whitespace)
|
|
12
|
+
* - Multiline cell content via `<br>` / `<br/>` / `<br />` tags
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* const result = parseMarkdown("| Name | Age |\n| --- | --- |\n| Alice | 30 |");
|
|
17
|
+
* // result.headers = ["Name", "Age"]
|
|
18
|
+
* // result.rows = [["Alice", "30"]]
|
|
19
|
+
* // result.alignments = ["none", "none"]
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
import type { MarkdownParseOptions, MarkdownParseResult } from "../types.js";
|
|
23
|
+
/**
|
|
24
|
+
* Parse a Markdown table string into structured data.
|
|
25
|
+
*
|
|
26
|
+
* The parser looks for the GFM table pattern:
|
|
27
|
+
* 1. A header row (pipe-delimited cells)
|
|
28
|
+
* 2. A separator row (dashes with optional colons for alignment)
|
|
29
|
+
* 3. Zero or more data rows
|
|
30
|
+
*
|
|
31
|
+
* Non-table content before and after the table is ignored.
|
|
32
|
+
*
|
|
33
|
+
* @param input - Markdown string containing a table
|
|
34
|
+
* @param options - Parse options
|
|
35
|
+
* @returns Parsed table data with headers, rows, and alignments
|
|
36
|
+
*
|
|
37
|
+
* @throws {MarkdownParseError} When no valid table is found in the input
|
|
38
|
+
*
|
|
39
|
+
* @example
|
|
40
|
+
* ```ts
|
|
41
|
+
* // Basic table
|
|
42
|
+
* const result = parseMarkdown("| Name | Age |\n| --- | --- |\n| Alice | 30 |");
|
|
43
|
+
*
|
|
44
|
+
* // With alignment
|
|
45
|
+
* const result = parseMarkdown("| Left | Center | Right |\n|:---|:---:|---:|\n|a|b|c|");
|
|
46
|
+
* // result.alignments = ["left", "center", "right"]
|
|
47
|
+
*
|
|
48
|
+
* // From a larger Markdown document
|
|
49
|
+
* const result = parseMarkdown(markdownDoc); // Finds the first table
|
|
50
|
+
*
|
|
51
|
+
* // With options
|
|
52
|
+
* const result = parseMarkdown(input, { trim: false, maxRows: 100 });
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export declare function parseMarkdown(input: string, options?: MarkdownParseOptions): MarkdownParseResult;
|
|
56
|
+
/**
|
|
57
|
+
* Parse all Markdown tables from a document.
|
|
58
|
+
*
|
|
59
|
+
* @param input - Markdown string containing one or more tables
|
|
60
|
+
* @param options - Parse options (maxRows applies per table)
|
|
61
|
+
* @returns Array of parsed tables
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```ts
|
|
65
|
+
* const tables = parseMarkdownAll(markdownDoc);
|
|
66
|
+
* console.log(`Found ${tables.length} tables`);
|
|
67
|
+
* tables.forEach((t, i) => console.log(`Table ${i}: ${t.headers.join(", ")}`));
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export declare function parseMarkdownAll(input: string, options?: MarkdownParseOptions): MarkdownParseResult[];
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Markdown Types
|
|
3
|
+
*
|
|
4
|
+
* Centralized type definitions for the Markdown module.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Column alignment in a Markdown table.
|
|
8
|
+
* Determined by colon placement in the separator row:
|
|
9
|
+
* - `:---` = left
|
|
10
|
+
* - `:---:` = center
|
|
11
|
+
* - `---:` = right
|
|
12
|
+
* - `---` = none (defaults to left in most renderers)
|
|
13
|
+
*/
|
|
14
|
+
export type MarkdownAlignment = "left" | "center" | "right" | "none";
|
|
15
|
+
/**
|
|
16
|
+
* Result of parsing a Markdown table.
|
|
17
|
+
*/
|
|
18
|
+
export interface MarkdownParseResult {
|
|
19
|
+
/** Parsed header row (column names) */
|
|
20
|
+
headers: string[];
|
|
21
|
+
/** Parsed data rows (each row is an array of cell values) */
|
|
22
|
+
rows: string[][];
|
|
23
|
+
/** Column alignments extracted from the separator row */
|
|
24
|
+
alignments: MarkdownAlignment[];
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Markdown parsing options.
|
|
28
|
+
*/
|
|
29
|
+
export interface MarkdownParseOptions {
|
|
30
|
+
/**
|
|
31
|
+
* Trim whitespace from cell values.
|
|
32
|
+
* @default true
|
|
33
|
+
*/
|
|
34
|
+
trim?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Unescape Markdown escape sequences in cell values (e.g. `\|` -> `|`).
|
|
37
|
+
* @default true
|
|
38
|
+
*/
|
|
39
|
+
unescape?: boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Skip empty rows in the output.
|
|
42
|
+
* @default true
|
|
43
|
+
*/
|
|
44
|
+
skipEmptyRows?: boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Maximum number of data rows to parse (excludes header).
|
|
47
|
+
* Useful for previewing large tables.
|
|
48
|
+
*/
|
|
49
|
+
maxRows?: number;
|
|
50
|
+
/**
|
|
51
|
+
* Convert `<br>`, `<br/>`, `<br />` tags in cell content to newline characters.
|
|
52
|
+
* Useful for round-tripping multiline cell content through Markdown.
|
|
53
|
+
* @default false
|
|
54
|
+
*/
|
|
55
|
+
convertBr?: boolean;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Column configuration for Markdown formatting.
|
|
59
|
+
*/
|
|
60
|
+
export interface MarkdownColumnConfig {
|
|
61
|
+
/** Column header text */
|
|
62
|
+
header: string;
|
|
63
|
+
/**
|
|
64
|
+
* Column alignment.
|
|
65
|
+
* @default "left"
|
|
66
|
+
*/
|
|
67
|
+
alignment?: MarkdownAlignment;
|
|
68
|
+
/**
|
|
69
|
+
* Minimum column width (in characters, excluding padding).
|
|
70
|
+
* The actual width will be the maximum of this value and the widest cell content.
|
|
71
|
+
* @default 3 (minimum for separator `---`)
|
|
72
|
+
*/
|
|
73
|
+
minWidth?: number;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Markdown formatting options.
|
|
77
|
+
*/
|
|
78
|
+
export interface MarkdownFormatOptions {
|
|
79
|
+
/**
|
|
80
|
+
* Column configuration. When provided, overrides auto-detected headers and alignment.
|
|
81
|
+
* Can be an array of strings (header names) or MarkdownColumnConfig objects.
|
|
82
|
+
*/
|
|
83
|
+
columns?: (string | MarkdownColumnConfig)[];
|
|
84
|
+
/**
|
|
85
|
+
* Default alignment for columns without explicit alignment.
|
|
86
|
+
* @default "left"
|
|
87
|
+
*/
|
|
88
|
+
alignment?: MarkdownAlignment;
|
|
89
|
+
/**
|
|
90
|
+
* Align columns to equal width by padding cell content with spaces.
|
|
91
|
+
* When false, disables width-alignment padding but retains the single space
|
|
92
|
+
* around cell content required by most Markdown renderers (`| value |`).
|
|
93
|
+
* @default true
|
|
94
|
+
*/
|
|
95
|
+
padding?: boolean;
|
|
96
|
+
/**
|
|
97
|
+
* Include a trailing newline at the end of the output.
|
|
98
|
+
* @default true
|
|
99
|
+
*/
|
|
100
|
+
trailingNewline?: boolean;
|
|
101
|
+
/**
|
|
102
|
+
* Escape pipe characters (`|`) and backslashes (`\`) in cell content.
|
|
103
|
+
* @default true
|
|
104
|
+
*/
|
|
105
|
+
escapeContent?: boolean;
|
|
106
|
+
/**
|
|
107
|
+
* Custom value-to-string converter.
|
|
108
|
+
* Called for each cell value before formatting.
|
|
109
|
+
* Return value is used as the cell text.
|
|
110
|
+
*/
|
|
111
|
+
stringify?: (value: unknown) => string;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Unified Markdown options for Workbook integration.
|
|
115
|
+
* Combines parse and format options with worksheet-specific settings.
|
|
116
|
+
*/
|
|
117
|
+
export interface MarkdownOptions extends MarkdownParseOptions, MarkdownFormatOptions {
|
|
118
|
+
/** Name of the worksheet to read from or write to */
|
|
119
|
+
sheetName?: string;
|
|
120
|
+
/** ID of the worksheet to read from or write to */
|
|
121
|
+
sheetId?: number;
|
|
122
|
+
/** Custom value mapper for parsing (MD string -> cell value) */
|
|
123
|
+
map?: (value: string, column: number) => unknown;
|
|
124
|
+
/** Date format string for formatting Date values */
|
|
125
|
+
dateFormat?: string;
|
|
126
|
+
/** Use UTC for date formatting */
|
|
127
|
+
dateUTC?: boolean;
|
|
128
|
+
/** Include empty rows in output */
|
|
129
|
+
includeEmptyRows?: boolean;
|
|
130
|
+
}
|