Coder Social home page Coder Social logo

winhtaikaung / react-tiny-link Goto Github PK

View Code? Open in Web Editor NEW
256.0 5.0 48.0 7.83 MB

Convert your links into rich previews

Home Page: https://winhtaikaung.github.io/react-tiny-link

JavaScript 5.03% TypeScript 94.93% Shell 0.04%
react link preview cards

react-tiny-link's Introduction

React-Tiny-Link

Convert your links into beautiful previews

Yet anotherReact link preview component with cards for web without a specific backend.

npm version npm GitHub code size in bytes NPM

All Contributors

NPM

CORS enabled?

No. You may need a CORS proxy to use this component. But if you dont have one, we made the component to use https://cors-anywhere.herokuapp.com as default proxy. Thanks to Rob. It saves my time for accessing urls.

Installation


npm install --save react-tiny-link

Usage & Configuration

import { ReactTinyLink } from 'react-tiny-link'
;<ReactTinyLink
  cardSize="small"
  showGraphic={true}
  maxLine={2}
  minLine={1}
  url="https://www.amazon.com/Steve-Madden-Mens-Jagwar-10-5/dp/B016X44MKA/ref=lp_18637582011_1_1?srs=18637582011&ie=UTF8&qid=1550721409&sr=8-1"
/>

Props & methods

PropName Description PropType value required
url URL to be display as preview string true
cardSize Size of the card string default (small) small,large false
maxLine Maximum number of line to ellipsis number 2 false
minLine Minimum number of line to ellipsis number 1 false
width Width of the link preview card number default(100vw) false
header Default Header content string null false
description Default description content string null false
proxyUrl Proxy URL to pass that resolve CORS string default(https://cors-anywhere.herokuapp.com) false
showGraphic Boolean value to display graphics boolean default(true) false
autoPlay Boolean value to play the media if provided url is video boolean default(false) false
defaultMedia Default value to provide the media for failure cases string N.A false
noCache Disables cache of link result boolean default(false) false
onError Error callback on when the url failed to fetch onError(error:Error) N.A false
onSuccess Success callback on when the url was fetched successfully onSuccess(data:IResponseData) N.A false
onClick Click event which will allow to add custom implementation onClick event if it was not provided the component itself will render as <a></a>. onClick(e:Event,data:IResponseData) default(null) false
loadSecureUrl Load only secure ( https:// ) resources. If no secure resource was found, then don't render the <img> and <video> element boolean default(false) false
requestHeaders Request headers that will override the fetch headers Record<string, string>; default(false) false

Hook usage

import { useScrapper } from 'react-tiny-link'

const [result, loading, error] = useScrapper({
  url:
    'https://www.amazon.com/Steve-Madden-Mens-Jagwar-10-5/dp/B016X44MKA/ref=lp_18637582011_1_1?srs=18637582011&ie=UTF8&qid=1550721409&sr=8-1',
})

Next.JS

For Next.Js you will have to turn off ssr false and use dynamic import. Because react-tiny-link uses styled-components as styling library which uses browser API window to attach scCGSHMRCache and stuffs after build.

import dynamic from "next/dynamic";
const ReactTinyLink = dynamic(
  () => {
    return import("react-tiny-link").then((mod) => mod.ReactTinyLink);
  },
  { ssr: false }
);

Params

PropName Description PropType value required
url URL to be display as preview string true
proxyUrl Proxy URL to pass that resolve CORS string default(https://cors-anywhere.herokuapp.com) false
defaultMedias Default value to provide the media for failure cases string[] N.A false
defaultValue Default response to provide for failure cases IReactTinyLinkData N.A false
noCache Disables cache of link result boolean default(false) false
onError Error callback on when the url failed to fetch onError(error:Error) N.A false
onSuccess Success callback on when the url was fetched successfully onSuccess(data:IResponseData) N.A false

Demo App &

CodeSandbox

Edit React Tiny Link

Contributing

  1. Fork it

  2. Create your feature branch (git checkout -b my-new-feature)

  3. Commit your changes (git commit -am 'Added some feature')

  4. Push to the branch (git push origin my-new-feature)

  5. Create new Pull Request

Contributors

Thanks goes to these wonderful people (emoji key):


Vladimir Moushkov

๐Ÿ’ป

Serhii Khoma

๐Ÿ’ป ๐Ÿ›

Hitesh Kumar

๐Ÿ’ป ๐Ÿ›

VadymMoiseyenkoAgiliway

๐Ÿ’ป ๐Ÿ›

nastikue

๐Ÿ›

sndsabin

๐Ÿ’ป ๐Ÿ›

Aviskar KC

๐Ÿ› ๐Ÿ’ป

Alexandre Bernard

๐Ÿ› ๐Ÿ’ป

Liz

๐Ÿ’ป ๐Ÿ’ก

This project follows the all-contributors specification. Contributions of any kind welcome!

License

MIT

Twitter Follow

react-tiny-link's People

Contributors

allcontributors[bot] avatar aviskarkc10 avatar dependabot[bot] avatar hitesh399 avatar hiteshsingsys avatar sndsabin avatar srghma avatar vadymmoiseyenkoagiliway avatar vladimirmoushkov avatar winhtaikaung avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

react-tiny-link's Issues

feature request: optionally cache requests

Hi, first of all thank you for this awesome and interesting work.
It would be usefull to introduce a cache system in order to save the response of a request, instead of performing again the request.

Proxy doesn't work in production

The proxy doesn't work after npm run build but does work when running npm run start - we've tried setting it explicitly

return <ReactTinyLink
  cardSize="small"
  showGraphic={ true }
  maxLine={ 2 }
  minLine={ 1 }
  url={ link }
  proxyUrl="https://cors-anywhere.herokuapp.com"
/>

As well as leaving it as the default, and it doesn't work in either configuration.

Cheers for the help

feature request: provide a custom hook for grabbing link meta data

  • Issue type: feature request

  • Description:
    Hi, i was just looking at the source and i thought it would be pretty neat if we have some sort of custom hook for just fetching the URL meta data.
    I noticed you have a useMountFetch hook and i think it will just work fine.
    Renaming & exporting it to something like useScrapper or useScrapeData would be sufficient i think.

  • Use cases
    Users will have more flexibility and can style the data or use their own custom components.
    If we leverage treeshaking then we don't have to bundle the react-tiny-link specific components.
    It will also solve issues for example if a project does not have styled-component then their project won't have it. and users would create their own custom components with the useScrapper hook
    might also solve #35 in some regards.

Unhandled Rejection (Error): [object Object]

I am working on something that is like a grid board ( Pinterest ) .... and when the react-tiny-link component is used in a grid item sub component that is rendered.. and then the parent component is changed ( aka changing to a different board / gird list ) and the Url's have not completed... I see Uncaught (in promise) [ object ] in the console and react throws up a Unhandled Rejection (Error): [object Object] page...

So I am looking for some guidance on how to use the component in a context like I described ?

add 'providor' to 'result' object of useScrapper

Currently, the result object contains: content, description, image, title, url, video

To have the same datas as in the main ReactTinyLink example, the provider (amazon.com in the readme example) should be added to the result object.

Too Many Requests

How to solve this one? We may need a cancellationToken if it has the same request.

when putting an url without http:// it crashes

hello, I'm using this library the latest version and when I tried to put a link without HTTP:// it crashed with error: TypeError Failed to construct 'URL': Invalid URL
i also tried on the sandbox and it's with the same error

anyone had this issue?

Preview not displayed (CORS 403 Forbidden issue)

If using the default proxy, after a while the preview stopped showing and I can see that the calls are getting 403 error. This seems to be the issue in other projects using react-tiny-link

Is the default proxy only intended for dev use? Should I be setting up a proxy server? Any tips around the different ways to resolve this will be appreciated.

Page breaks when I set url

Hi. Thank you for amazing library.

I use react-tiny-link for view links in comments. First I take a string and match it with regExp, pick up all the links and then use the map method to display all the links. As soon as the first link is transmitted, the entire application crashes. And I get this error:


Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object.

Check the render method of Comment.

I am absolutely sure that I send the string. Can you help me?

Hello from microlink

Hello, sir

We noted you did a copy of our library, Microlink SDK.

Nothing bad there, but after analyzing the code, I noted that the only difference here is, while Microlink SDK uses Microlink API for getting the data, you are using a set of rules over popular sites.

Well, actually you can do that using setData:

import Microlink from '@microlink/react'

export default () => (
  <Microlink
    url='https://www.instagram.com/p/Bt6EMQhHHIr/'
    setData={data => ({ 
     /* your data interface here! */
    }})
  />

Doing that, you don't need to rewrite all the library; just create a Higher Order Component over Microlink SDK.

Also, I'm very open to collaborate and join efforts ๐Ÿ™‚

gatsby support

image

I can't embed this component in my gatsby app
I just import this component and rendered it

accessibility issue with multiple header and footer using deque extension

Im working on accessibility on my react application and I'm using react tiny link at two places, Im using this extension for accessibility checks .
This extension raising issue that Contentinfo landmark should not be contained in another landmark because now there are two header one is mine and one is created by React tiny link
is there any solution for this
image

Some demo examples not working

Hello and thanks for this module!

Looking at the demo, it seems like the Amazon and mp3 example aren't working and i'm also getting the same issue when i used it. Any idea what's wrong?

thanks

AutoPlay not Working

<ReactTinyLink
                              cardSize="small"
                              showGraphic={true}
                              maxLine={2}
                              minLine={1}
                              autoPlay={true}
                              url="youtube url"
                            />

Preview of a Twitter page doesn't show correctly

Thanks for your awesome work of building this package. Here is an issue about Twitter.

Description

Preview the link https://twitter.com/linuxfoundation/status/1257347380728512513 in the demo page, but no image and title are rendered.

image

Analysis

In the demo, the fetched html via https://cors-anywhere.herokuapp.com/https://twitter.com/linuxfoundation/status/1257347380728512513 xhr, doesn't contain the title and image meta info. A site level html template will be returned.

If we visit twitter via either the original url https://twitter.com/linuxfoundation/status/1257347380728512513 in a separate browser tab or via curl, or we access https://cors-anywhere.herokuapp.com/https://twitter.com/linuxfoundation/status/1257347380728512513 via curl in command line and set user-agent to empty, we'll retrieve the html page with the both title and image meta.

Question

Have you tested Twitter links before? What's the recommended solution to resolve this issue?

Show only the image without descriptions

Hey.... nice work....first of all congratulations.....

I write because i have an array of links and need to show in a list but only the image... is tthis posible with your library??

waiting a fast response.

Memory leak when component unmount before fetch done

Hi ๐Ÿ‘‹

Problem

The fetchURL function sets the loading and result states in an asynchronous callback. If the component has been unmounted in the meantime, this results to a memory leak.

Reproduction

Just put the ReactTinyLink component in a modal, close the modal before the fetch is done "et voilร " ๐Ÿšฐ

Possible fix

You could use a cleanup function in your useEffectAsync which cancels the fetch and sets a variable which prevents the fetchURL to set the state.

TypeError: Failed to construct 'URL': Invalid URL

We are trying to render link previews from user input data which means that text which looks like URLs may end up being invalid.
Can this be made more fault tolerant?

Stack below:
t.getHostname
src/utils.ts:20
17 |
18 | export const getHostname = href => {
19 | const { hostname } = new URL(href);

20 | return hostname.replace(REGEX_STRIP_WWW, '');
21 | };
22 |
23 | export const isLarge = cardSize => cardSize === 'large';
View compiled
t.ReactTinyLink
src/ReactTinyLink.tsx:55
52 |
53 |
54 |


55 |
| ^ 56 | {getHostname(url)}
57 |
58 |
View compiled

fixRelativeUrls returns broken link for some images

  • fixRelativeUrls doesn't work as expected

    fixRelativeUrls doesn't work as expected for the absolute path for some url https://example.com/test (in either case if provided by user or if present in <base>) :

    <img class="svg" src="/images/example-logo.svg" alt="example-alt-text"/>

    Expected:
    https://example.com/images/example-logo.svg

    Returns:
    https://example.com/test/images/example-logo.svg

    export const fixRelativeUrls = (baseUrl: string, itemUrl: string) => {

  • baseUrl defaults to user provided url if <base> is not present in DOM

    Proposal: Instead default to origin of the user provided url.

Bundle size & duplicate react-dom.production

Hi, thank you for the amazing lib. it works super nicely.

but i only have one issue with it, is that the bundle size is too large.. i don't know if something is wrong or i'm doing something wrong the size is around 190kb and it also includes react-dom.production for some reason.

image

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.