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-componentify

v1.0.3

Published

## Table of Contents

Downloads

29

Readme

Componentify

Table of Contents

NPM package

www.npmjs.com/package/react-componentify

Installation

$ npm install react-componentify

Usage

With this module you can match parts of plain text and convert them to React components.

Example use cases:

  1. Bold/italics/underline etc. text formatting
  2. Clickable links
  3. Replacing \n with
  4. Anything else you can think of - just create your own Converter!

Converters

Componentify comes with some Converters built-in, which you can use out of the box. Additionally, you may create a Converter yourself, suiting your particular use-case.

First, let's look at the built-in Converters:

  • bold text:
const boldConverter = {
  regex: /\\*([\\w\\d\\s\\:\\/\\.\\[\\]]+)\\*/g,
  component: "span",
  props: {
    style: { fontWeight: "900" }
  },
  innerText: matches => matches[1]
};
  • italic text:
const italicConverter = {
  regex: /\\_([\\w\\d\\s\\:\\/\\.\\[\\]]+)\\_/g,
  component: "span",
  props: {
    style: { fontStyle: "italic" }
  },
  innerText: matches => matches[1]
};
  • link with href as a inner text or custom inner text
const linkConverter = {
  regex: /((?:https?:\/\/)?(?:[\da-z\.-]+)\.(?:[a-z\.]{2,6})(?:[-a-zA-Z0-9@:%_\+.~#?&//=]*)*\/?)(?:\[(.+?)\])?/g,
  component: "a",
  props: ([_, url]) => {
    return { href: url, targer: "_blank" };
  },
  innerText: matches => matches[2] || matches[1]
};
  • <br/> tag
const brTagConverter = {
  regex: /<br\/>/,
  component: "br"
};

Componentify renders tree of components:

Prop Types

| Property | Type | Required? | Description | | ------------ | ------ | :-------: | ------------------------------------------------------------- | | text | string | ✓ | text you want to format | | converters | array | | Converters define how to match and componentify parts of text |

ComponentifyProps = {
  // required
  text: string,
  // optional
  converters: [
    {
      // required
      regex: RegExp,
      // required
      component: string | Class<React.Component<Props, any>>,
      // optional
      props: object | RegExp$matchResult => void,
      // optional
      innerText: Array<matchingGroup1, matchingGroup2> | RegExp$matchResult => matchingGroup1 | matchingGroup2
    }
  ]
}

Expamples without component nesting

  • Style parts of text in bold
import Componentify, { boldConverter } from "react-componentify";

<Componentify
  text={"Тhis is bold *text* and *more* text"}
  converters={[boldConverter]}
/>;
  • Style parts of text in italic
import Componentify, { italicConverter } from "react-componentify";

<Componentify text={"Тhis is italic _text_"} converters={[italicConverter]} />;
  • Have clickable links
import Componentify, { linkConverter } from "react-componentify";

<Componentify
  text={"Тhis is https://google.com link"}
  converters={[linkConverter]}
/>;
  • Have clickable links with custom inner text
import Componentify, { linkConverter } from "react-componentify";

<Componentify
  text={"Тhis is https://google.com[Google] link"}
  converters={[linkConverter]}
/>;
  • Replace \n with
import Componentify, { brTagConverter } from "react-componentify";

<Componentify
  text={"Тhis is line one<br/>This is line two"}
  converters={[brTagConverter]}
/>;
  • Replace words
import Componentify from "react-componentify";

<Componentify
  text="Hi, my name is John"
  converters={[
    {
      regex: /John/,
      component: "span",
      innerText: "Snow"
    }
  ]}
/>;

Expamples with component nesting

  • Style parts of bold text in italic
import Componentify, {
  boldConverter,
  italicConverter
} from "react-componentify";

<Componentify
  text="Тhis is my *bold and _italic_* text"
  converters={[boldConverter, italicConverter]}
/>;
  • Style liks in bold
import Componentify, { boldConverter, linkConverter } from "react-componentify";

<Componentify
  text="Тhis is my bold *https://google.com* link"
  converters={[boldConverter, linkConverter]}
/>;

You can make your custom converters