Giter VIP home page Giter VIP logo

linkify's Introduction

linkify pub package

Low-level link (text, URLs, emails, phone numbers, user tags) parsing library in Dart.

Required Dart >=2.12 (has null-safety support).

Flutter library.

Pub - API Docs - GitHub

Install

Install by adding this package to your pubspec.yaml:

dependencies:
  linkify: ^5.0.0

Usage

import 'package:linkify/linkify.dart';

linkify("Made by https://cretezy.com [email protected]");
// Output: [TextElement: 'Made by ', UrlElement: 'https://cretezy.com' (cretezy.com), TextElement: ' ', EmailElement: '[email protected]' ([email protected])]

Options

You can pass LinkifyOptions to the linkify method to change the humanization of URLs (turning https://example.com to example.com):

linkify("https://cretezy.com");
// [UrlElement: 'https://cretezy.com' (cretezy.com)]

linkify("https://cretezy.com", options: LinkifyOptions(humanize: false));
// [UrlElement: 'https://cretezy.com' (https://cretezy.com)]
  • humanize: Removes http/https from shown URLs
  • removeWww: Removes www. from shown URLs
  • looseUrl: Enables loose URL parsing (should parse most URLs such as abc.com/xyz)
    • defaultToHttps: When used with [looseUrl], default to https instead of http
  • excludeLastPeriod: Excludes . at end of URLs

Linkifiers

You can pass linkifiers to linkify as such:

linkify("@cretezy", linkifiers: [UserTagLinkifier()]);

Available linkifiers:

  • EmailLinkifier
  • UrlLinkifier
  • PhoneNumberLinkifier
  • UserTagLinkifier

Custom Linkifier

You can write custom linkifiers for phone numbers or other types of links. Look at the URL linkifier for an example.

This is the flow:

  • Calls parse in the linkifier with a list of LinkifyElement. This starts as [TextElement(text)]
  • Your parsers then splits each element into it's parts. For example, [TextElement("Hello https://example.com")] would become [TextElement("Hello "), UrlElement("https://example.com")]
  • Each parsers is ran in order of how they are passed to the main linkify function. By default, this is URL and email linkifiers

linkify's People

Contributors

atillaturkmen avatar cretezy avatar esteveaguilera avatar ghunkins avatar hscogt avatar jdosornio avatar matehat avatar nilsreichardt avatar senne021 avatar spencerlindemuth 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

Watchers

 avatar  avatar  avatar  avatar

linkify's Issues

Log spam

I'm aware that this issue has already been fixed in 9038dd7, but it has not been released yet.

Please release 2.0.3 so that the fix can be used, would also be nice if flutter_linkify can update to [email protected] straight after.

Thanks for the great work!

Text preceded by an /r/n (carriage return + linefeed) causes REGEX parser to fail

Hello!

Great plugin! Saved me so much time fiddling with text spans!!!!

I ran into one problem, I'm pulling text from a database connection and the text is returned with carriage returns and linefeeds, which seems to make the REGEX parser fail. To fix it, I do a text replace of all '\r\n' to simply '\n' and Linkify works perfectly.

Might be something to look at in a future release, but in the interim the fix is easy as in the below code:

Linkify(
  text: widget.kennel['kennelDescription'].toString().replaceAll('\r\n', '\n'),
  style: bodyStyle,
  linkStyle: bodyStyleYellow,
  humanize: true,
  onOpen: (LinkableElement link) async {
    if (await canLaunch(link.url)) {
      await launch(link.url);
    } else {
      Utilities.showAlert(context, 'Unable to open link', 'The app was unable to open ${link.url}', 'OK');
    }
  },
),

Suport for phone numbers

Hi! How difficult would it be to add support for phone numbers? I am willing to contribute on this issue if guidance is provided.

looseUrl option identifies text with multiple periods as a url

Issue:

Currently, the following patterns of text are being identified as url when looseUrl option is true when using linkify.

pattern1 -> 'awdaw....aw'
pattern2 -> 'awdaw...wad...wadw'
and so on...

Expected behaviour:

Technically, this shouldn't be identified as urls as there are multiple periods present consecutively and thus is an invalid url pattern.

Escape character \r causes any URLs after it not to become hyperlinked

When a \r escape character is in the string given to the Linkify widget, anything after that escape character does not become a clickable hyperlink. This is a problem as I'm getting information from an API but the data returned contains \r\n for newlines (which is a common escape character sequence). A workaround is to simply remove all \r escape characters from the string but there wasn't any indication that was needed.

Example:
Note: onOpen code has been omitted for simplicity.
No \r
Linkify( text: 'https://www.google.com\nhttps://www.google.com', )
This code works fine as it does not contain \r.
Has \r
Linkify( text: 'https://www.google.com\r\nhttps://www.google.com', )
This code will cause only the first URL to become hyperlinked. The second one will not become hyperlinked as it is after the \r escape character. Using looseURL does not resolve the issue.
screenshot

This may be the same issue as Cretezy/flutter_linkify#47 but I cannot say for sure as the source string is not provided.

Please implement markdown support for embedded links

Something like this in text:

Would you like to visit [Google](https://www.google.com)?

Will be rendered as

Would you like to visit Google? (in blue, of course)

You can use flutter_markdown package for this?

Congratulations for your excellent package!

Email RegExp should be simpler.

Currently, the only way to know if an email address is valid is to send an email address (and potentially wait for an action, like clicking a link to validate, etc.)

tl;dr

Please use this RegExp:

r'.+@.+'

(No need for mailto: too)

Boring stuff below

Some examples for perfectly valid email addresses:

These addresses above are all valid!

If you really, really want to be kinda (covers 99.99% of the cases) compatible with some of the RFCs, you can use this RegExp (I use it in production, but lately considering to drop it):

r'^([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x22([^\x0d\x22\x5c\x80-\xff]|\x5c[\x00-\x7f])*\x22))*\x40([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d)(\x2e([^\x00-\x20\x22\x28\x29\x2c\x2e\x3a-\x3c\x3e\x40\x5b-\x5d\x7f-\xff]+|\x5b([^\x0d\x5b-\x5d\x80-\xff]|\x5c[\x00-\x7f])*\x5d))*\.?$'

More info:

Support to custom schema

It would be nice if this library and flutter_linkify recognized custom schemas for deeplinks.

Text with both loose and not loose URLs are not being parsed properly

When you have a text like example.com and http://example.com and use the looseUrl: true LinkifyOption, the first URL is not being parsed as an UrlElement, but if you have the text example.com and example.com it works as expected.

I've run this test:

expectListEqual(
    linkify(
        'example.com and http://example.com', 
        options: LinkifyOptions(looseUrl: true),
    ),
    [UrlElement('http://example.com', 'example.com'),
    TextElement(' and '),
    UrlElement('http://example.com', 'example.com')]
);

And gives this output:

Expected: <true>
Actual: <false>
Expected 
    [TextElement: 'example.com and ', 
    LinkElement: 'http://example.com' (example.com)] 
to be 
    [LinkElement: 'http://example.com' (example.com), 
    TextElement: ' and ', 
    LinkElement: 'http://example.com' (example.com)]

While this test runs ok

expectListEqual(
    linkify(
        'example.com and example.com', 
        options: LinkifyOptions(looseUrl: true),
    ),
    [UrlElement('http://example.com', 'example.com'),
    TextElement(' and '),
    UrlElement('http://example.com', 'example.com')]
);

Considers words at the end of a sentence as a valid url when looseUrl = true.

Hey!

Really thanks for making this package and it has proven to be quite useful for us. There's one issue however that I found. If looseUrl is set to true then words at the end of sentences are also considered as valid links. It disregards the space after the period. I'm not sure if this is the right package for this issue but I'm still going to add it here.

Here's a live example.
Screenshot 2020-06-08 at 2 37 39 PM

And here's a code snippet.

Linkify(
            text: text,
            maxLines: maxLines,
            overflow: TextOverflow.clip,
            textAlign: TextAlign.start,
            options: LinkifyOptions(
              looseUrl: true,
            ),
            onOpen: (link) async {
              if (await canLaunch(link.url)) {
                await launch(link.url);
              }
            },
)

I'm using package version flutter_linkify: 3.1.2 and flutter version 1.12.13+hotfix.8.

Looking forward to hearing from you!
Thanks again for this package.
Cheers.

Text with three dots is parsed as URL while text with two dots is not

The text ...and is parsed as an URL while the text ..and is not.

Test to reproduce it:

expectListEqual(
    linkify("...and", options: LinkifyOptions(looseUrl: true)),
    [TextElement('...and')],
);

Output:
Expected [LinkElement: 'http://...and' (...and)] to be [TextElement: '...and']

This test runs ok:

expectListEqual(
    linkify("..and", options: LinkifyOptions(looseUrl: true)),
    [TextElement('..and')],
);

Incorrect link type

When I turn on loose url detection it will create a link at the end of any sentence containing a "."

looseUrl highlighting words at end of sentence with punctuation

SelectableLinkify( onOpen: onOpen, text: "github.com test.", style: TextStyle(color: Colors.black), linkStyle: TextStyle( color: Colors.blue ), options: LinkifyOptions( looseUrl: true ), ),
The above code results in the following text to be highlighted:
Screen Shot 2020-07-16 at 18 52 04

The looseUrl option has done a good job of identifying most loose urls but its also highlighting anything with a period at the end. Is it possible to modify the _looseUrlRegex to require there be at least one non-whitespace character?

Not able to detect Multiple URL.

I am trying two display two URLs with the below code but it's detecting only the first one.
This is how it's displaying in the application.

Screenshot 2020-06-09 at 5 20 22 PM

Linkify(
          onOpen: (link) async => {
            if (await canLaunch(link.url))
              {
                await launch(link.url),
              }
            else
              {
                throw 'Could not launch $link',
              }
          },
          text: message,
          style: TextStyle(
            color: color,
            fontSize: fontSize,
            fontWeight: fontWeight,
            fontFamily: Fonts.TITLE,
          ),
          options: LinkifyOptions(humanize: false),
        ),

flutter_linkify: ^3.1.3

[βœ“] Flutter (Channel stable, v1.17.0, on Mac OS X 10.14.5 18F132, locale en-IN)

[βœ“] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[βœ“] Xcode - develop for iOS and macOS (Xcode 11.3.1)
[βœ“] Android Studio (version 3.6)
[βœ“] VS Code (version 1.45.1)
[βœ“] Connected device (1 available)

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.