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.

Blocks encoding in entityToHTML

See original GitHub issue

I was wondering why a ENTITY_MAP is used to convert text in entities, here https://github.com/HubSpot/draft-convert/blob/master/src/encodeBlock.js#L4 ?

Because this is what is happening when using a entityToHTML function to transform links into real <a>:

Editor:

screen shot 2017-02-22 at 15 08 50

Result after convertToHTML:

screen shot 2017-02-22 at 15 08 59

Only because it’s in a entity (a link).

Why?

Do I have to use a decoder on originalText to display the original text?

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:3
  • Comments:8

github_iconTop GitHub Comments

4reactions
benbriggscommented, Mar 1, 2017

Ahh I see. Interesting, thanks for bearing with me @jebarjonet. Are you using the same code that you posted above when converting to HTML? The first thing that came to mind as the culprit was using ReactElements to render out the HTML like this:

entityToHTML: (entity, originalText) => {
        if (entity.type === 'link') {
            return <a href={entity.data.url}>{originalText}</a>; // originalText is double-escaped
        }
        return originalText;
    }

Since React automatically escapes text nodes. However the example you posted above uses a string to convert, which I cannot reproduce the issue with. Additionally returning an empty ReactElement avoids the problem:

entityToHTML: (entity, originalText) => {
        if (entity.type === 'link') {
            return <a href={entity.data.url} />; // originalText is escaped once
        }
        return originalText;
    }

The first example I can fix, but there should be some already available workarounds in the meanwhile.

3reactions
jebarjonetcommented, Mar 3, 2017

You are right. It works well with

return <a href={entity.data.url} />

and

return {
    start: `<a href="${entity.data.url}">`,
    end: '</a>',
}

Thank you!

I just does not work well with

return <a href={entity.data.url}>{originalText}</a>
Read more comments on GitHub >

github_iconTop Results From Across the Web

ContentBlock - Draft.js
ContentBlock objects are largely analogous to block-level HTML elements ... This is how we encode styles and entities for a given block.
Read more >
Advanced Features - * arachnoid.com
When invoked as [FileEncoding:UTF-8], this command will change subsequent file operations to use the UTF-8 (full Unicode support) chartacter set. When invoked ...
Read more >
HTML Entities - W3Schools
... can be used in combination with alphanumeric characters to produce a character that is not present in the character set (encoding) used...
Read more >
How to use the draft-convert.convertToHTML function in ... - Snyk
To help you get started, we've selected a few draft-convert.convertToHTML examples, based on popular ways it is used in public projects.
Read more >
medium-draft @ 0.5.12 .. 0.5.13 - Package Diff
createElement("div",{className:"md-block-code-container"})};default:return null}},s=t.entityToHTML=function(e,t){return e.type===a.Entity.LINK?l.default.
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