commit: 056d7f2f3708863bb7ddcb29e8be9f14c53b29f5
parent 8f7496b1d3c291b7be563ffce5abba35c1d09406
Author: Egor Kislitsyn <egor@kislitsyn.com>
Date: Mon, 18 Feb 2019 19:06:06 +0700
improve mentions matching
Diffstat:
2 files changed, 3 insertions(+), 19 deletions(-)
diff --git a/lib/auto_linker/parser.ex b/lib/auto_linker/parser.ex
@@ -40,7 +40,7 @@ defmodule AutoLinker.Parser do
# @user
# @user@example.com
- @match_mention ~r/^@[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@?[a-zA-Z0-9_-](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*/u
+ @match_mention ~r/^@[a-zA-Z\d_-]+@[a-zA-Z0-9_-](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*|@[a-zA-Z\d_-]+/u
# https://www.w3.org/TR/html5/forms.html#valid-e-mail-address
@match_email ~r/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/u
diff --git a/test/auto_linker_test.exs b/test/auto_linker_test.exs
@@ -30,19 +30,15 @@ defmodule AutoLinkerTest do
test "all kinds of links" do
text =
- "hello @user google.com https://ddg.com 888 888-8888 #tag user@email.com [google.com](http://google.com) irc:///mIRC"
+ "hello google.com https://ddg.com 888 888-8888 user@email.com [google.com](http://google.com) irc:///mIRC"
expected =
- "hello <a href=\"https://example.com/user/user\">@user</a> <a href=\"http://google.com\">google.com</a> <a href=\"https://ddg.com\">ddg.com</a> <a href=\"#\" class=\"phone-number\" data-phone=\"8888888888\">888 888-8888</a> <a href=\"https://example.com/tag/tag\">#tag</a> <a href=\"mailto:user@email.com\" >user@email.com</a> <a href='http://google.com'>google.com</a> <a href=\"irc:///mIRC\">irc:///mIRC</a>"
+ "hello <a href=\"http://google.com\">google.com</a> <a href=\"https://ddg.com\">ddg.com</a> <a href=\"#\" class=\"phone-number\" data-phone=\"8888888888\">888 888-8888</a> <a href=\"mailto:user@email.com\" >user@email.com</a> <a href='http://google.com'>google.com</a> <a href=\"irc:///mIRC\">irc:///mIRC</a>"
assert AutoLinker.link(text,
phone: true,
markdown: true,
email: true,
- mention: true,
- mention_prefix: "https://example.com/user/",
- hashtag: true,
- hashtag_prefix: "https://example.com/tag/",
scheme: true,
extra: true,
class: false,
@@ -143,18 +139,6 @@ defmodule AutoLinkerTest do
mention_prefix: "https://example.com/user/"
) == expected
end
-
- test "skip if starts with @@" do
- text = "hello @@user and @anotherUser"
-
- expected =
- "hello @@user and <a href='https://example.com/user/anotherUser' class='auto-linker' target='_blank' rel='noopener noreferrer'>@anotherUser</a>"
-
- assert AutoLinker.link(text,
- mention: true,
- mention_prefix: "https://example.com/user/"
- ) == expected
- end
end
describe "hashtag links" do