Version 1.0.1
Overview
Updated 5 min ago
@m2d/react-markdown: A modern, SSR-compatible React Markdown renderer that preserves syntax trees for instant Word DOCX export.
✨ Features
- ⚛️ React Server Components (RSC) Ready: Zero-hook server imports (
@m2d/react-markdown) safe for Next.js App Router and server rendering. - 🔁 Client-Side Reactivity & Memoization: Dedicated
@m2d/react-markdown/cliententry for interactive editors with AST caching. - 🌳 Direct AST Capture (
astRef): Retains parsed MDAST/HAST representations so you can trigger instant Word (.docx) exports without re-parsing. - 🧱 Arbitrary JSX Support (
<Mdx>): Renders embedded Markdown strings nested within arbitrary React JSX structures. - 🎨 Component Overrides & Helpers: Seamless tag replacements and built-in utilities like
UnwrapandOmit.
📦 Installation
$ pnpm add @m2d/react-markdownor
$ npm install @m2d/react-markdownor
$ yarn add @m2d/react-markdown🚀 Quick Start
Basic Server Component Rendering
import { Md } from "@m2d/react-markdown";
export default function Article() {
return (
<article className="prose">
<Md>{"# Hello World\n\nRendered on the server with zero client hooks!"}</Md>
</article>
);
}Interactive Client Export to DOCX
"use client";
import { useRef } from "react";
import { Md } from "@m2d/react-markdown/client";
import { toDocx } from "mdast2docx";
export function DocumentationViewer({ markdown }: { markdown: string }) {
const astRef = useRef<{ mdast: any; hast: any }[]>([]);
const handleExport = async () => {
if (!astRef.current[0]?.mdast) return;
const blob = await toDocx(astRef.current[0].mdast);
const url = URL.createObjectURL(blob as Blob);
const a = document.createElement("a");
a.href = url;
a.download = "document.docx";
a.click();
URL.revokeObjectURL(url);
};
return (
<div>
<button onClick={handleExport}>Export to Word</button>
<Md astRef={astRef}>{markdown}</Md>
</div>
);
}📚 Deep Dive Guides
- ⚛️ Server & Client Rendering Guide: React Server Components, client memoization, JSX parsing with
<Mdx>, and component overrides. - 💾 AST Capture & DOCX Export Guide: Preserving ASTs with
astRefand coupling withmdast2docx.
License
This library is licensed under the MIT open-source license.
with 💖 by Mayank Kumar Chaudhari