add custom handlers for mentions and hashtags
This commit is contained in:
parent
479acfb82d
commit
4da0c84ba1
3 changed files with 169 additions and 92 deletions
|
@ -51,6 +51,10 @@ defmodule AutoLinker do
|
||||||
Note that passing opts to `link/2` will override the configuration settings.
|
Note that passing opts to `link/2` will override the configuration settings.
|
||||||
"""
|
"""
|
||||||
def link(text, opts \\ []) do
|
def link(text, opts \\ []) do
|
||||||
parse(text, opts)
|
parse({text, nil}, opts) |> elem(0)
|
||||||
|
end
|
||||||
|
|
||||||
|
def link_and_parse(text, parsed, opts \\ []) do
|
||||||
|
parse({text, parsed}, opts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -95,31 +95,32 @@ defmodule AutoLinker.Parser do
|
||||||
|
|
||||||
defp do_parse(text, %{phone: _} = opts) do
|
defp do_parse(text, %{phone: _} = opts) do
|
||||||
text
|
text
|
||||||
|> do_parse(false, opts, {"", "", :parsing}, &check_and_link_phone/3)
|
|> do_parse(false, opts, {"", "", :parsing}, &check_and_link_phone/4)
|
||||||
|> do_parse(Map.delete(opts, :phone))
|
|> do_parse(Map.delete(opts, :phone))
|
||||||
end
|
end
|
||||||
|
|
||||||
defp do_parse(text, %{mention: true} = opts) do
|
defp do_parse(text, %{mention: true} = opts) do
|
||||||
text
|
text
|
||||||
|> do_parse(false, opts, {"", "", :parsing}, &check_and_link_mention/3)
|
|> do_parse(false, opts, {"", "", :parsing}, &check_and_link_mention/4)
|
||||||
|> do_parse(Map.delete(opts, :mention))
|
|> do_parse(Map.delete(opts, :mention))
|
||||||
end
|
end
|
||||||
|
|
||||||
defp do_parse(text, %{extra: true} = opts) do
|
defp do_parse(text, %{extra: true} = opts) do
|
||||||
text
|
text
|
||||||
|> do_parse(false, opts, {"", "", :parsing}, &check_and_link_extra/3)
|
|> do_parse(false, opts, {"", "", :parsing}, &check_and_link_extra/4)
|
||||||
|> do_parse(Map.delete(opts, :extra))
|
|> do_parse(Map.delete(opts, :extra))
|
||||||
end
|
end
|
||||||
|
|
||||||
defp do_parse(text, %{markdown: true} = opts) do
|
defp do_parse({text, out}, %{markdown: true} = opts) do
|
||||||
text
|
text
|
||||||
|> Builder.create_markdown_links(opts)
|
|> Builder.create_markdown_links(opts)
|
||||||
|
|> (&{&1, out}).()
|
||||||
|> do_parse(Map.delete(opts, :markdown))
|
|> do_parse(Map.delete(opts, :markdown))
|
||||||
end
|
end
|
||||||
|
|
||||||
defp do_parse(text, %{email: true} = opts) do
|
defp do_parse(text, %{email: true} = opts) do
|
||||||
text
|
text
|
||||||
|> do_parse(false, opts, {"", "", :parsing}, &check_and_link_email/3)
|
|> do_parse(false, opts, {"", "", :parsing}, &check_and_link_email/4)
|
||||||
|> do_parse(Map.delete(opts, :email))
|
|> do_parse(Map.delete(opts, :email))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -127,137 +128,148 @@ defmodule AutoLinker.Parser do
|
||||||
if (exclude = Map.get(opts, :exclude_pattern, false)) && String.starts_with?(text, exclude) do
|
if (exclude = Map.get(opts, :exclude_pattern, false)) && String.starts_with?(text, exclude) do
|
||||||
text
|
text
|
||||||
else
|
else
|
||||||
do_parse(text, Map.get(opts, :scheme, false), opts, {"", "", :parsing}, &check_and_link/3)
|
do_parse(text, Map.get(opts, :scheme, false), opts, {"", "", :parsing}, &check_and_link/4)
|
||||||
end
|
end
|
||||||
|> do_parse(Map.delete(opts, :url))
|
|> do_parse(Map.delete(opts, :url))
|
||||||
end
|
end
|
||||||
|
|
||||||
defp do_parse(text, %{hashtag: true} = opts) do
|
defp do_parse(text, %{hashtag: true} = opts) do
|
||||||
text
|
text
|
||||||
|> do_parse(false, opts, {"", "", :parsing}, &check_and_link_hashtag/3)
|
|> do_parse(false, opts, {"", "", :parsing}, &check_and_link_hashtag/4)
|
||||||
|> do_parse(Map.delete(opts, :hashtag))
|
|> do_parse(Map.delete(opts, :hashtag))
|
||||||
end
|
end
|
||||||
|
|
||||||
defp do_parse(text, _), do: text
|
defp do_parse(text, _), do: text
|
||||||
|
|
||||||
defp do_parse("", _scheme, _opts, {"", acc, _}, _handler),
|
defp do_parse({"", out}, _scheme, _opts, {"", acc, _}, _handler),
|
||||||
do: acc
|
do: {acc, out}
|
||||||
|
|
||||||
defp do_parse("", scheme, opts, {buffer, acc, _}, handler),
|
defp do_parse({"", out}, scheme, opts, {buffer, acc, _}, handler) do
|
||||||
do: acc <> handler.(buffer, scheme, opts)
|
{buffer, out} = run_handler(handler, buffer, scheme, opts, out)
|
||||||
|
{acc <> buffer, out}
|
||||||
defp do_parse("<a" <> text, scheme, opts, {buffer, acc, :parsing}, handler),
|
|
||||||
do: do_parse(text, scheme, opts, {"", acc <> buffer <> "<a", :skip}, handler)
|
|
||||||
|
|
||||||
defp do_parse("</a>" <> text, scheme, opts, {buffer, acc, :skip}, handler),
|
|
||||||
do: do_parse(text, scheme, opts, {"", acc <> buffer <> "</a>", :parsing}, handler)
|
|
||||||
|
|
||||||
defp do_parse("<" <> text, scheme, opts, {"", acc, :parsing}, handler),
|
|
||||||
do: do_parse(text, scheme, opts, {"<", acc, {:open, 1}}, handler)
|
|
||||||
|
|
||||||
defp do_parse(">" <> text, scheme, opts, {buffer, acc, {:attrs, level}}, handler),
|
|
||||||
do: do_parse(text, scheme, opts, {"", acc <> buffer <> ">", {:html, level}}, handler)
|
|
||||||
|
|
||||||
defp do_parse(<<ch::8>> <> text, scheme, opts, {"", acc, {:attrs, level}}, handler) do
|
|
||||||
do_parse(text, scheme, opts, {"", acc <> <<ch::8>>, {:attrs, level}}, handler)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
defp do_parse("</" <> text, scheme, opts, {buffer, acc, {:html, level}}, handler),
|
defp do_parse({"<a" <> text, out}, scheme, opts, {buffer, acc, :parsing}, handler),
|
||||||
do:
|
do: do_parse({text, out}, scheme, opts, {"", acc <> buffer <> "<a", :skip}, handler)
|
||||||
|
|
||||||
|
defp do_parse({"</a>" <> text, out}, scheme, opts, {buffer, acc, :skip}, handler),
|
||||||
|
do: do_parse({text, out}, scheme, opts, {"", acc <> buffer <> "</a>", :parsing}, handler)
|
||||||
|
|
||||||
|
defp do_parse({"<" <> text, out}, scheme, opts, {"", acc, :parsing}, handler),
|
||||||
|
do: do_parse({text, out}, scheme, opts, {"<", acc, {:open, 1}}, handler)
|
||||||
|
|
||||||
|
defp do_parse({">" <> text, out}, scheme, opts, {buffer, acc, {:attrs, level}}, handler),
|
||||||
|
do: do_parse({text, out}, scheme, opts, {"", acc <> buffer <> ">", {:html, level}}, handler)
|
||||||
|
|
||||||
|
defp do_parse({<<ch::8>> <> text, out}, scheme, opts, {"", acc, {:attrs, level}}, handler) do
|
||||||
|
do_parse({text, out}, scheme, opts, {"", acc <> <<ch::8>>, {:attrs, level}}, handler)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp do_parse({"</" <> text, out}, scheme, opts, {buffer, acc, {:html, level}}, handler) do
|
||||||
|
{buffer, out} = run_handler(handler, buffer, scheme, opts, out)
|
||||||
|
|
||||||
do_parse(
|
do_parse(
|
||||||
text,
|
{text, out},
|
||||||
scheme,
|
scheme,
|
||||||
opts,
|
opts,
|
||||||
{"", acc <> handler.(buffer, scheme, opts) <> "</", {:close, level}},
|
{"", acc <> buffer <> "</", {:close, level}},
|
||||||
handler
|
handler
|
||||||
)
|
)
|
||||||
|
end
|
||||||
|
|
||||||
defp do_parse(">" <> text, scheme, opts, {buffer, acc, {:close, 1}}, handler),
|
defp do_parse({">" <> text, out}, scheme, opts, {buffer, acc, {:close, 1}}, handler),
|
||||||
do: do_parse(text, scheme, opts, {"", acc <> buffer <> ">", :parsing}, handler)
|
do: do_parse({text, out}, scheme, opts, {"", acc <> buffer <> ">", :parsing}, handler)
|
||||||
|
|
||||||
defp do_parse(">" <> text, scheme, opts, {buffer, acc, {:close, level}}, handler),
|
defp do_parse({">" <> text, out}, scheme, opts, {buffer, acc, {:close, level}}, handler),
|
||||||
do: do_parse(text, scheme, opts, {"", acc <> buffer <> ">", {:html, level - 1}}, handler)
|
do:
|
||||||
|
do_parse({text, out}, scheme, opts, {"", acc <> buffer <> ">", {:html, level - 1}}, handler)
|
||||||
|
|
||||||
defp do_parse(" " <> text, scheme, opts, {buffer, acc, {:open, level}}, handler),
|
defp do_parse({" " <> text, out}, scheme, opts, {buffer, acc, {:open, level}}, handler),
|
||||||
do: do_parse(text, scheme, opts, {"", acc <> buffer <> " ", {:attrs, level}}, handler)
|
do: do_parse({text, out}, scheme, opts, {"", acc <> buffer <> " ", {:attrs, level}}, handler)
|
||||||
|
|
||||||
defp do_parse("\n" <> text, scheme, opts, {buffer, acc, {:open, level}}, handler),
|
defp do_parse({"\n" <> text, out}, scheme, opts, {buffer, acc, {:open, level}}, handler),
|
||||||
do: do_parse(text, scheme, opts, {"", acc <> buffer <> "\n", {:attrs, level}}, handler)
|
do: do_parse({text, out}, scheme, opts, {"", acc <> buffer <> "\n", {:attrs, level}}, handler)
|
||||||
|
|
||||||
# default cases where state is not important
|
# default cases where state is not important
|
||||||
defp do_parse(" " <> text, scheme, %{phone: _} = opts, {buffer, acc, state}, handler),
|
defp do_parse({" " <> text, out}, scheme, %{phone: _} = opts, {buffer, acc, state}, handler),
|
||||||
do: do_parse(text, scheme, opts, {buffer <> " ", acc, state}, handler)
|
do: do_parse({text, out}, scheme, opts, {buffer <> " ", acc, state}, handler)
|
||||||
|
|
||||||
|
defp do_parse({" " <> text, out}, scheme, opts, {buffer, acc, state}, handler) do
|
||||||
|
{buffer, out} = run_handler(handler, buffer, scheme, opts, out)
|
||||||
|
|
||||||
defp do_parse(" " <> text, scheme, opts, {buffer, acc, state}, handler),
|
|
||||||
do:
|
|
||||||
do_parse(
|
do_parse(
|
||||||
text,
|
{text, out},
|
||||||
scheme,
|
scheme,
|
||||||
opts,
|
opts,
|
||||||
{"", acc <> handler.(buffer, scheme, opts) <> " ", state},
|
{"", acc <> buffer <> " ", state},
|
||||||
handler
|
handler
|
||||||
)
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp do_parse({"\n" <> text, out}, scheme, opts, {buffer, acc, state}, handler) do
|
||||||
|
{buffer, out} = run_handler(handler, buffer, scheme, opts, out)
|
||||||
|
|
||||||
defp do_parse("\n" <> text, scheme, opts, {buffer, acc, state}, handler),
|
|
||||||
do:
|
|
||||||
do_parse(
|
do_parse(
|
||||||
text,
|
{text, out},
|
||||||
scheme,
|
scheme,
|
||||||
opts,
|
opts,
|
||||||
{"", acc <> handler.(buffer, scheme, opts) <> "\n", state},
|
{"", acc <> buffer <> "\n", state},
|
||||||
handler
|
handler
|
||||||
)
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp do_parse({<<ch::8>>, out}, scheme, opts, {buffer, acc, state}, handler) do
|
||||||
|
{buffer, out} = run_handler(handler, buffer <> <<ch::8>>, scheme, opts, out)
|
||||||
|
|
||||||
defp do_parse(<<ch::8>>, scheme, opts, {buffer, acc, state}, handler),
|
|
||||||
do:
|
|
||||||
do_parse(
|
do_parse(
|
||||||
"",
|
{"", out},
|
||||||
scheme,
|
scheme,
|
||||||
opts,
|
opts,
|
||||||
{"", acc <> handler.(buffer <> <<ch::8>>, scheme, opts), state},
|
{"", acc <> buffer, state},
|
||||||
handler
|
handler
|
||||||
)
|
)
|
||||||
|
end
|
||||||
|
|
||||||
defp do_parse(<<ch::8>> <> text, scheme, opts, {buffer, acc, state}, handler),
|
defp do_parse({<<ch::8>> <> text, out}, scheme, opts, {buffer, acc, state}, handler),
|
||||||
do: do_parse(text, scheme, opts, {buffer <> <<ch::8>>, acc, state}, handler)
|
do: do_parse({text, out}, scheme, opts, {buffer <> <<ch::8>>, acc, state}, handler)
|
||||||
|
|
||||||
def check_and_link(buffer, scheme, opts) do
|
def check_and_link(buffer, scheme, opts, _out) do
|
||||||
buffer
|
buffer
|
||||||
|> is_url?(scheme)
|
|> is_url?(scheme)
|
||||||
|> link_url(buffer, opts)
|
|> link_url(buffer, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_and_link_email(buffer, _, opts) do
|
def check_and_link_email(buffer, _, opts, _out) do
|
||||||
buffer
|
buffer
|
||||||
|> is_email?
|
|> is_email?
|
||||||
|> link_email(buffer, opts)
|
|> link_email(buffer, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_and_link_phone(buffer, _, opts) do
|
def check_and_link_phone(buffer, _, opts, _out) do
|
||||||
buffer
|
buffer
|
||||||
|> match_phone
|
|> match_phone
|
||||||
|> link_phone(buffer, opts)
|
|> link_phone(buffer, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_and_link_mention(buffer, _, opts) do
|
def check_and_link_mention(buffer, _, opts, out) do
|
||||||
buffer
|
buffer
|
||||||
|> match_mention
|
|> match_mention
|
||||||
|> link_mention(buffer, opts)
|
|> link_mention(buffer, opts, out)
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_and_link_hashtag(buffer, _, opts) do
|
def check_and_link_hashtag(buffer, _, opts, out) do
|
||||||
buffer
|
buffer
|
||||||
|> match_hashtag
|
|> match_hashtag
|
||||||
|> link_hashtag(buffer, opts)
|
|> link_hashtag(buffer, opts, out)
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_and_link_extra("xmpp:" <> handle, _, opts) do
|
def check_and_link_extra("xmpp:" <> handle, _, opts, _out) do
|
||||||
handle
|
handle
|
||||||
|> is_email?
|
|> is_email?
|
||||||
|> link_extra("xmpp:" <> handle, opts)
|
|> link_extra("xmpp:" <> handle, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_and_link_extra(buffer, _, opts) do
|
def check_and_link_extra(buffer, _, opts, _out) do
|
||||||
buffer
|
buffer
|
||||||
|> String.starts_with?(@prefix_extra)
|
|> String.starts_with?(@prefix_extra)
|
||||||
|> link_extra(buffer, opts)
|
|> link_extra(buffer, opts)
|
||||||
|
@ -328,20 +340,23 @@ defmodule AutoLinker.Parser do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def link_hashtag(nil, buffer, _), do: buffer
|
def link_hashtag(nil, buffer, _, _out), do: buffer
|
||||||
|
|
||||||
def link_hashtag(hashtag, buffer, opts) do
|
def link_hashtag(hashtag, buffer, %{hashtag_handler: hashtag_handler} = opts, out) do
|
||||||
|
hashtag_handler.(hashtag, buffer, opts, out)
|
||||||
|
end
|
||||||
|
|
||||||
|
def link_hashtag(hashtag, buffer, opts, _out) do
|
||||||
Builder.create_hashtag_link(hashtag, buffer, opts)
|
Builder.create_hashtag_link(hashtag, buffer, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def link_mention(nil, buffer, _), do: buffer
|
def link_mention(nil, buffer, _, out), do: {buffer, out}
|
||||||
|
|
||||||
def link_mention(mention, _buffer, %{mention_formatter: mention_formatter} = opts) do
|
def link_mention(mention, buffer, %{mention_handler: mention_handler} = opts, out) do
|
||||||
{buffer, _} = mention_formatter.(mention, opts)
|
mention_handler.(mention, buffer, opts, out)
|
||||||
buffer
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def link_mention(mention, buffer, opts) do
|
def link_mention(mention, buffer, opts, _out) do
|
||||||
Builder.create_mention_link(mention, buffer, opts)
|
Builder.create_mention_link(mention, buffer, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -370,4 +385,11 @@ defmodule AutoLinker.Parser do
|
||||||
end
|
end
|
||||||
|
|
||||||
def link_extra(_, buffer, _opts), do: buffer
|
def link_extra(_, buffer, _opts), do: buffer
|
||||||
|
|
||||||
|
defp run_handler(handler, buffer, scheme, opts, out) do
|
||||||
|
case handler.(buffer, scheme, opts, out) do
|
||||||
|
{buffer, out} -> {buffer, out}
|
||||||
|
buffer -> {buffer, out}
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -51,6 +51,57 @@ defmodule AutoLinkerTest do
|
||||||
) == expected
|
) == expected
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "custom handlers" do
|
||||||
|
test "mentions handler" do
|
||||||
|
text = "hello @user, @valid_user and @invalid_user"
|
||||||
|
valid_users = ["user", "valid_user"]
|
||||||
|
|
||||||
|
handler = fn "@" <> user = mention, buffer, _opts, parsed ->
|
||||||
|
if Enum.member?(valid_users, user) do
|
||||||
|
link = ~s(<a href="https://example.com/user/#{user}" data-user="#{user}">#{mention}</a>)
|
||||||
|
{link, %{parsed | mentions: MapSet.put(parsed.mentions, {mention, user})}}
|
||||||
|
else
|
||||||
|
{buffer, parsed}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
{result_text, %{mentions: mentions}} =
|
||||||
|
AutoLinker.link_and_parse(text, %{mentions: MapSet.new()},
|
||||||
|
mention: true,
|
||||||
|
mention_handler: handler
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result_text ==
|
||||||
|
"hello <a href=\"https://example.com/user/user\" data-user=\"user\">@user</a> <a href=\"https://example.com/user/valid_user\" data-user=\"valid_user\">@valid_user</a> and @invalid_user"
|
||||||
|
|
||||||
|
assert mentions |> MapSet.to_list() |> Enum.map(&elem(&1, 1)) == valid_users
|
||||||
|
end
|
||||||
|
|
||||||
|
test "hashtags handler" do
|
||||||
|
text = "#hello #world"
|
||||||
|
|
||||||
|
handler = fn hashtag, buffer, opts, parsed ->
|
||||||
|
link = AutoLinker.Builder.create_hashtag_link(hashtag, buffer, opts)
|
||||||
|
{link, %{parsed | tags: MapSet.put(parsed.tags, hashtag)}}
|
||||||
|
end
|
||||||
|
|
||||||
|
{result_text, %{tags: tags}} =
|
||||||
|
AutoLinker.link_and_parse(text, %{tags: MapSet.new()},
|
||||||
|
hashtag: true,
|
||||||
|
hashtag_handler: handler,
|
||||||
|
hashtag_prefix: "https://example.com/user/",
|
||||||
|
class: false,
|
||||||
|
new_window: false,
|
||||||
|
rel: false
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result_text ==
|
||||||
|
"<a href='https://example.com/user/hello'>#hello</a> <a href='https://example.com/user/world'>#world</a>"
|
||||||
|
|
||||||
|
assert MapSet.to_list(tags) == ["hello", "world"]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "mentions" do
|
describe "mentions" do
|
||||||
test "simple mentions" do
|
test "simple mentions" do
|
||||||
expected =
|
expected =
|
||||||
|
|
Reference in a new issue