crosstype / node-html-markdown

Fast HTML to markdown converter for NodeJS or the browser
163 stars 28 forks source link

iframe cannot be left in the markdown #19

Closed 4www closed 3 years ago

4www commented 3 years ago

hello,

I do not find a way to let the iframe element in the final markdown.

Would you help me understand what I am missing?

It does not seem that it finds any iframe element, with a <iframe/> or <iframe></iframe> version.

Should'nt the iframe key translator be triggered?

https://codesandbox.io/s/node-html-markdowniframe-strong-test-n52nb?file=/src/index.js:234-237

Cheers!

Notes

It seems that if the iframe is in block version, and as content within its blocks, the iframe is found (with the setup in the link)

// version that works
const html = `
<p>
  <span>Hello</span>
  <iframe src="https://radio4000.com">test inner content</iframe>
  <strong>World</strong>
</p>
`;

// not working, but should it?
const html = `
<p>
  <span>Hello</span>
  <iframe src="https://radio4000.com"/>
  <strong>World</strong>
</p>
`;

It there anything missing in the configuration of the iframe translator that is missing, or should be setup differently to make it work with single html element version?

nonara commented 3 years ago

Thanks for the report. Cool to see someone using the customizable options! I'll have a look this weekend. I think the parser is ignoring them by default, but I believe we can get around that.

nonara commented 3 years ago

Thanks again for the thorough report. The issue was that we pre-optimize the tree beforehand to flag as ignored any elements without any children. For speed reasons, this flag happens separately, as it allows us to save a lot of instructions by not having to ask about or instantiate any translator config, etc for these sorts of nodes.

That said, I can certainly see where someone using custom translators would find this to be a hinderance.

In order to maintain performance but allow this functionality, I've added the preserveIfEmpty option to TranslatorConfig.

https://github.com/crosstype/node-html-markdown/blob/07f0fa96d815e1552a4adf37ee039e96fe68e9e4/src/translator.ts#L79-L85

You should be able to accomplish what you need now in v1.1.0, via:

const markdown = NodeHtmlMarkdown.translate(
  sHtml,
  undefined,
  {
    iframe: {
      preserveIfEmpty: true,
      postprocess: () => {
        console.log("iframe found");
      }
    }
  }
);

Let me know if that doesn't work for you.

4www commented 3 years ago

Hej! Thanks a lot for the fix. I will try the new version, really happy to cleanup some of my code :}

Cheers!