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 🙏

© 2024 – Pkg Stats / Ryan Hefner

rehype-react

v8.0.0

Published

rehype plugin to transform to React

Downloads

536,031

Readme

rehype-react

Build Coverage Downloads Size Sponsors Backers Chat

rehype plugin to turn HTML into preact, react, solid, svelte, vue, etc.

Contents

What is this?

This package is a unified (rehype) plugin that compiles HTML (hast) to any JSX runtime (preact, react, solid, svelte, vue, etc).

unified is a project that transforms content with abstract syntax trees (ASTs). rehype adds support for HTML to unified. hast is the HTML AST that rehype uses. This is a rehype plugin that adds a compiler to compile hast to a JSX runtime.

When should I use this?

This plugin adds a compiler for rehype, which means that it turns the final HTML (hast) syntax tree into something else (in this case, a JSX.Element). It’s useful when you’re already using unified (whether remark or rehype) or are open to learning about ASTs (they’re powerful!) and want to render content in your app.

If you’re not familiar with unified, then react-markdown might be a better fit. You can also use react-remark instead, which is somewhere between rehype-react and react-markdown, as it does more that the former and is more modern (such as supporting hooks) than the latter, and also a good alternative. If you want to use JavaScript and JSX inside markdown files, use MDX.

Install

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

npm install rehype-react

In Deno with esm.sh:

import rehypeReact from 'https://esm.sh/rehype-react@8'

In browsers with esm.sh:

<script type="module">
  import rehypeReact from 'https://esm.sh/rehype-react@8?bundle'
</script>

Use

Say our React app example.js looks as follows:

import {Fragment, createElement, useEffect, useState} from 'react'
import * as prod from 'react/jsx-runtime'
import rehypeParse from 'rehype-parse'
import rehypeReact from 'rehype-react'
import {unified} from 'unified'

// @ts-expect-error: the react types are missing.
const production = {Fragment: prod.Fragment, jsx: prod.jsx, jsxs: prod.jsxs}

const text = `<h2>Hello, world!</h2>
<p>Welcome to my page 👀</p>`

/**
 * @param {string} text
 * @returns {JSX.Element}
 */
function useProcessor(text) {
  const [Content, setContent] = useState(createElement(Fragment))

  useEffect(
    function () {
      ;(async function () {
        const file = await unified()
          .use(rehypeParse, {fragment: true})
          .use(rehypeReact, production)
          .process(text)

        setContent(file.result)
      })()
    },
    [text]
  )

  return Content
}

export default function App() {
  return useProcessor(text)
}

…running that in Next.js or similar, we’d get:

<h2>Hello, world!</h2>
<p>Welcome to my page 👀</p>

API

This package exports no identifiers. The default export is rehypeReact.

unified().use(rehypeReact, options)

Turn HTML into preact, react, solid, svelte, vue, etc.

Parameters
  • options (Options, required) — configuration
Returns

Nothing (undefined).

Result

This plugin registers a compiler that returns a JSX.Element where compilers typically return string. When using .stringify on unified, the result is such a JSX.Element. When using .process (or .processSync), the result is available at file.result.

Frameworks

There are differences between what JSX frameworks accept, such as whether they accept class or className, or background-color or backgroundColor.

For hast elements transformed by this project, this is be handled through options:

| Framework | elementAttributeNameCase | stylePropertyNameCase | | --------- | -------------------------- | ----------------------- | | Preact | 'html' | 'dom' | | React | 'react' | 'dom' | | Solid | 'html' | 'css' | | Vue | 'html' | 'dom' |

Components

Possible components to use (TypeScript type).

See Components from hast-util-to-jsx-runtime for more info.

Options

Configuration (TypeScript type).

Fields
  • Fragment (Fragment from hast-util-to-jsx-runtime, required) — fragment
  • jsx (Jsx from hast-util-to-jsx-runtime, required in production) — dynamic JSX
  • jsxs (Jsx from hast-util-to-jsx-runtime, required in production) — static JSX
  • jsxDEV (JsxDev from hast-util-to-jsx-runtime, required in development) — development JSX
  • components (Partial<Components>, optional) — components to use
  • development (boolean, default: false) — whether to use jsxDEV when on or jsx and jsxs when off
  • elementAttributeNameCase ('html' or 'react', default: 'react') — specify casing to use for attribute names
  • passNode (boolean, default: false) — pass the hast element node to components
  • space ('html' or 'svg', default: 'html') — whether tree is in the 'html' or 'svg' space, when an <svg> element is found in the HTML space, this package already automatically switches to and from the SVG space when entering and exiting it
  • stylePropertyNameCase ('css' or 'dom', default: 'dom') — specify casing to use for property names in style objects
  • tableCellAlignToStyle (boolean, default: true) — turn obsolete align props on td and th into CSS style props

Types

This package is fully typed with TypeScript. It exports the additional types Components and Options. More advanced types are exposed from hast-util-to-jsx-runtime.

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, rehype-react@^8, compatible with Node.js 17.

This plugin works with rehype-parse version 3+, rehype version 4+, and unified version 9+, and React 18+.

Security

Use of rehype-react can open you up to a cross-site scripting (XSS) attack if the tree is unsafe. Use rehype-sanitize to make the tree safe.

Related

Contribute

See contributing.md in rehypejs/.github for ways to get started. See support.md 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 © Titus Wormer, modified by Tom MacWright, Mapbox, and rhysd.