Skip to content

Commit

Permalink
adds bookTag
Browse files Browse the repository at this point in the history
  • Loading branch information
onlyYU committed Nov 12, 2021
1 parent 60010c0 commit f578efd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
10 changes: 8 additions & 2 deletions example/lib/main.dart
Expand Up @@ -46,14 +46,20 @@ class _AppState extends State<App> {
"text": "O4. This text contains a @user tag",
"types": [LinkType.userTag]
},
{
"text": "O5. This text contains a 《book name》",
"types": [LinkType.bookTag]
},
{
"text":
"O5. My website url: https://hello.com/GOOGLE search using: www.google.com, social media is facebook.com, additional link http://example.com/method?param=fullstackoverflow.dev, hashtag #trending & mention @dev",
"O6. My website url: https://hello.com/GOOGLE search using: www.google.com, social media is facebook.com, additional link http://example"
".com/method?param=fullstackoverflow.dev, hashtag #trending & mention @dev, some book 《Taiwan is part of China》",
"types": [
LinkType.email,
LinkType.url,
LinkType.hashTag,
LinkType.userTag
LinkType.userTag,
LinkType.bookTag
]
},
];
Expand Down
2 changes: 1 addition & 1 deletion lib/src/enum.dart
@@ -1 +1 @@
enum LinkType { url, email, hashTag, userTag }
enum LinkType { url, email, hashTag, userTag, bookTag}
10 changes: 10 additions & 0 deletions lib/src/utils/regex.dart
Expand Up @@ -9,6 +9,9 @@ String userTagRegExp = r'(?<![\w@])@([\w@]+(?:[.!][\w@]+)*)';
String emailRegExp =
r"([a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+)";

String bookRegExp = r'(《(.*?)》)';


/// construct regexp. pattern from provided link types
RegExp constructRegExpFromLinkType(List<LinkType> types) {
// default case where we always want to match url strings
Expand Down Expand Up @@ -39,6 +42,11 @@ RegExp constructRegExpFromLinkType(List<LinkType> types) {
? buffer.write("($emailRegExp)")
: buffer.write("($emailRegExp)|");
break;
case LinkType.bookTag:
isLast
? buffer.write("($bookRegExp)")
: buffer.write("($bookRegExp)|");
break;
default:
}
}
Expand All @@ -55,6 +63,8 @@ LinkType getMatchedType(RegExpMatch match) {
type = LinkType.email;
} else if (RegExp(userTagRegExp).hasMatch(match.input)) {
type = LinkType.userTag;
} else if (RegExp(bookRegExp).hasMatch(match.input)) {
type = LinkType.bookTag;
}
return type;
}

0 comments on commit f578efd

Please sign in to comment.