Added @userTag handling

This commit is contained in:
Filip 2021-09-02 09:54:14 +02:00
parent 7e54777385
commit d5a37832b5
2 changed files with 14 additions and 15 deletions

View File

@ -1 +1 @@
enum LinkType { url, email, hashTag } enum LinkType { url, email, hashTag, userTag }

View File

@ -4,14 +4,14 @@ String urlRegExp = r'(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-?=%.]+';
String hashtagRegExp = r'(#+[a-zA-Z0-9(_)]{1,})'; String hashtagRegExp = r'(#+[a-zA-Z0-9(_)]{1,})';
String emailRegExp = String userTagRegExp = r'(?<![\w@])@([\w@]+(?:[.!][\w@]+)*)';
r"([a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+)";
String emailRegExp = r"([a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+)";
/// construct regexp. pattern from provided link linkTypes /// construct regexp. pattern from provided link linkTypes
RegExp constructRegExpFromLinkType(List<LinkType> linkTypes) { RegExp constructRegExpFromLinkType(List<LinkType> linkTypes) {
// default case where we always want to match url strings // default case where we always want to match url strings
if (linkTypes.length == 1 && linkTypes.first == LinkType.url) if (linkTypes.length == 1 && linkTypes.first == LinkType.url) return RegExp(urlRegExp);
return RegExp(urlRegExp);
StringBuffer _regexBuffer = StringBuffer(); StringBuffer _regexBuffer = StringBuffer();
for (var i = 0; i < linkTypes.length; i++) { for (var i = 0; i < linkTypes.length; i++) {
@ -19,19 +19,16 @@ RegExp constructRegExpFromLinkType(List<LinkType> linkTypes) {
final _isLast = i == linkTypes.length - 1; final _isLast = i == linkTypes.length - 1;
switch (_type) { switch (_type) {
case LinkType.url: case LinkType.url:
_isLast _isLast ? _regexBuffer.write("($urlRegExp)") : _regexBuffer.write("($urlRegExp)|");
? _regexBuffer.write("($urlRegExp)")
: _regexBuffer.write("($urlRegExp)|");
break; break;
case LinkType.hashTag: case LinkType.hashTag:
_isLast _isLast ? _regexBuffer.write("($hashtagRegExp)") : _regexBuffer.write("($hashtagRegExp)|");
? _regexBuffer.write("($hashtagRegExp)") break;
: _regexBuffer.write("($hashtagRegExp)|"); case LinkType.userTag:
_isLast ? _regexBuffer.write("($userTagRegExp)") : _regexBuffer.write("($userTagRegExp)|");
break; break;
case LinkType.email: case LinkType.email:
_isLast _isLast ? _regexBuffer.write("($emailRegExp)") : _regexBuffer.write("($emailRegExp)|");
? _regexBuffer.write("($emailRegExp)")
: _regexBuffer.write("($emailRegExp)|");
break; break;
default: default:
} }
@ -45,8 +42,10 @@ LinkType getMatchedType(RegExpMatch match) {
_type = LinkType.url; _type = LinkType.url;
} else if (RegExp(hashtagRegExp).hasMatch(match.input)) { } else if (RegExp(hashtagRegExp).hasMatch(match.input)) {
_type = LinkType.hashTag; _type = LinkType.hashTag;
} else if (RegExp(hashtagRegExp).hasMatch(match.input)) { } else if (RegExp(emailRegExp).hasMatch(match.input)) {
_type = LinkType.email; _type = LinkType.email;
} else if (RegExp(userTagRegExp).hasMatch(match.input)) {
_type = LinkType.userTag;
} }
return _type; return _type;
} }