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` merged <p> tags into one <p> tag

See original GitHub issue

I’m having some problems with the convertFromHTML, it doesn’t render the right data as I expected.

For example, html: <p>Paragraph 1</p>, <p>Paragraph 2</p>, <p>Paragraph 3</p>

editorState: EditorState.createWithContent(ContentState.createFromBlockArray(convertFromHTML(_html_)), Decorator)

The html at this stage render correctly as expected.

Paragraph 1 Paragraph 2 Paragraph 3

However, when I save data and refresh the page, it displays as below (all in one line)

Paragraph 1 Paragraph 2 Paragraph 3

Then, I clicked on the editor and suddenly the html turned into <p>Paragraph 1 Paragraph 2 Paragraph 3</p>

Does anyone has similar problem like this? Thanks.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:7
  • Comments:22 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
tomconroycommented, Nov 23, 2016

This worked for me:

import { 
  DefaultDraftBlockRenderMap, 
  ContentState, 
  convertFromHTML,
  getSafeBodyFromHTML
} from 'draft-js';
const blockRenderMap = DefaultDraftBlockRenderMap.set('p', { element: 'p' });
const blocksFromHTML = convertFromHTML(html, getSafeBodyFromHTML, blockRenderMap)
  .map(block => (block.get('type') === 'p' ? block.set('type', 'unstyled') : block));
const contentState = ContentState.createFromBlockArray(blocksFromHTML);

edited to show imports

3reactions
N1ktocommented, Sep 28, 2016

OK. Seems <p> tags wont work in DraftJS. Maybe I was doing smth wrong, but it looks DraftJS wraps any text contents into div -> span and then wraps this into whatever element you provide in your blockRenderMap. Since <p> element supports only Phrasing content children it throws violation error since <div> cannot be a child of <p>. p_div

@jbrozena22 I tried same approach, but it only worked in case all blocks were <p />. If there was at least some known block right after <p />, say <h2 /> it combined all <p />'s into one <div /> with text being concatenated. My use case was copy/pasting content from other editor (Medium).

My ‘workaround’ was to replace all <p> tags with <section>ones (since I have access to input HTML string). And then add section to blockRenderMap

Read more comments on GitHub >

github_iconTop Results From Across the Web

Developers - `convertFromHTML` merged <p> tags into one <p> tag -
`convertFromHTML` merged <p> tags into one <p> tag ... I'm having some problems with the convertFromHTML , it doesn't render the right data...
Read more >
How to solve Draft.js convertFromHTML combining adjacent ...
I solved this by adding a custom DraftBlockRenderMap element for paragraph <p> tags for convertFromHTML because the default one doesn't ...
Read more >
Custom Block Rendering - Draft.js
This article discusses how to customize Draft default block rendering. ... type by matching the Draft block render map with the matched tag....
Read more >
Convert from HTML - Draft.js example - CodePen
1. <!--. 2. Source of the code: https://github.com/facebook/draft-js/tree/master/examples/draft-0-10-0/convertFromHTML. 3. -->. 4. <!DOCTYPE html>.
Read more >
DraftJs : Custom button to add custom HTML + CSS
first, create a map of types by merging the default supported tags with the button tag import { ... DefaultDraftBlockRenderMap, .
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