rich-text: Missing converted line breaks in rich-text-html-renderer
When I’m using the rich-text-html-renderer I expect that also the line breaks are converted to HTML
also. Currently when I’ve some manual line breaks in a paragraph they are ignored completely.
Line1
Line2
should be converted to
<p>
Line1<br />
Line2
</p>
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 2
- Comments: 23
@quinnmyers The above comments and examples are mainly referring to
using rich-text-html-renderer, notrich-text-react-renderer. That’s why these examples usenextrather thanchildren.The documentation for
rich-text-react-rendereractually gives an example for how to use therenderTextoption to replace\nline breaks with<br />.Try using this
optionsdefinition:You can see a working example on codesandbox.io.
Thank you, @sbezludny for your code snipped above. It helped us allot See
I enhenced it a little bit, because your example stripped away the
<p />and only converted the first\n'into<br />Maybe this is helpful for others as well
Hey there,
for me works an ordinary
white-space: pre-line;on the<p>which renders line breaks perfectly without adding<br>s to the DOM.Like:
Hey @xyNNN, line breaks stored as
new lineseparators within a text node.However, unlike
react-renderer,html-rendererdoes not exposerenderTextmethod to control the presentation of the text nodes.The same behaviour could be achieved by hooking into renderNode renderer e.g.:
It is absolutely ridiculous why contentful makes it so hard to write content on the platform. Very frustrated
@arhoy Do you have any constructive feedback? We have thousands of users producing content each day, so not sure what exactly you find ridiculous.
My workaround for this was treating empty paragraph blocks as
<br />. Leaving incase it helps anyone!I had the problem that flatMap is not supported in my deployment. This is my hacky alternative that worked for me:
Edit: using React.Fragment from the comment below
I’m using @DanweDE 's above solution in
@contentful/rich-text-react-rendererat13.4.0.In Safari and Firefox, my content looks fine.
In Chrome, my line breaks are appearing as a strange missing character:
Turns out the culprit is
U+2028(Line separator).See Chromium bug: https://bugs.chromium.org/p/chromium/issues/detail?id=550275
My hacky solution is:
My constructive feedback 😉 would be to make it easy and simple to configure either the RTE and/or Content Model and/or
rich-text-react-rendererto display line breaks as break elements. Either through some UI checkbox or some options key likerenderNewlinesAsBreaks: true. Or all of the above.Hope this helps someone.
Having line breaks should be the default or at least an option so we dont need to call
string.replace()for each text on each render call…I think I had the same problem with the linter. Mine also warned about the index in the key: react/no-array-index-key
This way the key should be pretty unique across the DOM