skip to Main Content

I’m using @mdxeditor/editor to edit a markdown. I save in database the changes every seconds. All of this works fine.

Problem:
When I save note, in database, I can see the n when I set a new line in my textarea but when I display it in my markdown, the new lines are not rendered.

For example if there is this in database :

"notes":"**my super note**nnnnew line after 2 entersnn*and again*"

And the console log of my user.notes is exactly the same, my frontend end will display :

**my super note**
new line after 2 enters
and again

Here is my code :

export const MarkDownEditor: React.FC = () => {
  const [user, setUser] = useAtom(userAtom);
  const [currentNote, setCurrentNote] = useState("");

  useEffect(() => {
    const timer = setTimeout(() => {
      if (user && currentNote !== user.notes) {
        UserApi.patch({ notes: currentNote }).then((res) => {
          if (res.status === 200) {
            const newUser = { ...user };
            newUser.notes = currentNote;

            setUser(newUser);
          }
        });
      }
    }, 1000);

    return () => clearTimeout(timer);
  }, [currentNote, user, setUser]);

  const handleMarkdownChange = (newNote: string) => {
    setCurrentNote(newNote);
  };

  if (!user) return null;

  return (
    <ForwardRefEditor markdown={user?.notes} placeholder="Prenez vos notes ici... !" onChange={handleMarkdownChange} />
  );
};
const Editor = dynamic(() => import("./InitializedMDXEditor"), {
  ssr: false,
});

export const ForwardRefEditor = forwardRef<MDXEditorMethods, MDXEditorProps>((props, ref) => (
  <motion.div initial="hidden" animate="visible" variants={makeCardOpacity()}>
    <Card className="h-full w-full ring lg:w-fit lg:min-w-[400px]">
      <Editor {...props} editorRef={ref} />
    </Card>
  </motion.div>
));

ForwardRefEditor.displayName = "ForwardRefEditor";
export default function InitializedMDXEditor({
  editorRef,
  ...props
}: { editorRef: ForwardedRef<MDXEditorMethods> | null } & MDXEditorProps) {
  return (
    <MDXEditor
      {...props}
      ref={editorRef}
      plugins={[
        headingsPlugin(),
        listsPlugin(),
        quotePlugin(),
        imagePlugin(),
        tablePlugin(),
        thematicBreakPlugin(),
        markdownShortcutPlugin(),
        toolbarPlugin({
          toolbarContents: () => (
            <>
              <UndoRedo />
              <BoldItalicUnderlineToggles />
              <CodeToggle />
              <ListsToggle />
              <InsertTable />
              <InsertThematicBreak />
            </>
          ),
        }),
      ]}
    />
  );
}

How can I force the render of the new lines ?

Thank you

2

Answers


  1. Chosen as BEST ANSWER

    I found a solution, which was to create a function to format the text :

    const formatMarkdown = (markdownText: string) => {
        return markdownText.replace(/n/g, "<br />");
    };
    

  2. Okay after researching and testing real quick I found this solution:

    const string = "testestntest";
    return (
        <p style={{whiteSpace: 'pre-line'}}>{string}</p>
    );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search