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

rehype-mdx-elements

v1.0.0

Published

**[rehype][]** plugin to convert MDX JSX elements to regular HTML elements.

Downloads

19,260

Readme

rehype-mdx-elements

rehype plugin to convert MDX JSX elements to regular HTML elements.

Contents

What is this?

This package is a unified (rehype) plugin to convert MDX JSX element nodes to regular HTML elements that can be handled by component mapping.

unified is a project that transforms content with abstract syntax trees (ASTs). rehype adds support for HTML to unified. MDX is markdown that supports JSX. hast is the HTML AST that rehype uses. This is a rehype plugin that transforms MDX JSX elements in the hast.

When should I use this?

This plugin is useful when you want to render arbitrary markdown with custom component support while maintaining security. Unlike full MDX implementations that support JSX expressions and imports, this plugin only processes JSX elements with literal attributes, making it safe for untrusted content.

Use this plugin when you need to:

  • Render arbitrary markdown with custom components
  • Preserve case-sensitive component names and attributes
  • Avoid the security risks of full JSX expression evaluation

Install

This package is ESM only.

npm install rehype-mdx-elements
yarn add rehype-mdx-elements
<script type="module">
  import {rehypeMdxElements} from 'https://esm.sh/rehype-mdx-elements?bundle'
</script>

Use

example.mdx:

# Hello

<Button variant="primary">Click me</Button>

Some text with <Alert type="warning">important info</Alert>.

<script>alert('xss')</script>

example.tsx:

import {readFileSync} from 'node:fs'
import {unified} from 'unified'
import remarkParse from 'remark-parse'
import remarkMdx from 'remark-mdx'
import remarkRehype from 'remark-rehype'
import {rehypeMdxElements} from 'rehype-mdx-elements'
import {toJsxRuntime} from 'hast-util-to-jsx-runtime'
import {Fragment, jsx, jsxs} from 'react/jsx-runtime'
import React from 'react'

// Component definitions
const Button = (props: {variant?: string; children?: React.ReactNode}) => (
  <button className={`btn ${props.variant || 'default'}`}>
    {props.children}
  </button>
)

const Alert = (props: {type?: string; children?: React.ReactNode}) => (
  <div className={`alert alert-${props.type || 'info'}`}>
    {props.children}
  </div>
)

const components = {Button, Alert}

const file = readFileSync('example.mdx')

const processor = unified()
  .use(remarkParse)
  .use(remarkMdx)
  .use(remarkRehype, {passThrough: ['mdxJsxFlowElement', 'mdxJsxTextElement']})
  .use(rehypeMdxElements, {
    allowedElements: Object.keys(components)
  })

const tree = processor.parse(file)
const transformedTree = await processor.run(tree)

// Convert to React elements
const reactElement = toJsxRuntime(transformedTree, {
  Fragment,
  jsx,
  jsxs,
  components
})

console.log(reactElement)

Running example.tsx produces JSX runtime elements with the following content:

<>
  <h1>Hello</h1>
  <Button variant="primary">Click me</Button>
  <p>Some text with <Alert type="warning">important info</Alert>.</p>
</>

API

rehypeMdxElements(options?)

Convert MDX JSX elements to regular HTML elements.

Parameters
  • options (Options, optional) — configuration
Returns

Transform (Transformer).

Options

Fields
  • allowedElements (Array<string>, optional) list of allowed component names, case insensitive. If provided, only these components will be converted to HTML elements. Other JSX elements will be removed.
  • disallowedElements (Array<string>, optional) list of disallowed component names, case insensitive. These components will be removed from the output.

Security

MDX JSX elements can contain arbitrary JavaScript expressions, which is dangerous when executed in an untrusted environment. This plugin helps mitigate security risks by:

  1. Only converting static JSX elements and omiting imports and expressions which could contain arbitrary code
  2. Only converting static JSX attributes with string or void values
  3. Allowing you to specify which component names are allowed or disallowed

Related

License

MIT © Tim Etler