-
-
Notifications
You must be signed in to change notification settings - Fork 104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added support for mailto: links. #8
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,19 @@ class LinkElement extends LinkifyElement { | |
} | ||
} | ||
|
||
/// Represents an element containing an email address | ||
class EmailElement extends LinkifyElement { | ||
final String emailAddress; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a case where the email address != displayed address? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not now I think.. But it will be when support for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that's still gonna be the same though, since subject, body, etc is simply gonna be properties (not shown) |
||
final String text; | ||
|
||
EmailElement(this.emailAddress, [String text]) : this.text = text ?? emailAddress; | ||
|
||
@override | ||
String toString() { | ||
return "EmailElement: $emailAddress ($text)"; | ||
} | ||
} | ||
|
||
/// Represents an element containing text | ||
class TextElement extends LinkifyElement { | ||
final String text; | ||
|
@@ -25,11 +38,16 @@ class TextElement extends LinkifyElement { | |
} | ||
} | ||
|
||
final _linkifyRegex = RegExp( | ||
final _linkifyUrlRegex = RegExp( | ||
r"(\n*?.*?\s*?)((?:https?):\/\/[^\s/$.?#].[^\s]*)", | ||
caseSensitive: false, | ||
); | ||
|
||
final _linkifyEmailRegex = RegExp( | ||
r"(\n*?.*?\s*?)((mailto:)?[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4})", | ||
caseSensitive: false | ||
); | ||
|
||
/// Turns [text] into a list of [LinkifyElement] | ||
/// | ||
/// Use [humanize] to remove http/https from the start of the URL shown. | ||
|
@@ -40,29 +58,43 @@ List<LinkifyElement> linkify(String text, {bool humanize = false}) { | |
return list; | ||
} | ||
|
||
final match = _linkifyRegex.firstMatch(text); | ||
if (match == null) { | ||
final urlMatch = _linkifyUrlRegex.firstMatch(text); | ||
final emailMatch = _linkifyEmailRegex.firstMatch(text); | ||
if (urlMatch == null && emailMatch == null) { | ||
list.add(TextElement(text)); | ||
} else { | ||
text = text.replaceFirst(_linkifyRegex, ""); | ||
} else if (urlMatch != null){ | ||
text = text.replaceFirst(_linkifyUrlRegex, ""); | ||
|
||
if (match.group(1).isNotEmpty) { | ||
list.add(TextElement(match.group(1))); | ||
if (urlMatch.group(1).isNotEmpty) { | ||
list.add(TextElement(urlMatch.group(1))); | ||
} | ||
|
||
if (match.group(2).isNotEmpty) { | ||
if (urlMatch.group(2).isNotEmpty) { | ||
if (humanize ?? false) { | ||
print("humanizing ${match.group(2)}"); | ||
PieterAelse marked this conversation as resolved.
Show resolved
Hide resolved
|
||
list.add(LinkElement( | ||
match.group(2), | ||
match.group(2).replaceFirst(RegExp(r"https?://"), ""), | ||
urlMatch.group(2), | ||
urlMatch.group(2).replaceFirst(RegExp(r"https?://"), ""), | ||
)); | ||
} else { | ||
print("not humanizing ${match.group(2)}"); | ||
list.add(LinkElement(match.group(2))); | ||
list.add(LinkElement(urlMatch.group(2))); | ||
} | ||
} | ||
|
||
list.addAll(linkify(text, humanize: humanize)); | ||
} else if (emailMatch != null) { | ||
text = text.replaceFirst(_linkifyEmailRegex, ""); | ||
|
||
if (emailMatch.group(1).isNotEmpty) { | ||
list.add(TextElement(emailMatch.group(1))); | ||
} | ||
|
||
if (emailMatch.group(2).isNotEmpty) { | ||
// Always humanize emails | ||
list.add(EmailElement( | ||
emailMatch.group(2).replaceFirst(RegExp(r"mailto:"), "") | ||
)); | ||
} | ||
|
||
list.addAll(linkify(text, humanize: humanize)); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,6 +1,6 @@ | ||||||
name: flutter_linkify | ||||||
description: Turns text URLs into clickable inline links in text for Flutter. | ||||||
version: 1.1.1 | ||||||
description: Turns text URLs and Mailto links into clickable inline links in text for Flutter. | ||||||
version: 1.2.0 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
To match the breaking change above. We could support both onOpen and onOpenLink (and deprecate onOpen). To to you to make the decision! |
||||||
author: Charles Crete <charles@cretezy.com> | ||||||
homepage: https://github.com/Cretezy/flutter_linkify | ||||||
|
||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Breaking change, will require a major bump.