From 8f7496b1d3c291b7be563ffce5abba35c1d09406 Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Mon, 18 Feb 2019 18:55:17 +0700 Subject: [PATCH] fix mentions and hashtags --- lib/auto_linker/parser.ex | 12 ++++++------ test/auto_linker_test.exs | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/auto_linker/parser.ex b/lib/auto_linker/parser.ex index 14cb785..2e8a97b 100644 --- a/lib/auto_linker/parser.ex +++ b/lib/auto_linker/parser.ex @@ -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 diff --git a/test/auto_linker_test.exs b/test/auto_linker_test.exs index fc6f64d..7ac672e 100644 --- a/test/auto_linker_test.exs +++ b/test/auto_linker_test.exs @@ -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(@#{mention}) + end + + expected = + "Hello again, @@user.<script></script>\nThis is on another :moominmamma: line. #2hu #epic #phantasmagoric" + + assert AutoLinker.link(text, + mention: true, + mention_handler: handler, + hashtag: true, + hashtag_prefix: "/tag/" + ) == expected + end end describe "mentions" do