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

@imertz/react-link-parser

v1.1.7

Published

Effortlessly parse text into links, tags, mentions, emails, etc.

Downloads

15

Readme

React Link Parser

npm npm npm

A tiny React component (~1KB) that parses plain text into links, emails or renders elements that start or end with a specific character or word in the way you want.

How it works?

Iterates over every word in the given string and matches watchers to render it customized with matched watcher render function.

Install

Install the react-link-parser package with npm or yarn:

npm install react-link-parser
yarn add react-link-parser

Get started

Let's start using the component with a couple of practical examples.

A simple example

As a common use case, let's say you want to identify links in your text and render them with your custom render component.
This is the default behavior of the component, so all you need to do is import the package and start using it in your React application.

import LinkParser from "react-link-parser";

export function App() {
  return (
    <LinkParser>
      I know you will forgive me for it. Were not my other associations so chosen by Fate as to make a heart like mine
      uneasy? Read more here: https://lorem.ipsum/book
    </LinkParser>
  );
}

Parse tags, mentions and more

In cases where you want to customize how certain words or characters are rendered, you can define a list of watchers that specify which words or characters to target and which function to use for rendering. With this functionality, you can easily create a more personalized parser.

import LinkParser from "react-link-parser";

export function App() {
  // a list of watchers
  const watchers = [
    {
      type: "startsWith",
      watchFor: "#",
      render: (tag) => <a href={`/posts?filterByTag=${tag}`}>{tag}</a>,
    },
    {
      type: "startsWith",
      watchFor: "@",
      render: (mention) => <i>{mention}</i>,
    },
    {
      type: "endsWith",
      watchFor: "*",
      render: (text) => <span style={{ color: "red" }}>{text}</span>,
    },
    {
      watchFor: "link",
      render: (url) => (
        <a href={url} target="_blank" rel="noreferrer noopener nofollow">
          {url}
        </a>
      ),
    },
    {
      watchFor: "email",
      render: (url: string) => (
        <a href={`mailto:${url}`} target="_blank" rel="noreferrer noopener">
          {url.replace("@", "[at]")}
        </a>
      ),
    },
  ];

  return (
    <LinkParser watchers={watchers}>
      #Far_far_away, behind the word mountains, far from the countries @Vokalia and @Consonantia, there live the blind
      texts. Separated they live in @Bookmarksgrove right at the coast of the Semantics*, a large language ocean. A
      small river named Duden flows by their place and supplies it with the necessary regelialia. \n Credit: \n
      https://www.blindtextgenerator.com/lorem-ipsum \n Contact Me: [email protected]
    </LinkParser>
  );
}

Props

| Name | Required | Type | Default | Description | | :--------------- | :------- | :-------- | :--------------- | :----------------------------------------------------------------------------- | | children       | Yes | Node   |           | Only the text will be rendered | | newLineWatcher | | String | \\n | If parseNewLine is true, what character(s) should be considered as new line. | | parseNewLine | | Boolean | true | Whether new line should be parsed as <br /> | | watchers       | | Array   | link watcher     | An array of watchers |

Watcher props

| Name | Required | Type | Default | Description | | :------------- | :------- | :---------------------------- | :--------------- | :------------------------------------------------ | | render       | Yes | Function   |  anchor       | A function to customize watcher render | | type       | | startsWith or endsWith | startsWith     | Where should it watch to find the watchFor clue | | watchFor | Yes | link or email or String | link | What to look up in the string |