file2md
Version:
A TypeScript library for converting various document types (PDF, DOCX, XLSX, PPTX, HWP, HWPX) into Markdown with image and layout preservation
154 lines • 4.41 kB
JavaScript
/**
* Browser API polyfills for Node.js environment
* Provides minimal implementations of browser APIs needed by hwp.js
*/
/**
* IntersectionObserver polyfill for Node.js
* Provides a minimal implementation that doesn't actually observe anything
*/
export class IntersectionObserver {
root = null;
rootMargin = '0px';
thresholds = [0];
callback;
options;
constructor(callback, options) {
this.callback = callback;
this.options = options;
if (options) {
this.root = options.root || null;
this.rootMargin = options.rootMargin || '0px';
this.thresholds = Array.isArray(options.threshold) ? options.threshold : [options.threshold || 0];
}
}
observe(target) {
// In Node.js, we immediately trigger the callback with a mock entry
// indicating the element is intersecting
const mockEntry = {
target,
boundingClientRect: {
x: 0, y: 0, width: 100, height: 100,
top: 0, left: 0, bottom: 100, right: 100,
toJSON: () => ({})
},
intersectionRatio: 1.0,
intersectionRect: {
x: 0, y: 0, width: 100, height: 100,
top: 0, left: 0, bottom: 100, right: 100,
toJSON: () => ({})
},
isIntersecting: true,
rootBounds: null,
time: Date.now()
};
// Trigger callback asynchronously
setTimeout(() => {
this.callback([mockEntry], this);
}, 0);
}
unobserve(_target) {
// No-op in Node.js
}
disconnect() {
// No-op in Node.js
}
takeRecords() {
return [];
}
}
/**
* ResizeObserver polyfill for Node.js
*/
export class ResizeObserver {
callback;
constructor(callback) {
this.callback = callback;
}
observe(target, _options) {
// Mock resize entry
const mockEntry = {
target,
contentRect: {
x: 0, y: 0, width: 100, height: 100,
top: 0, left: 0, bottom: 100, right: 100,
toJSON: () => ({})
},
borderBoxSize: [],
contentBoxSize: [],
devicePixelContentBoxSize: []
};
setTimeout(() => {
this.callback([mockEntry], this);
}, 0);
}
unobserve(_target) {
// No-op
}
disconnect() {
// No-op
}
}
/**
* MutationObserver polyfill for Node.js
*/
export class MutationObserver {
callback;
constructor(callback) {
this.callback = callback;
}
observe(_target, _options) {
// No-op in Node.js - just don't call the callback
}
disconnect() {
// No-op
}
takeRecords() {
return [];
}
}
/**
* Setup polyfills in the global scope
* This should be called before importing hwp.js
*/
export function setupBrowserPolyfills() {
// Only set up if not already defined (avoid overriding in browser environment)
if (typeof global !== 'undefined') {
if (!global.IntersectionObserver) {
global.IntersectionObserver = IntersectionObserver;
}
if (!global.ResizeObserver) {
global.ResizeObserver = ResizeObserver;
}
if (!global.MutationObserver) {
global.MutationObserver = MutationObserver;
}
// Add requestAnimationFrame polyfill if needed
if (!global.requestAnimationFrame) {
global.requestAnimationFrame = (callback) => {
return setTimeout(callback, 16); // ~60fps
};
}
if (!global.cancelAnimationFrame) {
global.cancelAnimationFrame = (id) => {
clearTimeout(id);
};
}
// Add performance.now polyfill if needed
if (!global.performance) {
global.performance = {
now: () => Date.now(),
timeOrigin: Date.now()
};
}
}
}
/**
* Cleanup polyfills from global scope
*/
export function cleanupBrowserPolyfills() {
if (typeof global !== 'undefined') {
// Note: We don't actually delete these as other code might depend on them
// This is mainly for documentation purposes
}
}
//# sourceMappingURL=browser-polyfills.js.map