Client and server markdown rendering components with MDX support
If you need to render Markdown in our React application, you can easily do so with the two components provided.
This component allows you to display Markdown client-side, meaning it is not rendered server-side. It uses the markdown-to-jsx library to parse and render Markdown.
import { ClientMarkdown } from "@/features/markdown/client-markdown";
const markdown = `
# Hello World
This is a paragraph
- Item 1
- Item 2
- Item 3
`;
export default function App() {
return <ClientMarkdown>{markdown}</ClientMarkdown>;
}
children: string - The Markdown content to renderclassName?: string - Custom CSS classes (replaces the default typography class)markdown-to-jsx props are supportedBy default, this component applies the typography class. You can replace it by passing a className prop to the component.
This component allows you to display Markdown server-side, meaning the HTML is generated server-side and the client only receives the final render.
Why Mdx? Because this component supports mdx syntax, which is a markdown extension allowing you to add React components in Markdown.
import { ServerMdx } from "@/features/markdown/server-mdx";
const markdown = `
# Hello World
This is a paragraph
<Alert type="info">
This is an information message
</Alert>
`;
export default async function Page() {
return <ServerMdx source={markdown} />;
}
source: string - The MDX content to renderclassName?: string - Custom CSS classes (applied to the wrapper div, added to the default typography class)By default, no React components are supported. To add them, you need to modify the src/features/markdown/server-mdx.tsx file:
import { Alert } from "@/components/nowts/alert";
const MdxComponent = {
Alert: Alert,
// Add other components here
} satisfies Record<string, React.ComponentType>;
Complete example with an Alert component:
# My Document
This is a normal paragraph.
<Alert type="info">This is an information message</Alert>
Use ServerMdx when:
Use ClientMarkdown when:
No, ClientMarkdown only supports basic Markdown. To integrate React components, you must use ServerMdx.