Ignore non-word chars in the beginning of mentions

This commit is contained in:
Sergey Suprunenko 2020-11-16 18:57:01 +01:00
parent e16136f709
commit 2a050e1cbe
No known key found for this signature in database
GPG key ID: 5DCA7D1BE3914F9C
3 changed files with 14 additions and 3 deletions

View file

@ -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

View file

@ -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

View file

@ -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"