Adding multiple line break in react-markdown
Asked Answered
C

3

4

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>
    </>
  );
}
Costanzo answered 2/9, 2021 at 8:12 Comment(2)
Does this solve your issue? github.com/remarkjs/react-markdown/issues/273Plyler
@Plyler no so I have tried adding &nbsp; at the end of a line and \n as well but both didn't give me the outputCostanzo
L
4

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)

Lip answered 2/9, 2021 at 8:32 Comment(1)
saved my day. Was struggling to add extra space. Your solution is precise.Macroscopic
B
12

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")}
/>
Barbuto answered 31/8, 2022 at 22:22 Comment(3)
this works nice with spaces but it breaks some of my other tags. <code/> having &nbsp; at the start and finish and the <ul> list not displaying the dots. Every <li> has a <br> in between. How to fix these?Gallia
Thanks. <ReactMarkdown remarkPlugins={[remarkBreaks]}>{recommendation}</ReactMarkdown> worked for me. I just needed to check my SCSS rules and make sure I wasn't doing weird stuff with p margins.Kommunarsk
@Gallia change the replace to .replace(/\n/gi, "\n &nbsp; \n"). Won't break li and ul stuffLeong
L
4

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)

Lip answered 2/9, 2021 at 8:32 Comment(1)
saved my day. Was struggling to add extra space. Your solution is precise.Macroscopic
S
3

Even after adding the remarkBreaks remarkPlugins, ReactMarkdown still does not produce multiple line breaks for \n\n, instead turning them into single line breaks. This is not the best solution, but I had to use this approach to resolve the issue, as there are no options to configure the package or plugins to detect \n\n as multiple line breaks.

I am able to make it work by configuring the component in the following way:


import ReactMarkdown, { ExtraProps } from 'react-markdown'
import remarkBreaks from 'remark-breaks'

const code = ({
  className,
  children,
}: React.ClassAttributes<HTMLElement> & React.HTMLAttributes<HTMLElement> & ExtraProps) => {
  const _children = children && (children as string)
  if (!_children) return null
  return <code className={className}>{_children.replace(/&nbsp;/g, '')}</code>
}

type MarkdownProps = {
  className?: string
  children: string
}

export const Markdown = (props: MarkdownProps) => {
  const { className, children } = props
  return (
    <ReactMarkdown
      remarkPlugins={[remarkBreaks]}
      components={{
        code: code,
      }}
      className={className}>
      {children}
    </ReactMarkdown>
  )
}

Raw payload

"Sure! Here's an example of a simple React functional component that renders a greeting:\n\njsx\nimport React from 'react';\n\nfunction Greeting({ name }) {\n return <div>Hello, {name}!</div>;\n}\n\nexport default Greeting;\n\n\nIn this example, the Greeting component takes a name prop and displays a greeting message. You can use this component by importing it and passing a name prop.\n\nIs there anything specific you would like to know about React components?"

Output

Sure! Here's an example of a simple React functional component that renders a greeting:
 

import React from 'react';

function Greeting({ name }) {
  return <div>Hello, {name}!</div>;
}

export default Greeting;
 
In this example, the Greeting component takes a name prop and displays a greeting message. You can use this component by importing it and passing a name prop.
 
Is there anything specific you would like to know about React components?

CodeSandbox

Straitlaced answered 21/4, 2024 at 19:37 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.