JSPM

mdx-renderer

0.1.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q38476F
  • License MPL-2.0

A modern, SSR-friendly React Markdown renderer that preserves the MDAST tree for reuse (e.g., mdast2docx), supports full JSX children, unified plugins, and component overrides.

Package Exports

  • mdx-renderer
  • mdx-renderer/dist/server
  • mdx-renderer/dist/server/omit
  • mdx-renderer/dist/server/unwrap
  • mdx-renderer/dist/test-utils
  • mdx-renderer/omit
  • mdx-renderer/server
  • mdx-renderer/test-utils
  • mdx-renderer/unwrap

Readme

MDX Renderer [@m2d/react-markdown]

test Maintainability codecov Version Downloads npm bundle size Gitpod ready-to-code

โœจ A modern, JSX-compatible, SSR-ready Markdown renderer for React โ€” with full access to MDAST & HAST trees for tools like mdast2docx.


๐Ÿ”ฅ Why mdx-render?

mdx-render goes beyond traditional React Markdown libraries by focusing on:

  • โœ… Server-side rendering (SSR) without hooks
  • โœ… Full JSX children support (not just strings)
  • โœ… Access to raw MDAST & HAST trees
  • โœ… Drop-in plugin support via Unified (remark, rehype, etc.)
  • โœ… Custom component overrides per tag
  • โœ… Integration with tools like mdast2docx

๐Ÿš€ Installation

pnpm add @m2d/react-markdown

or

npm install @m2d/react-markdown

or

yarn add @m2d/react-markdown

โšก Quick Example

import { Md } from "mdx-render";
import { toDocx } from "mdast2docx";
import { useRef } from "react";

const astRef = useRef([]);

export default function Page() {
  return (
    <>
      <Md astRef={astRef}>{`# Hello\n\nThis is **Markdown**.`}</Md>
      <button
        onClick={() => {
          const doc = toDocx(astRef.current[0].mdast);
          // Export DOCX, or save
        }}>
        Export to DOCX
      </button>
    </>
  );
}

๐Ÿง  JSX-Aware Parsing

Unlike other libraries, this renderer supports JSX as children, which means you can nest Markdown inside arbitrary components:

<Md>
  <section>{`# Title\n\nContent.`}</section>
</Md>

Note: astRef.current is an array โ€” one entry per Markdown segment. Each entry contains { mdast, hast } for fine-grained control.


โœจ Component Overrides

Override default HTML rendering with your own components:

<Md
  components={{
        code: (props) => <CodeWithHighlights {...props} />
    em: Unwrap, // Renders <em> content without tags
    blockquote: Omit, // Removes <blockquote> completely
  }}>
  {`*This will be unwrapped*\n\n> This will be removed!`}
</Md>

Use the built-in helpers:

  • Unwrap โ€“ renders children, ignores tag & props.
  • Omit โ€“ removes the element and its content entirely.

๐Ÿงฉ Plugin Support

Use any remark or rehype plugins with full flexibility:

<Md remarkPlugins={[remarkGfm]} rehypePlugins={[rehypeSlug, rehypeAutolinkHeadings]}>
  {markdown}
</Md>

๐Ÿ“ฆ astRef: MDAST + HAST Access

type astRef = {
  current: { mdast: Root; hast: HastRoot }[];
};

Each markdown block is processed independently to allow full JSX flexibility. You can access all parsed trees via astRef.current, ideal for:

  • DOCX/PDF generation (mdast2docx)
  • Markdown linting or analytics
  • AST-aware transformations

๐Ÿงญ Roadmap

  • ๐Ÿ”„ Merge surrounding JSX + <Md> blocks into unified MDAST/HAST
  • ๐Ÿงช Add test utilities for structural validation
  • ๐Ÿ“š Provide Next.js examples with DOCX export

  • mdast2docx โ€“ Convert MDAST to Word (.docx)
  • unifiedjs โ€“ Syntax tree processing toolkit
  • react-markdown โ€“ A simpler but less flexible Markdown renderer

License

This library is licensed under the MPL-2.0 open-source license.

Please enroll in our courses or sponsor our work.


with ๐Ÿ’– by Mayank Kumar Chaudhari