legal-markdown-js
Version:
Node.js implementation of LegalMarkdown for processing legal documents with markdown and YAML - Complete feature parity with Ruby version
62 lines • 2.37 kB
JavaScript
/**
* Pandoc WebAssembly implementation for browser
*/
export class PandocWasm {
pandocWasm;
options;
constructor(pandocWasm, options = {}) {
this.pandocWasm = pandocWasm;
this.options = {
timeout: 10000, // 10 seconds default timeout
verbose: false,
...options,
};
}
/**
* Convert content using pandoc-wasm
*/
async convert(content, from, to) {
if (!this.pandocWasm) {
throw new Error('Pandoc WASM not initialized');
}
try {
if (this.options.verbose) {
console.log(`Converting ${from} to ${to} using pandoc-wasm`);
}
// Create a timeout promise
const timeoutPromise = new Promise((_, reject) => {
setTimeout(() => {
reject(new Error(`Pandoc WASM conversion timed out after ${this.options.timeout}ms`));
}, this.options.timeout);
});
// Different pandoc-wasm versions have different APIs
let conversionPromise;
if (typeof this.pandocWasm.convert === 'function') {
// Direct convert method
conversionPromise = Promise.resolve(this.pandocWasm.convert(content, from, to));
}
else if (typeof this.pandocWasm.pandoc === 'function') {
// Pandoc function interface
conversionPromise = Promise.resolve(this.pandocWasm.pandoc(content, { from, to }));
}
else if (typeof this.pandocWasm.run === 'function') {
// Run interface with arguments
const args = ['-f', from, '-t', to, '--wrap=none'];
if (this.options.args) {
args.push(...this.options.args);
}
conversionPromise = Promise.resolve(this.pandocWasm.run(args, content));
}
else {
throw new Error('Unsupported pandoc-wasm API');
}
// Race between conversion and timeout
const result = await Promise.race([conversionPromise, timeoutPromise]);
return result;
}
catch (error) {
throw new Error(`Pandoc WASM conversion failed: ${error.message}`);
}
}
}
//# sourceMappingURL=pandoc-wasm.js.map