md-spreadsheet-parser 1.1.5 → 1.1.7
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 +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +43 -12
- package/dist/parser.core.wasm +0 -0
- package/dist/parser.core8.wasm +0 -0
- package/dist/parser.d.ts +1 -1
- package/dist/parser.js +90 -83
- package/package.json +2 -1
- package/src/__pycache__/app.cpython-314.pyc +0 -0
- package/src/__pycache__/generated_adapter.cpython-314.pyc +0 -0
- package/src/index.ts +50 -14
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
</a>
|
|
8
8
|
</p>
|
|
9
9
|
|
|
10
|
-
**md-spreadsheet-parser** is a robust
|
|
10
|
+
**md-spreadsheet-parser** is a robust Markdown table parser and manipulator for Node.js.
|
|
11
11
|
It is powered by the [Python Core](https://github.com/f-y/md-spreadsheet-parser) compiled to WebAssembly, ensuring 100% logic parity with the Python version while running natively in Node.js.
|
|
12
12
|
|
|
13
13
|
## Features
|
package/dist/index.d.ts
CHANGED
|
@@ -10,9 +10,9 @@ export declare function scanTables(markdown: any, schema?: any): any;
|
|
|
10
10
|
export declare function generateTableMarkdown(table: any, schema?: any): any;
|
|
11
11
|
export declare function generateSheetMarkdown(sheet: any, schema?: any): any;
|
|
12
12
|
export declare function generateWorkbookMarkdown(workbook: any, schema: any): any;
|
|
13
|
-
export declare function parseTableFromFile(source: any, schema?: any): any
|
|
14
|
-
export declare function parseWorkbookFromFile(source: any, schema?: any): any
|
|
15
|
-
export declare function scanTablesFromFile(source: any, schema?: any): any
|
|
13
|
+
export declare function parseTableFromFile(source: any, schema?: any): Promise<any>;
|
|
14
|
+
export declare function parseWorkbookFromFile(source: any, schema?: any): Promise<any>;
|
|
15
|
+
export declare function scanTablesFromFile(source: any, schema?: any): Promise<any>;
|
|
16
16
|
export declare function scanTablesIter(source: any, schema?: any): any;
|
|
17
17
|
export declare class Table {
|
|
18
18
|
headers: any | undefined;
|
|
@@ -49,7 +49,7 @@ export declare class Workbook {
|
|
|
49
49
|
constructor(data?: Partial<Workbook>);
|
|
50
50
|
toDTO(): any;
|
|
51
51
|
getSheet(name: any): any;
|
|
52
|
-
toMarkdown(schema
|
|
52
|
+
toMarkdown(schema?: any): any;
|
|
53
53
|
addSheet(name: any): any;
|
|
54
54
|
deleteSheet(index: any): any;
|
|
55
55
|
}
|
package/dist/index.js
CHANGED
|
@@ -1,15 +1,43 @@
|
|
|
1
1
|
import { cleanCell as _cleanCell, splitRowGfm as _splitRowGfm, parseRow as _parseRow, parseSeparatorRow as _parseSeparatorRow, isSeparatorRow as _isSeparatorRow, parseTable as _parseTable, parseSheet as _parseSheet, parseWorkbook as _parseWorkbook, scanTables as _scanTables, generateTableMarkdown as _generateTableMarkdown, generateSheetMarkdown as _generateSheetMarkdown, generateWorkbookMarkdown as _generateWorkbookMarkdown, parseTableFromFile as _parseTableFromFile, parseWorkbookFromFile as _parseWorkbookFromFile, scanTablesFromFile as _scanTablesFromFile, scanTablesIter as _scanTablesIter, tableToModels as _tableToModels, tableToMarkdown as _tableToMarkdown, tableUpdateCell as _tableUpdateCell, tableDeleteRow as _tableDeleteRow, tableDeleteColumn as _tableDeleteColumn, tableClearColumnData as _tableClearColumnData, tableInsertRow as _tableInsertRow, tableInsertColumn as _tableInsertColumn, sheetGetTable as _sheetGetTable, sheetToMarkdown as _sheetToMarkdown, workbookGetSheet as _workbookGetSheet, workbookToMarkdown as _workbookToMarkdown, workbookAddSheet as _workbookAddSheet, workbookDeleteSheet as _workbookDeleteSheet } from '../dist/parser.js';
|
|
2
|
-
// @ts-ignore
|
|
3
|
-
import path from 'node:path';
|
|
4
|
-
// @ts-ignore
|
|
5
|
-
import process from 'node:process';
|
|
6
|
-
// @ts-ignore
|
|
7
|
-
import { _addPreopen } from '@bytecodealliance/preview2-shim/filesystem';
|
|
8
2
|
import { clientSideToModels } from './client-adapters.js';
|
|
9
|
-
//
|
|
10
|
-
|
|
3
|
+
// Environment detection
|
|
4
|
+
// @ts-ignore - process may not be defined in browser
|
|
5
|
+
const isNode = typeof process !== 'undefined'
|
|
6
|
+
&& typeof process.versions !== 'undefined'
|
|
7
|
+
&& typeof process.versions.node !== 'undefined';
|
|
8
|
+
// Lazily loaded Node.js modules (only in Node.js environment)
|
|
9
|
+
let _pathModule = null;
|
|
10
|
+
let _nodeInitialized = false;
|
|
11
|
+
/**
|
|
12
|
+
* Ensures Node.js environment is initialized for file system operations.
|
|
13
|
+
* Throws an error in browser environments.
|
|
14
|
+
*/
|
|
15
|
+
async function ensureNodeEnvironment() {
|
|
16
|
+
if (!isNode) {
|
|
17
|
+
throw new Error('File system operations (parseTableFromFile, parseWorkbookFromFile, scanTablesFromFile) ' +
|
|
18
|
+
'are not supported in browser environments. ' +
|
|
19
|
+
'Use parseTable(), parseWorkbook(), or scanTables() with string content instead.');
|
|
20
|
+
}
|
|
21
|
+
if (_nodeInitialized)
|
|
22
|
+
return;
|
|
23
|
+
// Dynamic imports for Node.js only
|
|
24
|
+
const [pathModule, processModule, fsShim] = await Promise.all([
|
|
25
|
+
import('node:path'),
|
|
26
|
+
import('node:process'),
|
|
27
|
+
import('@bytecodealliance/preview2-shim/filesystem')
|
|
28
|
+
]);
|
|
29
|
+
_pathModule = pathModule.default || pathModule;
|
|
30
|
+
const proc = processModule.default || processModule;
|
|
31
|
+
const root = _pathModule.parse(proc.cwd()).root;
|
|
32
|
+
// @ts-ignore - _addPreopen is an internal function
|
|
33
|
+
fsShim._addPreopen('/', root);
|
|
34
|
+
_nodeInitialized = true;
|
|
35
|
+
}
|
|
11
36
|
function resolveToVirtualPath(p) {
|
|
12
|
-
|
|
37
|
+
if (!_pathModule) {
|
|
38
|
+
throw new Error('Node.js modules not initialized. Call ensureNodeEnvironment() first.');
|
|
39
|
+
}
|
|
40
|
+
return _pathModule.resolve(p);
|
|
13
41
|
}
|
|
14
42
|
export function cleanCell(cell, schema) {
|
|
15
43
|
const res = _cleanCell(cell, schema);
|
|
@@ -59,17 +87,20 @@ export function generateWorkbookMarkdown(workbook, schema) {
|
|
|
59
87
|
const res = _generateWorkbookMarkdown(workbook, schema);
|
|
60
88
|
return res;
|
|
61
89
|
}
|
|
62
|
-
export function parseTableFromFile(source, schema) {
|
|
90
|
+
export async function parseTableFromFile(source, schema) {
|
|
91
|
+
await ensureNodeEnvironment();
|
|
63
92
|
const source_resolved = resolveToVirtualPath(source);
|
|
64
93
|
const res = _parseTableFromFile(source_resolved, schema);
|
|
65
94
|
return new Table(res);
|
|
66
95
|
}
|
|
67
|
-
export function parseWorkbookFromFile(source, schema) {
|
|
96
|
+
export async function parseWorkbookFromFile(source, schema) {
|
|
97
|
+
await ensureNodeEnvironment();
|
|
68
98
|
const source_resolved = resolveToVirtualPath(source);
|
|
69
99
|
const res = _parseWorkbookFromFile(source_resolved, schema);
|
|
70
100
|
return new Workbook(res);
|
|
71
101
|
}
|
|
72
|
-
export function scanTablesFromFile(source, schema) {
|
|
102
|
+
export async function scanTablesFromFile(source, schema) {
|
|
103
|
+
await ensureNodeEnvironment();
|
|
73
104
|
const source_resolved = resolveToVirtualPath(source);
|
|
74
105
|
const res = _scanTablesFromFile(source_resolved, schema);
|
|
75
106
|
return res.map((x) => new Table(x));
|
package/dist/parser.core.wasm
CHANGED
|
Binary file
|
package/dist/parser.core8.wasm
CHANGED
|
Binary file
|
package/dist/parser.d.ts
CHANGED
|
@@ -63,6 +63,6 @@ export function tableInsertColumn(selfObj: Table, colIdx: number): Table;
|
|
|
63
63
|
export function sheetGetTable(selfObj: Sheet, name: string): Table | undefined;
|
|
64
64
|
export function sheetToMarkdown(selfObj: Sheet, schema: ParsingSchema | undefined): string;
|
|
65
65
|
export function workbookGetSheet(selfObj: Workbook, name: string): Sheet | undefined;
|
|
66
|
-
export function workbookToMarkdown(selfObj: Workbook, schema: MultiTableParsingSchema): string;
|
|
66
|
+
export function workbookToMarkdown(selfObj: Workbook, schema: MultiTableParsingSchema | undefined): string;
|
|
67
67
|
export function workbookAddSheet(selfObj: Workbook, name: string): Workbook;
|
|
68
68
|
export function workbookDeleteSheet(selfObj: Workbook, index: number): Workbook;
|
package/dist/parser.js
CHANGED
|
@@ -24381,7 +24381,7 @@ function workbookGetSheet(arg0, arg1) {
|
|
|
24381
24381
|
let exports11WorkbookToMarkdown;
|
|
24382
24382
|
|
|
24383
24383
|
function workbookToMarkdown(arg0, arg1) {
|
|
24384
|
-
var ptr0 = realloc3(0, 0, 4,
|
|
24384
|
+
var ptr0 = realloc3(0, 0, 4, 88);
|
|
24385
24385
|
var {sheets: v1_0, metadata: v1_1 } = arg0;
|
|
24386
24386
|
var vec25 = v1_0;
|
|
24387
24387
|
var len25 = vec25.length;
|
|
@@ -24534,87 +24534,94 @@ function workbookToMarkdown(arg0, arg1) {
|
|
|
24534
24534
|
dataView(memory0).setUint32(ptr0 + 16, len26, true);
|
|
24535
24535
|
dataView(memory0).setUint32(ptr0 + 12, ptr26, true);
|
|
24536
24536
|
}
|
|
24537
|
-
var
|
|
24538
|
-
|
|
24539
|
-
if (variant30 === null || variant30=== undefined) {
|
|
24537
|
+
var variant41 = arg1;
|
|
24538
|
+
if (variant41 === null || variant41=== undefined) {
|
|
24540
24539
|
dataView(memory0).setInt8(ptr0 + 20, 0, true);
|
|
24541
24540
|
} else {
|
|
24542
|
-
const e =
|
|
24541
|
+
const e = variant41;
|
|
24543
24542
|
dataView(memory0).setInt8(ptr0 + 20, 1, true);
|
|
24544
|
-
var
|
|
24545
|
-
var
|
|
24546
|
-
|
|
24547
|
-
|
|
24548
|
-
|
|
24549
|
-
|
|
24550
|
-
|
|
24551
|
-
|
|
24552
|
-
|
|
24553
|
-
|
|
24554
|
-
|
|
24555
|
-
|
|
24556
|
-
var
|
|
24557
|
-
|
|
24558
|
-
|
|
24559
|
-
|
|
24560
|
-
|
|
24561
|
-
|
|
24562
|
-
|
|
24563
|
-
|
|
24564
|
-
|
|
24565
|
-
|
|
24566
|
-
|
|
24567
|
-
|
|
24568
|
-
|
|
24569
|
-
|
|
24570
|
-
|
|
24571
|
-
|
|
24572
|
-
|
|
24573
|
-
|
|
24574
|
-
|
|
24575
|
-
|
|
24576
|
-
|
|
24577
|
-
|
|
24578
|
-
|
|
24579
|
-
|
|
24580
|
-
|
|
24581
|
-
|
|
24582
|
-
|
|
24583
|
-
|
|
24584
|
-
|
|
24585
|
-
|
|
24586
|
-
|
|
24587
|
-
|
|
24588
|
-
|
|
24589
|
-
|
|
24590
|
-
|
|
24591
|
-
var
|
|
24592
|
-
|
|
24593
|
-
|
|
24594
|
-
|
|
24595
|
-
|
|
24596
|
-
|
|
24597
|
-
|
|
24598
|
-
|
|
24599
|
-
|
|
24600
|
-
|
|
24601
|
-
|
|
24602
|
-
|
|
24603
|
-
|
|
24604
|
-
|
|
24605
|
-
|
|
24606
|
-
|
|
24607
|
-
|
|
24608
|
-
|
|
24609
|
-
|
|
24610
|
-
|
|
24611
|
-
|
|
24612
|
-
|
|
24613
|
-
|
|
24614
|
-
|
|
24615
|
-
|
|
24616
|
-
|
|
24617
|
-
|
|
24543
|
+
var {columnSeparator: v28_0, headerSeparatorChar: v28_1, requireOuterPipes: v28_2, stripWhitespace: v28_3, convertBrToNewline: v28_4, rootMarker: v28_5, sheetHeaderLevel: v28_6, tableHeaderLevel: v28_7, captureDescription: v28_8 } = e;
|
|
24544
|
+
var variant30 = v28_0;
|
|
24545
|
+
if (variant30 === null || variant30=== undefined) {
|
|
24546
|
+
dataView(memory0).setInt8(ptr0 + 24, 0, true);
|
|
24547
|
+
} else {
|
|
24548
|
+
const e = variant30;
|
|
24549
|
+
dataView(memory0).setInt8(ptr0 + 24, 1, true);
|
|
24550
|
+
var ptr29 = utf8Encode(e, realloc3, memory0);
|
|
24551
|
+
var len29 = utf8EncodedLen;
|
|
24552
|
+
dataView(memory0).setUint32(ptr0 + 32, len29, true);
|
|
24553
|
+
dataView(memory0).setUint32(ptr0 + 28, ptr29, true);
|
|
24554
|
+
}
|
|
24555
|
+
var variant32 = v28_1;
|
|
24556
|
+
if (variant32 === null || variant32=== undefined) {
|
|
24557
|
+
dataView(memory0).setInt8(ptr0 + 36, 0, true);
|
|
24558
|
+
} else {
|
|
24559
|
+
const e = variant32;
|
|
24560
|
+
dataView(memory0).setInt8(ptr0 + 36, 1, true);
|
|
24561
|
+
var ptr31 = utf8Encode(e, realloc3, memory0);
|
|
24562
|
+
var len31 = utf8EncodedLen;
|
|
24563
|
+
dataView(memory0).setUint32(ptr0 + 44, len31, true);
|
|
24564
|
+
dataView(memory0).setUint32(ptr0 + 40, ptr31, true);
|
|
24565
|
+
}
|
|
24566
|
+
var variant33 = v28_2;
|
|
24567
|
+
if (variant33 === null || variant33=== undefined) {
|
|
24568
|
+
dataView(memory0).setInt8(ptr0 + 48, 0, true);
|
|
24569
|
+
} else {
|
|
24570
|
+
const e = variant33;
|
|
24571
|
+
dataView(memory0).setInt8(ptr0 + 48, 1, true);
|
|
24572
|
+
dataView(memory0).setInt8(ptr0 + 49, e ? 1 : 0, true);
|
|
24573
|
+
}
|
|
24574
|
+
var variant34 = v28_3;
|
|
24575
|
+
if (variant34 === null || variant34=== undefined) {
|
|
24576
|
+
dataView(memory0).setInt8(ptr0 + 50, 0, true);
|
|
24577
|
+
} else {
|
|
24578
|
+
const e = variant34;
|
|
24579
|
+
dataView(memory0).setInt8(ptr0 + 50, 1, true);
|
|
24580
|
+
dataView(memory0).setInt8(ptr0 + 51, e ? 1 : 0, true);
|
|
24581
|
+
}
|
|
24582
|
+
var variant35 = v28_4;
|
|
24583
|
+
if (variant35 === null || variant35=== undefined) {
|
|
24584
|
+
dataView(memory0).setInt8(ptr0 + 52, 0, true);
|
|
24585
|
+
} else {
|
|
24586
|
+
const e = variant35;
|
|
24587
|
+
dataView(memory0).setInt8(ptr0 + 52, 1, true);
|
|
24588
|
+
dataView(memory0).setInt8(ptr0 + 53, e ? 1 : 0, true);
|
|
24589
|
+
}
|
|
24590
|
+
var variant37 = v28_5;
|
|
24591
|
+
if (variant37 === null || variant37=== undefined) {
|
|
24592
|
+
dataView(memory0).setInt8(ptr0 + 56, 0, true);
|
|
24593
|
+
} else {
|
|
24594
|
+
const e = variant37;
|
|
24595
|
+
dataView(memory0).setInt8(ptr0 + 56, 1, true);
|
|
24596
|
+
var ptr36 = utf8Encode(e, realloc3, memory0);
|
|
24597
|
+
var len36 = utf8EncodedLen;
|
|
24598
|
+
dataView(memory0).setUint32(ptr0 + 64, len36, true);
|
|
24599
|
+
dataView(memory0).setUint32(ptr0 + 60, ptr36, true);
|
|
24600
|
+
}
|
|
24601
|
+
var variant38 = v28_6;
|
|
24602
|
+
if (variant38 === null || variant38=== undefined) {
|
|
24603
|
+
dataView(memory0).setInt8(ptr0 + 68, 0, true);
|
|
24604
|
+
} else {
|
|
24605
|
+
const e = variant38;
|
|
24606
|
+
dataView(memory0).setInt8(ptr0 + 68, 1, true);
|
|
24607
|
+
dataView(memory0).setInt32(ptr0 + 72, toInt32(e), true);
|
|
24608
|
+
}
|
|
24609
|
+
var variant39 = v28_7;
|
|
24610
|
+
if (variant39 === null || variant39=== undefined) {
|
|
24611
|
+
dataView(memory0).setInt8(ptr0 + 76, 0, true);
|
|
24612
|
+
} else {
|
|
24613
|
+
const e = variant39;
|
|
24614
|
+
dataView(memory0).setInt8(ptr0 + 76, 1, true);
|
|
24615
|
+
dataView(memory0).setInt32(ptr0 + 80, toInt32(e), true);
|
|
24616
|
+
}
|
|
24617
|
+
var variant40 = v28_8;
|
|
24618
|
+
if (variant40 === null || variant40=== undefined) {
|
|
24619
|
+
dataView(memory0).setInt8(ptr0 + 84, 0, true);
|
|
24620
|
+
} else {
|
|
24621
|
+
const e = variant40;
|
|
24622
|
+
dataView(memory0).setInt8(ptr0 + 84, 1, true);
|
|
24623
|
+
dataView(memory0).setInt8(ptr0 + 85, e ? 1 : 0, true);
|
|
24624
|
+
}
|
|
24618
24625
|
}
|
|
24619
24626
|
_debugLog('[iface="workbook-to-markdown", function="workbook-to-markdown"][Instruction::CallWasm] enter', {
|
|
24620
24627
|
funcName: 'workbook-to-markdown',
|
|
@@ -24625,16 +24632,16 @@ function workbookToMarkdown(arg0, arg1) {
|
|
|
24625
24632
|
const _wasm_call_currentTaskID = startCurrentTask(0, false, 'exports11WorkbookToMarkdown');
|
|
24626
24633
|
const ret = exports11WorkbookToMarkdown(ptr0);
|
|
24627
24634
|
endCurrentTask(0);
|
|
24628
|
-
var
|
|
24629
|
-
var
|
|
24630
|
-
var
|
|
24635
|
+
var ptr42 = dataView(memory0).getUint32(ret + 0, true);
|
|
24636
|
+
var len42 = dataView(memory0).getUint32(ret + 4, true);
|
|
24637
|
+
var result42 = utf8Decoder.decode(new Uint8Array(memory0.buffer, ptr42, len42));
|
|
24631
24638
|
_debugLog('[iface="workbook-to-markdown", function="workbook-to-markdown"][Instruction::Return]', {
|
|
24632
24639
|
funcName: 'workbook-to-markdown',
|
|
24633
24640
|
paramCount: 1,
|
|
24634
24641
|
async: false,
|
|
24635
24642
|
postReturn: true
|
|
24636
24643
|
});
|
|
24637
|
-
const retCopy =
|
|
24644
|
+
const retCopy = result42;
|
|
24638
24645
|
|
|
24639
24646
|
let cstate = getOrCreateAsyncState(0);
|
|
24640
24647
|
cstate.mayLeave = false;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "md-spreadsheet-parser",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.7",
|
|
4
4
|
"description": "A robust Markdown table parser and manipulator, powered by Python and WebAssembly.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -29,6 +29,7 @@
|
|
|
29
29
|
"license": "MIT",
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@bytecodealliance/jco": "^1.0.0",
|
|
32
|
+
"@types/node": "^25.0.3",
|
|
32
33
|
"typescript": "^5.9.3",
|
|
33
34
|
"zod": "^4.3.4"
|
|
34
35
|
},
|
|
Binary file
|
|
Binary file
|
package/src/index.ts
CHANGED
|
@@ -1,17 +1,50 @@
|
|
|
1
1
|
import { cleanCell as _cleanCell, splitRowGfm as _splitRowGfm, parseRow as _parseRow, parseSeparatorRow as _parseSeparatorRow, isSeparatorRow as _isSeparatorRow, parseTable as _parseTable, parseSheet as _parseSheet, parseWorkbook as _parseWorkbook, scanTables as _scanTables, generateTableMarkdown as _generateTableMarkdown, generateSheetMarkdown as _generateSheetMarkdown, generateWorkbookMarkdown as _generateWorkbookMarkdown, parseTableFromFile as _parseTableFromFile, parseWorkbookFromFile as _parseWorkbookFromFile, scanTablesFromFile as _scanTablesFromFile, scanTablesIter as _scanTablesIter, tableToModels as _tableToModels, tableToMarkdown as _tableToMarkdown, tableUpdateCell as _tableUpdateCell, tableDeleteRow as _tableDeleteRow, tableDeleteColumn as _tableDeleteColumn, tableClearColumnData as _tableClearColumnData, tableInsertRow as _tableInsertRow, tableInsertColumn as _tableInsertColumn, sheetGetTable as _sheetGetTable, sheetToMarkdown as _sheetToMarkdown, workbookGetSheet as _workbookGetSheet, workbookToMarkdown as _workbookToMarkdown, workbookAddSheet as _workbookAddSheet, workbookDeleteSheet as _workbookDeleteSheet } from '../dist/parser.js';
|
|
2
|
-
// @ts-ignore
|
|
3
|
-
import path from 'node:path';
|
|
4
|
-
// @ts-ignore
|
|
5
|
-
import process from 'node:process';
|
|
6
|
-
// @ts-ignore
|
|
7
|
-
import { _addPreopen } from '@bytecodealliance/preview2-shim/filesystem';
|
|
8
2
|
import { clientSideToModels } from './client-adapters.js';
|
|
9
3
|
|
|
10
|
-
//
|
|
11
|
-
|
|
4
|
+
// Environment detection
|
|
5
|
+
// @ts-ignore - process may not be defined in browser
|
|
6
|
+
const isNode = typeof process !== 'undefined'
|
|
7
|
+
&& typeof process.versions !== 'undefined'
|
|
8
|
+
&& typeof process.versions.node !== 'undefined';
|
|
9
|
+
|
|
10
|
+
// Lazily loaded Node.js modules (only in Node.js environment)
|
|
11
|
+
let _pathModule: any = null;
|
|
12
|
+
let _nodeInitialized = false;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Ensures Node.js environment is initialized for file system operations.
|
|
16
|
+
* Throws an error in browser environments.
|
|
17
|
+
*/
|
|
18
|
+
async function ensureNodeEnvironment(): Promise<void> {
|
|
19
|
+
if (!isNode) {
|
|
20
|
+
throw new Error(
|
|
21
|
+
'File system operations (parseTableFromFile, parseWorkbookFromFile, scanTablesFromFile) ' +
|
|
22
|
+
'are not supported in browser environments. ' +
|
|
23
|
+
'Use parseTable(), parseWorkbook(), or scanTables() with string content instead.'
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
if (_nodeInitialized) return;
|
|
27
|
+
|
|
28
|
+
// Dynamic imports for Node.js only
|
|
29
|
+
const [pathModule, processModule, fsShim] = await Promise.all([
|
|
30
|
+
import('node:path'),
|
|
31
|
+
import('node:process'),
|
|
32
|
+
import('@bytecodealliance/preview2-shim/filesystem')
|
|
33
|
+
]);
|
|
34
|
+
|
|
35
|
+
_pathModule = pathModule.default || pathModule;
|
|
36
|
+
const proc = processModule.default || processModule;
|
|
37
|
+
const root = _pathModule.parse(proc.cwd()).root;
|
|
38
|
+
// @ts-ignore - _addPreopen is an internal function
|
|
39
|
+
(fsShim as any)._addPreopen('/', root);
|
|
40
|
+
_nodeInitialized = true;
|
|
41
|
+
}
|
|
12
42
|
|
|
13
|
-
function resolveToVirtualPath(p: string) {
|
|
14
|
-
|
|
43
|
+
function resolveToVirtualPath(p: string): string {
|
|
44
|
+
if (!_pathModule) {
|
|
45
|
+
throw new Error('Node.js modules not initialized. Call ensureNodeEnvironment() first.');
|
|
46
|
+
}
|
|
47
|
+
return _pathModule.resolve(p);
|
|
15
48
|
}
|
|
16
49
|
|
|
17
50
|
export function cleanCell(cell: any, schema: any): any {
|
|
@@ -74,19 +107,22 @@ export function generateWorkbookMarkdown(workbook: any, schema: any): any {
|
|
|
74
107
|
return res;
|
|
75
108
|
}
|
|
76
109
|
|
|
77
|
-
export function parseTableFromFile(source: any, schema?: any): any {
|
|
110
|
+
export async function parseTableFromFile(source: any, schema?: any): Promise<any> {
|
|
111
|
+
await ensureNodeEnvironment();
|
|
78
112
|
const source_resolved = resolveToVirtualPath(source);
|
|
79
113
|
const res = _parseTableFromFile(source_resolved, schema);
|
|
80
114
|
return new Table(res);
|
|
81
115
|
}
|
|
82
116
|
|
|
83
|
-
export function parseWorkbookFromFile(source: any, schema?: any): any {
|
|
117
|
+
export async function parseWorkbookFromFile(source: any, schema?: any): Promise<any> {
|
|
118
|
+
await ensureNodeEnvironment();
|
|
84
119
|
const source_resolved = resolveToVirtualPath(source);
|
|
85
120
|
const res = _parseWorkbookFromFile(source_resolved, schema);
|
|
86
121
|
return new Workbook(res);
|
|
87
122
|
}
|
|
88
123
|
|
|
89
|
-
export function scanTablesFromFile(source: any, schema?: any): any {
|
|
124
|
+
export async function scanTablesFromFile(source: any, schema?: any): Promise<any> {
|
|
125
|
+
await ensureNodeEnvironment();
|
|
90
126
|
const source_resolved = resolveToVirtualPath(source);
|
|
91
127
|
const res = _scanTablesFromFile(source_resolved, schema);
|
|
92
128
|
return res.map((x: any) => new Table(x));
|
|
@@ -243,7 +279,7 @@ export class Workbook {
|
|
|
243
279
|
return res ? new Sheet(res) : undefined;
|
|
244
280
|
}
|
|
245
281
|
|
|
246
|
-
toMarkdown(schema
|
|
282
|
+
toMarkdown(schema?: any): any {
|
|
247
283
|
const dto = this.toDTO();
|
|
248
284
|
const res = _workbookToMarkdown(dto, schema);
|
|
249
285
|
return res;
|