legal-markdown-js
Version:
Node.js implementation of LegalMarkdown for processing legal documents with markdown and YAML - Complete feature parity with Ruby version
87 lines • 2.61 kB
JavaScript
import { ContentDetector } from './content-detector.js';
/**
* Dynamic pandoc loader with conditional loading
*/
export class PandocLoader {
static pandocWasm = null;
static loadingPromise = null;
/**
* Loads pandoc only if content needs it
*/
static async loadIfNeeded(content) {
if (!ContentDetector.needsPandoc(content)) {
return false; // No necesita pandoc
}
if (typeof window === 'undefined') {
// Node.js environment - assume pandoc is available
return true;
}
// Browser environment - load pandoc-wasm
if (this.pandocWasm) {
return true; // Already loaded
}
if (this.loadingPromise) {
// Loading in progress, wait for it
try {
await this.loadingPromise;
return this.pandocWasm !== null;
}
catch (error) {
return false;
}
}
// Start loading
this.loadingPromise = this.loadPandocWasm();
try {
this.pandocWasm = await this.loadingPromise;
return true;
}
catch (error) {
console.warn('Failed to load pandoc-wasm:', error);
return false;
}
finally {
this.loadingPromise = null;
}
}
/**
* Get loaded pandoc instance
*/
static getPandocWasm() {
return this.pandocWasm;
}
/**
* Load pandoc-wasm module
*/
static async loadPandocWasm() {
try {
// Dynamic import for pandoc-wasm
const pandocModule = await import('pandoc-wasm');
// Initialize pandoc-wasm - different pandoc-wasm versions have different APIs
if (typeof pandocModule.init === 'function') {
return await pandocModule.init();
}
else if (typeof pandocModule.default === 'function') {
return await pandocModule.default();
}
else if (typeof pandocModule.default === 'object' &&
pandocModule.default.convert) {
return pandocModule.default;
}
else {
return pandocModule;
}
}
catch (error) {
throw new Error(`Failed to load pandoc-wasm: ${error.message}`);
}
}
/**
* Reset loader state (mainly for testing)
*/
static reset() {
this.pandocWasm = null;
this.loadingPromise = null;
}
}
//# sourceMappingURL=pandoc-loader.js.map