Browse Source

fix mentions and hashtags

merge-requests/2/head
Egor Kislitsyn 5 years ago
parent
commit
8f7496b1d3
2 changed files with 25 additions and 6 deletions
  1. +6
    -6
      lib/auto_linker/parser.ex
  2. +19
    -0
      test/auto_linker_test.exs

+ 6
- 6
lib/auto_linker/parser.ex 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


+ 19
- 0
test/auto_linker_test.exs 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


Loading…
Cancel
Save