@mdx-js/react vs @mdx-js/mdx vs @mdx-js/loader
MDX (Markdown for JSX) Libraries Comparison
3 Years
@mdx-js/react@mdx-js/mdx@mdx-js/loader
What's MDX (Markdown for JSX) Libraries?

MDX (Markdown for JSX) is a powerful format that allows you to write JSX (React components) directly within Markdown files. This enables a seamless blend of static content and dynamic components, making it ideal for documentation, blogs, and interactive content. The MDX ecosystem provides tools to parse, compile, and render these mixed-content files, offering developers flexibility and creativity in how they present information. The three main packages in the MDX ecosystem are @mdx-js/loader, @mdx-js/mdx, and @mdx-js/react, each serving a unique purpose in the workflow of handling MDX files.

Package Weekly Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
@mdx-js/react7,782,108
18,74914.4 kB1610 months agoMIT
@mdx-js/mdx2,914,580
18,749164 kB1610 months agoMIT
@mdx-js/loader690,529
18,74915.1 kB1610 months agoMIT
Feature Comparison: @mdx-js/react vs @mdx-js/mdx vs @mdx-js/loader

Purpose

  • @mdx-js/react:

    The @mdx-js/react package focuses on rendering MDX content within React applications. It provides the necessary components and context to handle both Markdown and JSX elements, enabling seamless integration of interactive content within React components.

  • @mdx-js/mdx:

    The @mdx-js/mdx package serves as a command-line tool for compiling MDX files. It provides utilities for converting MDX content into various formats, making it suitable for static site generation and other use cases where MDX needs to be processed outside of a React environment.

  • @mdx-js/loader:

    The @mdx-js/loader package is designed to integrate MDX processing into Webpack workflows. It transforms MDX files into React components during the build process, allowing developers to import and use MDX content directly in their applications.

Integration

  • @mdx-js/react:

    @mdx-js/react is designed for integration within React applications. It works alongside other React components and libraries, making it easy to incorporate MDX content into existing React projects.

  • @mdx-js/mdx:

    @mdx-js/mdx operates as a standalone tool, independent of any specific build system. This makes it versatile for use in various environments, including static site generators and command-line workflows.

  • @mdx-js/loader:

    @mdx-js/loader integrates with Webpack, allowing for seamless processing of MDX files as part of the build pipeline. This integration is crucial for projects that rely on Webpack for asset management and bundling.

Rendering

  • @mdx-js/react:

    @mdx-js/react provides the rendering capabilities for MDX content within React applications. It handles both Markdown and JSX elements, allowing for dynamic and interactive rendering of MDX files.

  • @mdx-js/mdx:

    @mdx-js/mdx does not handle rendering directly. Instead, it compiles MDX content into a format that can be rendered by other tools or frameworks, such as React, Vue, or static HTML.

  • @mdx-js/loader:

    @mdx-js/loader handles the rendering of MDX content indirectly by transforming it into React components. The actual rendering is performed by the React application using the components generated by the loader.

Example Code

  • @mdx-js/react:

    Example of rendering MDX content with @mdx-js/react:

    import { MDXProvider } from '@mdx-js/react';
    import MyMdxContent from './content.mdx';
    
    const App = () => (
      <MDXProvider>
        <MyMdxContent />
      </MDXProvider>
    );
    
  • @mdx-js/mdx:

    Example of using @mdx-js/mdx from the command line:

    npx @mdx-js/mdx compile input.mdx -o output.html
    
  • @mdx-js/loader:

    Example of using @mdx-js/loader in a Webpack configuration:

    module.exports = {
      module: {
        rules: [
          {
            test: /\.mdx$/,
            use: '@mdx-js/loader',
          },
        ],
      },
    };
    
How to Choose: @mdx-js/react vs @mdx-js/mdx vs @mdx-js/loader
  • @mdx-js/react:

    Opt for @mdx-js/react if you are building a React application and want to render MDX content seamlessly. This package provides the necessary components and context to integrate MDX files into your React app, allowing for dynamic rendering of both Markdown and JSX.

  • @mdx-js/mdx:

    Select @mdx-js/mdx if you need a command-line tool for compiling MDX files into HTML or other formats. This package is useful for projects that require a standalone solution for processing MDX content without integrating it directly into a React application.

  • @mdx-js/loader:

    Choose @mdx-js/loader if you are working on a project that requires Webpack integration for MDX files. This package is essential for transforming MDX content into React components during the build process, making it ideal for static site generators and applications that leverage Webpack.

README for @mdx-js/react

@mdx-js/react

Build Coverage Downloads Size Sponsors Backers Chat

React context for MDX.

Contents

What is this?

This package is a context based components provider for combining React with MDX.

When should I use this?

This package is not needed for MDX to work with React. See ¶ MDX provider in § Using MDX for when and how to use an MDX provider.

If you use Next.js, do not use this. Add an mdx-components.tsx (in src/ or /) file instead. See Configuring MDX on nextjs.org for more info.

Install

This package is ESM only. In Node.js (version 16+), install with npm:

npm install @mdx-js/react

In Deno with esm.sh:

import {MDXProvider} from 'https://esm.sh/@mdx-js/react@3'

In browsers with esm.sh:

<script type="module">
  import {MDXProvider} from 'https://esm.sh/@mdx-js/react@3?bundle'
</script>

Use

/**
 * @import {MDXComponents} from 'mdx/types.js'
 */

import {MDXProvider} from '@mdx-js/react'
import Post from './post.mdx'
// ^-- Assumes an integration is used to compile MDX to JS, such as
// `@mdx-js/esbuild`, `@mdx-js/loader`, `@mdx-js/node-loader`, or
// `@mdx-js/rollup`, and that it is configured with
// `options.providerImportSource: '@mdx-js/react'`.

/** @type {MDXComponents} */
const components = {
  em(properties) {
    return <i {...properties} />
  }
}

console.log(
  <MDXProvider components={components}>
    <Post />
  </MDXProvider>
)

👉 Note: you don’t have to use MDXProvider and can pass components directly:

-<MDXProvider components={components}>
-  <Post />
-</MDXProvider>
+<Post components={components} />

See ¶ React in § Getting started for how to get started with MDX and React. See ¶ MDX provider in § Using MDX for how to use an MDX provider.

API

This package exports the identifiers MDXProvider and useMDXComponents. There is no default export.

MDXProvider(properties?)

Provider for MDX context.

Parameters
  • properties (Props) — configuration
Returns

Element (JSX.Element).

useMDXComponents(components?)

Get current components from the MDX Context.

Parameters
Returns

Current components (MDXComponents from mdx/types.js).

MergeComponents

Custom merge function (TypeScript type).

Parameters
Returns

Additional components (MDXComponents from mdx/types.js).

Props

Configuration for MDXProvider (TypeScript type).

Fields

Types

This package is fully typed with TypeScript. It exports the additional types MergeComponents and Props.

For types to work, make sure the TypeScript JSX namespace is typed. This is done by installing and using the types of your framework, as in @types/react.

Compatibility

Projects maintained by the unified collective are compatible with maintained versions of Node.js.

When we cut a new major release, we drop support for unmaintained versions of Node. This means we try to keep the current release line, @mdx-js/react@^3, compatible with Node.js 16.

Security

See § Security on our website for information.

Contribute

See § Contribute on our website for ways to get started. See § Support for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MIT © Compositor and Vercel