diff --git a/CHANGELOG.md b/CHANGELOG.md index adfc4a7..9cb2132 100644 --- 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 index eab5891..af826c3 100644 --- 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*)?(?@[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*)?(?@[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 index a18dc3c..73ff6d0 100644 --- 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 = + "@@example hey! >@@test@example.com" + + assert Linkify.link(text, mention: true, mention_prefix: "/users/") == expected + end + test "invalid mentions" do text = "hey user@example"