2

Currently I'm using React and have content in my rich text in strapi CMS that is spaced out the way I want it in my markdown version, but as soon as I switch to preview, or view the content in my browser, the spaces go away. I have tried adding <br/> tags, but there was still no line breaks.

This is the content in my strapi markdown: ![image|690x273, 50%](upload://jy7Rsdurp6rq6VNB0ki4Jnc9L24.png)

But this is the output on my webpage: ![image|690x128, 50%](upload://b4cGhjj4uOjO6uCCpXcggetnqNO.png)

This is my current code:

import ReactMarkdown from "react-markdown";
import rehypeRaw from "rehype-raw";
export default function name({ posts }) {
  return (
    <>
        <div>
          {posts.Title}
        </div>
      <div>
        <ReactMarkdown children={posts.Content} rehypePlugins={[rehypeRaw]} />
      </div>
    </>
  );
}
Ryan Le
  • 7,708
  • 1
  • 13
  • 23
Jay Modi
  • 569
  • 7
  • 24
  • Does this solve your issue? https://github.com/remarkjs/react-markdown/issues/273 – John Sep 02 '21 at 08:24
  • @John no so I have tried adding   at the end of a line and \n as well but both didn't give me the output – Jay Modi Sep 02 '21 at 08:31

2 Answers2

5

use remark-breaks

and replace \n with &nbsp; \n. if one \n doesn't work, try adding one more

import remarkBreaks from "remark-breaks";

//...

<ReactMarkdown 
  remarkPlugins={[remarkBreaks]}
  rehypePlugins={[rehypeRaw]}
  children={posts.Content.replace(/\n/gi, "&nbsp; \n")}
/>
Carneiro
  • 167
  • 1
  • 5
  • 1
    this works nice with spaces but it breaks some of my other tags. `` having ` ` at the start and finish and the `
      ` list not displaying the dots. Every `
    • ` has a `
      ` in between. How to fix these?
    – Ilir Mar 17 '23 at 18:30
2

For adding multiple line break, this should do it:

import React from "react";
import "./styles.css";

import ReactMarkdown from "react-markdown";

export default function App() {
  const markdown = `hello
  \n &nbsp;
  \n &nbsp;
  \n &nbsp;
  \n &nbsp;
  \n
  world
  `;

  return (
    <div className="App">
      <ReactMarkdown source={markdown} />
    </div>
  );
}

Edit react-markdown basic example (forked)

Ryan Le
  • 7,708
  • 1
  • 13
  • 23