Skip to content
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

Merged
merged 3 commits into from
Mar 18, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class LinkifyExample extends StatelessWidget {
throw 'Could not launch $url';
}
},
text: "Made by https://cretezy.com",
text: "Made by https://cretezy.com\n\nMail: mailto:charles@cretezy.com",
),
),
),
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ environment:
dependencies:
flutter:
sdk: flutter
url_launcher: ^3.0.3
url_launcher: ^5.0.1
flutter_linkify:
path: ..

Expand Down
49 changes: 36 additions & 13 deletions lib/linkify.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,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.
Expand All @@ -40,29 +45,47 @@ 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)}");
print("humanizing url ${urlMatch.group(2)}");
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)));
print("not humanizing url ${urlMatch.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
print("humanizing email ${emailMatch.group(2)}");
list.add(LinkElement(
emailMatch.group(2),
emailMatch.group(2).replaceFirst(RegExp(r"mailto:"), ""),
));
}

list.addAll(linkify(text, humanize: humanize));
}

Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
version: 1.2.0
version: 2.0.0

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

Expand Down