shuding / nextra

Simple, powerful and flexible site generation framework with everything you love from Next.js.
https://nextra.site
MIT License
11.29k stars 1.24k forks source link

Use Nextra MDX parser in Component #3077

Open LuciferUchiha opened 1 month ago

LuciferUchiha commented 1 month ago

I have the following component where the content is a string. The string contain markdown text. How can I use the nextra parser to render this string as markdown?

Similarly to the Callout component.

The component in question:

const MessageCard = ({ role, content }: Message) => {
  return (
    <div
      className={`flex flex-row ${
        role === "assistant" ? "justify-start" : "justify-end"
      }`}
    >
      <div
        className={`p-4 max-w-[80%] rounded-lg ${
          role === "assistant"
            ? "bg-zinc-800 text-zinc-50"
            : "bg-zinc-50 text-zinc-800"
        }`}
      >
        {content}
      </div>
    </div>
  );
}
dimaMachina commented 1 month ago

rename content with children and

<MessageCard>
  # Hello

  - one
  - two

  > blockquote
</MessageCard>
LuciferUchiha commented 1 month ago

Thx @dimaMachina unfortunatly this didn't work. Maybe for more context. MessageCard is never used in a .mdx file only in .tsx file. The "content" is the response from a ChatGPT call. This is what I tried:

type Message = {
  role: Role;
  content: string;
};

const MessageContent = ({ children }) => {
  return (
    <>
    {children}
    </>
  )
}

const MessageCard = ({ role, content }: Message) => {
  return (
    <div
      className={`flex flex-row ${
        role === "assistant" ? "justify-start" : "justify-end"
      }`}
    >
      <div
        className={`p-4 max-w-[80%] rounded-lg ${
          role === "assistant"
            ? "bg-zinc-800 text-zinc-50"
            : "bg-zinc-50 text-zinc-800"
        }`}
      >
        <MessageContent>{content}</MessageContent>
      </div>
    </div>
  );
}

Rather then using https://github.com/remarkjs/react-markdown I just want to use the already existing markdown compiler/parser.

dimaMachina commented 1 week ago

I think this can be fixed with new playground component https://github.com/shuding/nextra/pull/3174