mixmark-io / turndown

🛏 An HTML to Markdown converter written in JavaScript
https://mixmark-io.github.io/turndown
MIT License
8.93k stars 880 forks source link

error while running with webpack bundle in nodejs #439

Closed mohit-singh-pepper closed 6 months ago

mohit-singh-pepper commented 1 year ago

Getting following error while trying to run turndown. I am using esbuild to get the bundle for same.

/Users/mohitsingh/Documents/abc/dom-text-abc-function/dist/main.js:21918
        } else if (stack.top instanceof impl.HTMLTemplateElement) {
                             ^

TypeError: Right-hand side of 'instanceof' is not an object
    at insertElement (/Users/mohitsingh/Documents/abc/dom-text-scrapper-agc/dist/main.js:21918:30)
    at insertHTMLElement (/Users/mohitsingh/Documents/abc/dom-text-agc-function/dist/main.js:21906:19)
    at before_head_mode (/Users/mohitsingh/Documents/abc/dom-text-agc-function/dist/main.js:24842:27)
    at before_head_mode (/Users/mohitsingh/Documents/abc/dom-text-abc-function/dist/main.js:24859:9)
    at before_html_mode (/Users/mohitsingh/Documents/abc/dom-text-abc-function/dist/main.js:24822:9)
    at initial_mode (/Users/mohitsingh/Documents/abc/dom-text-abc-function/dist/main.js:24783:9)
mohit-singh-pepper commented 1 year ago

Its due to this https://github.com/fgnass/domino/issues/146

mohit-singh-pepper commented 1 year ago

Is there a workaround, anyone know of ?

martincizek commented 1 year ago

Not familiar with webpack, so the workaround in general terms:

  1. use the browser build as the dependency - the browser build does not contain domino (see package.js)
  2. if your build is for a browser, you're done. If it is for an environment without DOM parser, you need to bring your own DOM parser (e.g. jsdom) and call turndown with a DOM object instead of string.

For reference, discussion about separating turndown and the DOM implementation: #265 .

castroCrea commented 1 year ago

@mohit-singh-pepper did you found a solution ?

baltagih2 commented 7 months ago

For anyone still running into this problem, the following worked for me (based on the commit from @julienc91 above):

npm i jsdom npm i --save-dev @types/jsdom

import { JSDOM } from "jsdom";
import TurndownService from "turndown";

const turndownService = new TurndownService();

const result = turndownService.turndown(
      JSDOM.fragment(your_html_content)
    );
pavelhoral commented 6 months ago

I think this issue should be solved with the latest Turndown release 7.2.0. Can you confirm @mohit-singh-pepper ?

pavelhoral commented 6 months ago

This should be solved with the introduction of custom domino fork in the latest release #461.