question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

convertFromHTML tag <img /> problem

See original GitHub issue

Hi! In convertFromHTML for links i can do this:

    htmlToEntity: (nodeName, node) => {
        if (nodeName === 'a') {
            return Entity.create(
                'LINK',
                'MUTABLE',
                {url: node.href}
            )
        }
    }

For images ( tag) I trying:

    htmlToEntity: (nodeName, node) => {
        if (nodeName === 'img') {
            return Entity.create(
                'IMAGE',
                'MUTABLE',
                {src: node.src}
            )
        }
    }

But it doesn’t work. In draft-js editor img not showed.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:7

github_iconTop GitHub Comments

10reactions
benbriggscommented, Aug 1, 2016

Images get a bit more complicated because they’re not strict decorations of text. An additional atomic block type is used to have a custom block renderer, then the entity can be used to store metadata within that block. In our internal image plugin our conversion options look something like this:

const ENTITY_TYPE = 'IMAGE';
const BLOCK_TYPE = 'atomic';

export default createPlugin({
  htmlToBlock: (nodeName, node) => {
    if (nodeName === 'figure') {
      return BLOCK_TYPE;
    }
  },
  htmlToEntity: (nodeName, node) => {
    if (nodeName === 'img') {
      return Entity.create(ENTITY_TYPE, ...)
    }
  },
  blockRendererFn: (block) => {
    if (block.getType() === 'atomic' && block.length > 0 && Entity.get(block.getEntityAt(0)).getType() === ENTITY_TYPE) {
      return {
        component: ({block}) => {
          const {src} = Entity.get(block.getEntityAt(0)).getData();
          return <img src={src} />;
        },
        editable: false
      };
    }
  },
  blockToHTML: {
    'atomic': {
      start: '<figure>',
      end: '</figure>'
    }
  },
  entityToHTML: (entity, originalText) => {
    if (entity.type === ENTITY_TYPE) {
      return `<img src="${entity.data.src}" />`;
    }
  }
 });

This way your HTML output looking like this:

<figure>
  <img src="..." />
</figure>

If you need to deal with any incoming <img> tags that don’t have wrapping <figures> you can add the block type by changing htmlToBlock to add the atomic block type around any tags without it:

htmlToBlock: (nodeName, node, lastList, inBlock) => {
  if (nodeName === 'figure' && node.firstChild.nodeName === 'IMG' || (nodeName === 'img' && inBlock !== BLOCK_TYPE) {
  return BLOCK_TYPE;
  }
}

Let me know if this works @Zhurbin!

3reactions
racmathafidzcommented, Nov 30, 2021

i know its too late but i’m just trying to help those who have this issue in the future. just have this issue today for showing <img> tag and im using ‘entityToHTML’ and its fix the problem for me.

const currentContentAsHTML = convertToHTML({
      entityToHTML: (entity, originalText) => {
        if (entity.type === 'IMAGE') {          
          return `<img src="${entity.data.src}" />`;
        }
        return originalText;
      },
    })(editorState.getCurrentContent());
Read more comments on GitHub >

github_iconTop Results From Across the Web

Draft JS recognize img from HTML - Stack Overflow
I'm trying to convert HTML to editorState using the native convertFromHtml . But it's not recognizing the img tag.
Read more >
How to use the draft-convert.convertFromHTML function in ...
To help you get started, we've selected a few draft-convert.convertFromHTML examples, based on popular ways it is used in public projects.
Read more >
Problem: Export PDF With Image - Power Platform Community
Hello guys, I have problem with convert html to pdf with image . Τhe problem is that the image cannot be modified in...
Read more >
How to solve Draft.js convertFromHTML converting image ...
js convertFromHTML converting image <img> tags into 'unstyled' contentBlock type instead of 'atomic' content block type? Edit. openJavaScript ...
Read more >
[Solved]-Draft JS recognize img from HTML-Reactjs
the Mikhail Khazov solution is partially correct, I had to add the img tag into the figure tag, that should solve the problem....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found