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

jsxify-html

v1.0.3

Published

A library for converting an HTML string into a JSX string using ASTs.

Downloads

837

Readme

jsxify-html

jsxify-html is a utility that converts HTML strings into JSX syntax, making it easier to integrate HTML content into React components. It offers customization options through the JsxifyHtml class, allowing users to control specific aspects of the conversion process.

Installation

You can install jsxify-html via npm:

npm install jsxify-html

Or via pnpm:

pnpm add jsxify-html

Usage

Basic Usage with convert Function

The simplest way to use jsxify-html is through the convert function:

import { convert } from 'jsxify-html'

const jsx = convert('<div>Hello World</div>')

Advanced Usage with JsxifyHtml Class

For more control over the conversion process, you can create an instance of the JsxifyHtml class with custom options:

import { JsxifyHtml } from 'jsxify-html'

const jsxifier = new JsxifyHtml({
  preservePreTags: true,
  xml: true
})

const jsx = jsxifier.convert('<div>Hello World</div>')

Options

The JsxifyHtml class accepts an Options object to customize its behavior:

interface Options {
  /**
   * Preserves the formatting within <pre> tags when set to true.
   */
  preservePreTags?: boolean
  /**
   * Recommended way of configuring htmlparser2 when wanting to parse XML.
   */
  xml?: boolean
  /**
   * Configures the `html-entities` plugin rules for handling HTML entities.
   * For detailed configuration options, please refer to the documentation: <a href="https://www.npmjs.com/package/html-entities">html-entities</a>
   * @default { mode: 'nonAsciiPrintable', level: 'html5' }
   */
  htmlEntities?: EncodeOptions
}

Option Details

  • preservePreTags: When true, the content within <pre> tags will be preserved with its original formatting.
  • xml: If set to true, the parser is configured to handle XML parsing, which may be necessary depending on the HTML content.
  • htmlEntities: This option allows you to customize the behavior of the html-entities library, which is used by default to handle special characters in the HTML. For more detailed configuration options, please refer to the html-entities documentation.

Example

import { JsxifyHtml } from 'jsxify-html'

const jsxifier = new JsxifyHtml({
  preservePreTags: false,
  xml: true
})

const jsx = jsxifier.convert(`
  <div>
    <h1>Title</h1>
    <pre>
      function helloWorld() {
        console.log("Hello, World!");
      }
    </pre>
  </div>
`)

API

convert(html?: string): undefined | string

A convenient function that wraps the JsxifyHtml class for simple use cases.

  • html: The HTML string you want to convert to JSX.
  • Returns: The converted JSX string or undefined if no HTML string is provided.

JsxifyHtml

The main class providing detailed control over the conversion process.

  • constructor(options?: Options): Initializes the JsxifyHtml instance with optional configuration.
  • convert(html?: string): undefined | string: Converts the provided HTML string to JSX.

License

This project is licensed under the MIT License.