flex-md 4.5.0 → 4.5.3
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/dist/flex-md-loader.d.ts +35 -0
- package/dist/flex-md-loader.js +99 -0
- package/package.json +2 -2
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flex-MD Loader - ES Module Compatible Dynamic Import
|
|
3
|
+
*
|
|
4
|
+
* This module provides ES module compatible dynamic loading of the flex-md library,
|
|
5
|
+
* handling the case where dependencies might use require() in ES module environments.
|
|
6
|
+
*/
|
|
7
|
+
interface FlexMdExtractionOptions {
|
|
8
|
+
instructions?: string;
|
|
9
|
+
complianceLevel?: 'L1' | 'L2' | 'L3';
|
|
10
|
+
}
|
|
11
|
+
interface FlexMdExtractionResult {
|
|
12
|
+
json: Record<string, any>;
|
|
13
|
+
success: boolean;
|
|
14
|
+
error?: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Dynamically loads the flex-md library with ES module compatibility.
|
|
18
|
+
* Handles both ES module and CommonJS loading patterns.
|
|
19
|
+
*/
|
|
20
|
+
declare function loadFlexMd(): Promise<any>;
|
|
21
|
+
/**
|
|
22
|
+
* Extracts structured JSON from markdown content using flex-md.
|
|
23
|
+
*
|
|
24
|
+
* @param content - The markdown content to parse
|
|
25
|
+
* @param spec - Optional Output Format Specification
|
|
26
|
+
* @param options - Extraction options
|
|
27
|
+
* @returns Promise resolving to extraction result
|
|
28
|
+
*/
|
|
29
|
+
export declare function extractJsonFromFlexMd(content: string, spec?: any, options?: FlexMdExtractionOptions): Promise<FlexMdExtractionResult>;
|
|
30
|
+
/**
|
|
31
|
+
* Alternative extraction using transformWithOfs if a spec is provided.
|
|
32
|
+
* This provides more structured extraction when an Output Format Spec is available.
|
|
33
|
+
*/
|
|
34
|
+
export declare function extractWithOfs(content: string, spec: any, options?: FlexMdExtractionOptions): Promise<FlexMdExtractionResult>;
|
|
35
|
+
export { loadFlexMd };
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flex-MD Loader - ES Module Compatible Dynamic Import
|
|
3
|
+
*
|
|
4
|
+
* This module provides ES module compatible dynamic loading of the flex-md library,
|
|
5
|
+
* handling the case where dependencies might use require() in ES module environments.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Dynamically loads the flex-md library with ES module compatibility.
|
|
9
|
+
* Handles both ES module and CommonJS loading patterns.
|
|
10
|
+
*/
|
|
11
|
+
async function loadFlexMd() {
|
|
12
|
+
try {
|
|
13
|
+
// First, try direct ES module import (preferred for modern environments)
|
|
14
|
+
const flexMdModule = await import('flex-md');
|
|
15
|
+
return flexMdModule;
|
|
16
|
+
}
|
|
17
|
+
catch (esError) {
|
|
18
|
+
try {
|
|
19
|
+
// Fallback: Use Node.js createRequire for CommonJS compatibility
|
|
20
|
+
// This handles cases where flex-md or its dependencies use require()
|
|
21
|
+
const { createRequire } = await import('module');
|
|
22
|
+
const require = createRequire(import.meta.url);
|
|
23
|
+
const flexMdModule = require('flex-md');
|
|
24
|
+
return flexMdModule;
|
|
25
|
+
}
|
|
26
|
+
catch (cjsError) {
|
|
27
|
+
// If both fail, try the dynamic Function approach as last resort
|
|
28
|
+
try {
|
|
29
|
+
const importFunc = new Function('specifier', 'return import(specifier)');
|
|
30
|
+
const flexMdModule = await importFunc('flex-md');
|
|
31
|
+
return flexMdModule;
|
|
32
|
+
}
|
|
33
|
+
catch (dynamicError) {
|
|
34
|
+
throw new Error(`flex-md import failed: ES module error: ${esError instanceof Error ? esError.message : String(esError)}, ` +
|
|
35
|
+
`CommonJS fallback error: ${cjsError instanceof Error ? cjsError.message : String(cjsError)}, ` +
|
|
36
|
+
`Dynamic import error: ${dynamicError instanceof Error ? dynamicError.message : String(dynamicError)}`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Extracts structured JSON from markdown content using flex-md.
|
|
43
|
+
*
|
|
44
|
+
* @param content - The markdown content to parse
|
|
45
|
+
* @param spec - Optional Output Format Specification
|
|
46
|
+
* @param options - Extraction options
|
|
47
|
+
* @returns Promise resolving to extraction result
|
|
48
|
+
*/
|
|
49
|
+
export async function extractJsonFromFlexMd(content, spec, options = {}) {
|
|
50
|
+
try {
|
|
51
|
+
// Load flex-md library with compatibility handling
|
|
52
|
+
const flexMd = await loadFlexMd();
|
|
53
|
+
// Use the markdownToJson function for basic structured extraction
|
|
54
|
+
// This handles markdown with sections like # Short answer, # Full answer, etc.
|
|
55
|
+
const json = flexMd.markdownToJson(content);
|
|
56
|
+
return {
|
|
57
|
+
json,
|
|
58
|
+
success: true
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
catch (error) {
|
|
62
|
+
console.warn('Flex-md extraction failed - flex-md library compatibility issue');
|
|
63
|
+
console.warn('Error:', error instanceof Error ? error.message : String(error));
|
|
64
|
+
console.warn('Issue: flex-md uses require() in ES module context - needs fixing in flex-md-loader.ts');
|
|
65
|
+
return {
|
|
66
|
+
json: { rawText: content },
|
|
67
|
+
success: false,
|
|
68
|
+
error: error instanceof Error ? error.message : 'Unknown extraction error'
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Alternative extraction using transformWithOfs if a spec is provided.
|
|
74
|
+
* This provides more structured extraction when an Output Format Spec is available.
|
|
75
|
+
*/
|
|
76
|
+
export async function extractWithOfs(content, spec, options = {}) {
|
|
77
|
+
try {
|
|
78
|
+
const flexMd = await loadFlexMd();
|
|
79
|
+
// If spec is provided, use the more advanced transformWithOfs
|
|
80
|
+
if (spec && flexMd.transformWithOfs) {
|
|
81
|
+
const result = flexMd.transformWithOfs(content, spec);
|
|
82
|
+
return {
|
|
83
|
+
json: result,
|
|
84
|
+
success: true
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
// Fallback to basic markdownToJson
|
|
89
|
+
return extractJsonFromFlexMd(content, spec, options);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
catch (error) {
|
|
93
|
+
console.warn('Flex-md OFS extraction failed:', error instanceof Error ? error.message : String(error));
|
|
94
|
+
// Fallback to basic extraction
|
|
95
|
+
return extractJsonFromFlexMd(content, spec, options);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
// Export the loader function for advanced use cases
|
|
99
|
+
export { loadFlexMd };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "flex-md",
|
|
3
|
-
"version": "4.5.
|
|
3
|
+
"version": "4.5.3",
|
|
4
4
|
"description": "Parse and stringify FlexMD: semi-structured Markdown with three powerful layers - Frames, Output Format Spec (OFS), and Detection/Extraction.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "",
|
|
@@ -56,6 +56,6 @@
|
|
|
56
56
|
"nd": "^1.2.0",
|
|
57
57
|
"nx-helpers": "^1.5.0",
|
|
58
58
|
"nx-json-parser": "^1.2.1",
|
|
59
|
-
"nx-md-parser": "^2.2.
|
|
59
|
+
"nx-md-parser": "^2.2.1"
|
|
60
60
|
}
|
|
61
61
|
}
|