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

react-html-string-parser

v1.4.0

Published

Converting HTML markup to React components on the client and server side.

Downloads

349

Readme

react-html-string-parser

Lightweight library that allows you to convert HTML markup to React components on the client and server side, while providing flexibility in handling tags, attributes, and text segments.

Installation

using npm:

npm install --save react-html-string-parser

or yarn:

yarn add react-html-string-parser

or pnpm:

pnpm add react-html-string-parser

API

HTML2React

Component to convert HTML string into React components

type HTML2ReactProps = {
  html: string;
  components?: Record<
    string,
    ComponentType<Record<string, any>> | keyof JSX.IntrinsicElements
  >;
  attributes?: Record<string, string>;
  converters?: Record<string, (value: string, tag: string) => any>;
  processTextSegment?(segment: string, parentMeta: Meta): ReactNode;
  getComponent?(
    tag: string
  ):
    | ComponentType<Record<string, any>>
    | keyof JSX.IntrinsicElements
    | undefined;
  shouldBeIgnored?(tag: string, props: Record<string, any>): boolean;
  withMeta?: boolean;
};

const HTML2React: FC<HTML2ReactProps>;

| Prop | Description | | --------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- | | html | The HTML string to be converted into React components. | | components? | Custom tag components to replace HTML tags. Keys are not case sensitive. If a component is not provided, the corresponding HTML tag will be used. | | getComponent? | Returns a component for the given tag if it is not found in components. If it returns undefined, the tag will be used as a component. | | attributes? | Map HTML attributes to corresponding React props. If the attribute is not specified, it will be passed as is. | | converters? | Converters for processing attribute values. If no converter is provided, the property will be of type string. | | processTextSegment? | Method to process and transform string parts of HTML content. | | shouldBeIgnored? | Determines if a tag should be ignored based on the tag name and its props. | | withMeta? | If true, adds metadata to each component in the form of a _meta prop, allowing for additional contextual information. |

Example:

import HTML2React from 'react-html-string-parser/HTML2React';

<HTML2React
  html='<div><h1>Title</h1><br><p>Paragraph</p><br /><button data-attribute="any" tabindex="1">Button</button>text</div>'
  components={{
    h1: ({ children }) => <h3>{children}</h3>,
    p: 'div',
    script: () => null,
  }}
  attributes={{ tabindex: 'tabIndex' }}
  converters={{ tabIndex: (value) => +value }}
/>;

both lines below will have the same parse result

// the line below contains extra spaces, an unclosed `<p>` tag,
// the `class` attribute is not quoted, the `disabled` attribute is a boolean
'<div><p>Paragraph<button\n   class=any    disabled\n>Button</button>text</div>';

'<div><p>Paragraph<button class="any" disabled="true">Button</button>text</p></div>';

styleConverter

Converter for style attribute

const styleConverter: (value: string) => CSSProperties;

Example:

import HTML2React from 'react-html-string-parser/HTML2React';
import styleConverter from 'react-html-string-parser/styleConverter';

<HTML2React
  html='<p style="margin-top: 25px; margin-bottom:10px;">text</p>'
  converters={{ style: styleConverter }}
/>;

License

MIT © Krombik