logo

auto_linker

AutoLinker-shim, based on https://git.pleroma.social/pleroma/auto_linker git clone https://hacktivis.me/git/auto_linker.git
commit: 8f7496b1d3c291b7be563ffce5abba35c1d09406
parent 33c6754592063c94b4e8598675f1f2d46fc9a49e
Author: Egor Kislitsyn <egor@kislitsyn.com>
Date:   Mon, 18 Feb 2019 18:55:17 +0700

fix mentions and hashtags

Diffstat:

Mlib/auto_linker/parser.ex12++++++------
Mtest/auto_linker_test.exs19+++++++++++++++++++
2 files changed, 25 insertions(+), 6 deletions(-)

diff --git 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 @@ -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.&lt;script&gt;&lt;/script&gt;\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