fix mentions and hashtags

This commit is contained in:
Egor Kislitsyn 2019-02-18 18:55:17 +07:00
parent 33c6754592
commit 8f7496b1d3
2 changed files with 25 additions and 6 deletions

View file

@ -100,10 +100,10 @@ defmodule AutoLinker.Parser do
|> do_parse(Map.delete(opts, :phone))
end
defp do_parse(input, %{mention: true} = opts) do
defp do_parse(input, %{hashtag: true} = opts) do
input
|> do_parse(opts, {"", "", :parsing}, &check_and_link_mention/3)
|> do_parse(Map.delete(opts, :mention))
|> do_parse(opts, {"", "", :parsing}, &check_and_link_hashtag/3)
|> do_parse(Map.delete(opts, :hashtag))
end
defp do_parse(input, %{extra: true} = opts) do
@ -144,10 +144,10 @@ defmodule AutoLinker.Parser do
do_parse(input, Map.delete(opts, :url))
end
defp do_parse(input, %{hashtag: true} = opts) do
defp do_parse(input, %{mention: true} = opts) do
input
|> do_parse(opts, {"", "", :parsing}, &check_and_link_hashtag/3)
|> do_parse(Map.delete(opts, :hashtag))
|> do_parse(opts, {"", "", :parsing}, &check_and_link_mention/3)
|> do_parse(Map.delete(opts, :mention))
end
defp do_parse(input, _), do: input

View file

@ -100,6 +100,25 @@ defmodule AutoLinkerTest do
assert MapSet.to_list(tags) == ["#hello", "#world"]
end
test "mention handler and hashtag prefix" do
text =
"Hello again, @user.<script></script>\nThis is on another :moominmamma: line. #2hu #epic #phantasmagoric"
handler = fn "@" <> user = mention, _, _, _ ->
~s(<span class='h-card'><a href='#/user/#{user}'>@<span>#{mention}</span></a></span>)
end
expected =
"Hello again, <span class='h-card'><a href='#/user/user'>@<span>@user</span></a></span>.&lt;script&gt;&lt;/script&gt;\nThis is on another :moominmamma: line. <a class=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\" href=\"/tag/2hu\">#2hu</a> <a class=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\" href=\"/tag/epic\">#epic</a> <a class=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\" href=\"/tag/phantasmagoric\">#phantasmagoric</a>"
assert AutoLinker.link(text,
mention: true,
mention_handler: handler,
hashtag: true,
hashtag_prefix: "/tag/"
) == expected
end
end
describe "mentions" do