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

convert-html-to-jsx

v0.1.2

Published

covert HTML to JSX, supports custom conversion such as Antd or React Native

Downloads

309

Readme

Convert-HTML-To-JSX

English | 简体中文

Introduction

Convert HTML in plain text to JSX that React can understand.

It supports custom conversions such as other third party components or React Native components.

Due to the use of RegExpMatchArray.groups, the target version of JavaScript is es2018.

Installation

yarn add convert-html-to-jsx
# or
npm i convert-html-to-jsx

Usage

Pass in innerHTML and you're done!

import HTML2JSX from "convert-html-to-jsx"

const innerHTML = `
    <p style="font-weight: bold;">Hello, world!</p>
    <img src="http://xxx.xxx.xxx" />
    <button disabled>Click</button>
`

export default () => {
    return (
        <div>
            <HTML2JSX innerHTML={innerHTML} />
        </div>
    )
}

Note that the <script> tag and its internal text will be ignored, so if you don't want to do that, pass in enableScript={true}

Events starting with on will be ignored, if you want to handle them, please use the Conversion

Conversion

If you are not satisfied with the default conversion, or if you are using React Native, you can use the convert function to implement custom component.

interface HTMLProps {
    [PropName: string]: string | boolean | Style
}

interface EventProps {
    [PropName: string]: string
}

interface ConvertProps {
    tagName: string,
    HTMLProps: HTMLProps,
    eventProps: EventProps,
    originalElement: JSX.Element,
    children?: ReactNode,
}

type Convert = (props: ConvertProps) => JSX.Element

convert will take the following parameters:

  1. tagName

    Tag names, such as p or div. tagName of Plain text is the empty string ""

  2. HTMLProps

    HTML attribute object of the tag, such as src: "https://xxx" or type: "text". For those properties that do not specify a value, true will be passed. For example, the attribute in <input disabled /> will be converted to { disabled: true }

  3. eventProps

    tag event objects, such as { onClick: "clickMe" }, and handle them yourself if needed

  4. originalElement

    The component that will be converted to if no convert is passed in

  5. children

    children of this tag

Examples

  1. Disable all button tags

    const convert = (props: ConvertProps) => {
        const { tagName, HTMLProps, eventProps, originalElement, children } = props
        if (tagName === "button") {
    
            const newHTMLProps = {...HTMLProps, disabled: true}
    
            // You can use createElement
            return React.createElement("button", newHTMLProps, children )
    
            // or jsx
            return (
                <button {...newHTMLProps} >
                    {children}
                </button>
            )
        }
    
        // For those tags that don't meet the conditions, just return the processed component directly
        return originalElement
    }
    
    <HTML2JSX innerHTML={innerHTML} convert={convert} >
  2. Bold the tag with id as abc

    const convert = (props: ConvertProps) => {
        const { tagName, HTMLProps, eventProps, originalElement, children } = props
        if (HTMLProps.id === "abc") {
            if (HTMLProps.style) {
                HTMLProps.style.fontWeight = "bold"
            } else {
                HTMLProps.style = { fontWeight: "bold" }
            }
            return React.createElement(tagName, HTMLProps, children )
        }
    
        // For those tags that don't meet the conditions, just return the processed component directly
        return originalElement
    }
    
    <HTML2JSX innerHTML={innerHTML} convert={convert} >
  3. Replace tags with other components, which is useful when using other component libraries

    import { Image } from "antd"
    
    const convert = (props: ConvertProps) => {
    
        const { tagName, HTMLProps, eventProps, originalElement, children } = props
    
        if (tagName === "img") {
    
            // You can process HTMLProps here as props of the target component.
            // action only represents a procedure here
            const newHTMLProps = action(HTMLProps)
    
            return React.createElement(Image, newHTMLProps, children )
        }
    
        // For those tags that don't meet the conditions, just return the processed component directly
        return originalElement
    }
  4. use in React Native

    HTML2JSX does not provide any conversions other than in a browser environment, but it can provide some ideas. For example, in React Native, plain text must be placed in the Text component, you could write it like this.

    import { Text } from "react-native"
    
    const convert = (props: ConvertProps) => {
    
        const { tagName, HTMLProps, eventProps, originalElement, children } = props
    
        if (tagName === "") {
    
            const newHTMLProps = action(HTMLProps)
    
            return React.createElement(Text, newHTMLProps, children )
        }
    
        if (tagName === "div") {
    
            // ...
        }
    }