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(/ /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