commit: 2a050e1cbe3005bec25cb321f4c60cd656ac7ed9
parent e16136f70950c60a879fbd965c871946fedb51bf
Author: Sergey Suprunenko <suprunenko.s@gmail.com>
Date: Mon, 16 Nov 2020 18:57:01 +0100
Ignore non-word chars in the beginning of mentions
Diffstat:
3 files changed, 14 insertions(+), 3 deletions(-)
diff --git a/CHANGELOG.md b/CHANGELOG.md
@@ -7,6 +7,7 @@
- Hashtags followed by HTML tags "a", "code" and "pre" were not detected
- Incorrect parsing of HTML links inside HTML tags
- Punctuation marks in the end of urls were included in the html links
+- Incorrect parsing of mentions with symbols before them
## 0.2.0 - 2020-07-21
diff --git a/lib/linkify/parser.ex b/lib/linkify/parser.ex
@@ -15,7 +15,7 @@ defmodule Linkify.Parser do
# @user
# @user@example.com
- @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
+ @match_mention ~r/^(?:\W*)?(?<long>@[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])?)*)|^(?:\W*)?(?<short>@[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
@@ -254,8 +254,9 @@ defmodule Linkify.Parser do
def ip?(buffer), do: Regex.match?(@match_ip, buffer)
def match_mention(buffer) do
- case Regex.run(@match_mention, buffer) do
- [mention] -> mention
+ case Regex.run(@match_mention, buffer, capture: [:long, :short]) do
+ [mention, ""] -> mention
+ ["", mention] -> mention
_ -> nil
end
end
diff --git a/test/linkify_test.exs b/test/linkify_test.exs
@@ -386,6 +386,15 @@ defmodule LinkifyTest do
) == expected
end
+ test "mentions with symbols before them" do
+ text = "@@example hey! >@@test@example.com"
+
+ expected =
+ "@<a href=\"/users/example\">@example</a> hey! >@<a href=\"/users/test@example.com\">@test@example.com</a>"
+
+ assert Linkify.link(text, mention: true, mention_prefix: "/users/") == expected
+ end
+
test "invalid mentions" do
text = "hey user@example"