Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

iframe cannot be left in the markdown #19

Closed
4www opened this issue Jul 15, 2021 · 3 comments
Closed

iframe cannot be left in the markdown #19

4www opened this issue Jul 15, 2021 · 3 comments

Comments

@4www
Copy link

4www commented Jul 15, 2021

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
Copy link
Collaborator

nonara commented Jul 23, 2021

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 added a commit that referenced this issue Jul 26, 2021
…ranslators that do not have children (closes #19)
@nonara nonara closed this as completed in c3d8829 Jul 26, 2021
@nonara
Copy link
Collaborator

nonara commented Jul 26, 2021

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.

/**
* Ensure translator is always visited, even if element is empty
* Note: For speed, trees are optimized beforehand to only visit elements which have child nodes or text content.
* In some cases, however, you may want to create or alter a translator to be triggered even if the element is empty.
* (If using a TranslatorConfigFactory, this value is always treated as true)
*/
preserveIfEmpty?: boolean

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
Copy link
Author

4www commented Jul 27, 2021

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

Cheers!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants