rich-text icon indicating copy to clipboard operation
rich-text copied to clipboard

richTextFromMarkdown() doesn't include line breaks

Open alana314 opened this issue 4 years ago • 3 comments

richTextFromMarkdown doesn't seem to recognize line breaks. Markdown with two spaces and a line break should generate a \n in the text node in contentful rich text, but it just gets stripped. In the meantime, finding and replacing markdown linebreaks with \n before running richTextFromMarkdown is working for me:

let markdown = `test  
test  
test`
markdown = markdown.replace(/\s\s\n/g, '\n') //add line breaks
//returns 'test\ntest\ntest\n'
const content = await richTextFromMarkdown(markdown);
//includes \ns in the rich text nodes

alana314 avatar Feb 13 '20 21:02 alana314

This worked for me:

import { documentToReactComponents } from "@contentful/rich-text-react-renderer";

  const options = {
    renderNode: {
      [BLOCKS.PARAGRAPH]: (node, children) => (
        <>
          <p>{children}</p>
          <br />
        </>
      ),
    },
  };

          {documentToReactComponents(post.fields.content, options)}

sfonua10 avatar Aug 10 '22 18:08 sfonua10

You can add white-space: pre-wrap; to the CSS of the parent element.

maximilliangeorge avatar May 04 '23 10:05 maximilliangeorge

You can add white-space: pre-wrap; to the CSS of the parent element.

This worked, thank you!

istvanjkb avatar Oct 17 '23 10:10 istvanjkb