Giter Club home page Giter Club logo

react-link-parser's Introduction

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

react-link-parser's People

Contributors

amir2mi avatar mahdikhashan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.