mdast2docx logo Md2Docx
Version 1.0.1

Overview

Updated 5 min ago

CIcodecovnpm versionnpm downloadsnpm bundle sizelicense

@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/client entry 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 Unwrap and Omit.

📦 Installation

$ pnpm add @m2d/react-markdown

or

$ npm install @m2d/react-markdown

or

$ 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

License

This library is licensed under the MIT open-source license.


with 💖 by Mayank Kumar Chaudhari

On this page