npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

react-markdown-with-mdx

v1.0.1

Published

Higher Order Component to add MDX syntax support to [react-markdown][react-markdown].

Readme

react-markdown-with-mdx

Higher Order Component to add MDX syntax support to react-markdown.

Contents

What is this?

This package provides a Higher Order Component that enhances react-markdown with MDX JSX syntax support.

The HOC processes markdown content to:

  • Parse MDX syntax using remark-mdx
  • Transform MDX elements into React components
  • Preserve all existing react-markdown functionality

When should I use this?

Use this package when you want to:

  • Add MDX component support to existing react-markdown implementations
  • Use MDX component syntax without full MDX compilation
  • Render MDX component content at runtime without executing arbitrary Javascript

Don't use this package if you need full MDX features like imports, exports, or expression execution - use @mdx-js/mdx instead.

How does it work?

For a technical deep dive see here.

Install

This package is ESM only.

npm install react-markdown-with-mdx

You'll also need react and react-markdown as peer dependencies:

npm install react react-markdown

Use

import React from 'react'
import Markdown from 'react-markdown'
import { withMdx } from 'react-markdown-with-mdx'

// Create enhanced component
const MarkdownWithMdx = withMdx(Markdown)

// Define your custom components
const components = {
  Card: ({ children, variant = 'default' }) => (
    <div className={`card card-${variant}`}>{children}</div>
  ),
  Alert: ({ message, type = 'info' }) => (
    <div className={`alert alert-${type}`}>{message}</div>
  )
}

// Use with MDX JSX syntax
const markdown = `
# Hello MDX!

This is regular markdown with **bold** text.

<Card variant="primary">Hello!</Card>

<Alert message="This is an alert" type="warning" />
`

function App() {
  return (
    <MarkdownWithMdx components={components}>
      {markdown}
    </MarkdownWithMdx>
  )
}

API

withMdx(MarkdownComponent)

Higher Order Component that enhances a react-markdown component with MDX support.

Parameters

  • MarkdownComponent (React.ComponentType<Options>) — The react-markdown component to enhance

Returns

Enhanced component (React.ComponentType<WithMdxOptions>) with MDX support.

WithMdxOptions

Extended options interface that includes all react-markdown options plus:

  • remarkMdxPlugins (PluggableList, optional) — Additional remark plugins specifically for MDX processing

All other options from react-markdown are supported, including:

  • components — React components to use for rendering
  • remarkPlugins — Additional remark plugins
  • rehypePlugins — Additional rehype plugins
  • remarkRehypeOptions — Options for remark-rehype

MdxComponents

Component type for creating a components mapping object.

mdxComponent(Component, validator)

mdxComponent<P>(Component: React.FC<P>, validator: ZodType<P>) => React.FC<ComponentProps>

Optional validation helper function that takes a functional component along with a zod validator that ensures the react-markdown props are mapped to the provided component.

interface CustomCardProps extends React.PropsWithChildren {
  id: string
  title: string
  type?: "standard" | "primary"
}

const CustomCard: React.FC<CustomCardProps> = memo(function CustomCard({
  id,
  title,
  type,
  children
}) {
  return <div data-id={id} className={type === "primary" ? "primary-card" : "standard-card"}>
    <h2>{title}</h2>
    {children}
  </div>
}

const components: MdxComponents = {
  CustomCard: mdxComponent(CustomCard,
    z.object({
      id: z.string(),
      title: z.string(),
      type: z.union([z.literal("standard"), z.literal("primary")]).optional(),
      children: z.any()
    })
  )
}

Examples

Basic Usage

import { withMdx } from 'react-markdown-with-mdx'
import Markdown from 'react-markdown'

const MarkdownWithMdx = withMdx(Markdown)

const components = {
  CustomCard: ({ children, primary }) => (
    <div className={primary ? "primary-card" : "standard-card"}>{children}</div>
  )
}

const content = `
# My Document

<CustomCard type="primary">
  Hello There!
</CustomCard>
`

<MarkdownWithMdx components={components}>{content}</MarkdownWithMdx>

Nested Components

const components = {
  Card: ({ title, children }) => (
    <div className="card">
      <h3>{title}</h3>
      <div className="content">{children}</div>
    </div>
  ),
  Link: ({ url, children }) => <a href={url} className="card-link">{children}</a>
}

const content = `
<Card title="Welcome">
  This is a card with **markdown** content.

  <Link url="https://www.example.com">Action</Link>
</Card>
`

With Additional Plugins

const MarkdownWithMdx = withMdx(Markdown)

const customRemarkPlugin = () => (tree) => {
  // Your custom remark plugin logic
}

<MarkdownWithMdx
  components={components}
  remarkPlugins={[customRemarkPlugin]}
  remarkMdxPlugins={[/* MDX-specific plugins */]}
>
  {content}
</MarkdownWithMdx>

Security

This package maintains the security model of react-markdown:

  • Only components explicitly provided in the components prop are allowed
  • JSX components not in the allowlist are completely removed
  • All other security features of react-markdown are preserved
// Only Card is allowed, script will be removed
const components = { Card: MyCard }

const unsafeContent = `
<Card>Safe</Card>
<script>alert('unsafe');</script>
`
// Result: Only the Card renders, script is removed

Compatibility

This package works with:

It uses ES modules and requires a modern JavaScript environment.

Related

License

MIT © Tim Etler