Remove markdown links support

This commit is contained in:
Egor Kislitsyn 2019-06-21 17:12:11 +07:00
parent 95e8188490
commit f081e6b553
6 changed files with 2 additions and 57 deletions

View file

@ -45,9 +45,6 @@ iex> AutoLinker.link("or at home on 555.555.5555", phone: true)
iex> AutoLinker.link(", work (555) 555-5555", phone: true)
", work <a href=\"#\" class=\"phone-number\" data-phone=\"5555555555\">(555) 555-5555</a>"
iex> AutoLinker.link("[Google Search](http://google.com)", markdown: true)
"<a href='http://google.com' class='auto-linker' target='_blank' rel='noopener noreferrer'>Google Search</a>"
```
See the [Docs](https://hexdocs.pm/auto_linker/) for more examples

View file

@ -15,12 +15,6 @@ defmodule AutoLinker do
iex> AutoLinker.link("google.com", new_window: false, rel: false, class: false)
~s(<a href="http://google.com">google.com</a>)
iex> AutoLinker.link("[Google](http://google.com)", markdown: true, new_window: false, rel: false, class: false)
~s(<a href='http://google.com'>Google</a>)
iex> AutoLinker.link("[Google Search](http://google.com)", markdown: true)
~s(<a href='http://google.com' class="auto-linker" target="_blank" rel="noopener noreferrer">Google Search</a>)
"""
import AutoLinker.Parser
@ -39,7 +33,6 @@ defmodule AutoLinker do
* `exclude_class: false` - Set to a class name when you don't want urls auto linked in the html of the give class
* `exclude_id: false` - Set to an element id when you don't want urls auto linked in the html of the give element
* `exclude_patterns: ["```"]` - Don't link anything between the the pattern
* `markdown: false` - link markdown style links
* `email: false` - link email links
* `mention: false` - link @mentions (when `true`, requires `mention_prefix` or `mention_handler` options to be set)
* `mention_prefix: nil` - a prefix to build a link for a mention (example: `https://example.com/user/`)

View file

@ -17,14 +17,6 @@ defmodule AutoLinker.Builder do
|> format_url(text, opts)
end
def create_markdown_links(text, opts) do
[]
|> build_attrs(text, opts, :rel)
|> build_attrs(text, opts, :target)
|> build_attrs(text, opts, :class)
|> format_markdown(text, opts)
end
defp build_attrs(attrs, uri, %{rel: get_rel}, :rel) when is_function(get_rel, 1) do
case get_rel.(uri) do
nil -> attrs
@ -68,16 +60,6 @@ defmodule AutoLinker.Builder do
|> Enum.join(" ")
end
defp format_markdown(attrs, text, _opts) do
attrs =
case format_attrs(attrs) do
"" -> ""
attrs -> " " <> attrs
end
Regex.replace(~r/\[(.+?)\]\((.+?)\)/, text, "<a href='\\2'#{attrs}>\\1</a>")
end
defp truncate(url, false), do: url
defp truncate(url, len) when len < 3, do: url

View file

@ -111,13 +111,6 @@ defmodule AutoLinker.Parser do
|> do_parse(Map.delete(opts, :extra))
end
defp do_parse({text, user_acc}, %{markdown: true} = opts) do
text
|> Builder.create_markdown_links(opts)
|> (&{&1, user_acc}).()
|> do_parse(Map.delete(opts, :markdown))
end
defp do_parse(input, %{email: true} = opts) do
input
|> do_parse(opts, {"", "", :parsing}, &check_and_link_email/3)

View file

@ -12,32 +12,19 @@ defmodule AutoLinkerTest do
"<a href=\"http://google.com\" class=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\">google.com</a>"
end
test "markdown" do
assert AutoLinker.link("[google.com](http://google.com)", markdown: true) ==
"<a href='http://google.com' class=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\">google.com</a>"
end
test "does on link existing links" do
assert AutoLinker.link("<a href='http://google.com'>google.com</a>") ==
"<a href='http://google.com'>google.com</a>"
end
test "phone number and markdown link" do
assert AutoLinker.link("888 888-8888 [ab](a.com)", phone: true, markdown: true) ==
~s(<a href="#" class="phone-number" data-phone="8888888888">888 888-8888</a>) <>
~s( <a href='a.com' class="auto-linker" target="_blank" rel="noopener noreferrer">ab</a>)
end
test "all kinds of links" do
text =
"hello google.com https://ddg.com 888 888-8888 user@email.com [google.com](http://google.com) irc:///mIRC"
text = "hello google.com https://ddg.com 888 888-8888 user@email.com irc:///mIRC"
expected =
"hello <a href=\"http://google.com\">google.com</a> <a href=\"https://ddg.com\">ddg.com</a> <a href=\"#\" class=\"phone-number\" data-phone=\"8888888888\">888 888-8888</a> <a href=\"mailto:user@email.com\">user@email.com</a> <a href='http://google.com'>google.com</a> <a href=\"irc:///mIRC\">irc:///mIRC</a>"
"hello <a href=\"http://google.com\">google.com</a> <a href=\"https://ddg.com\">ddg.com</a> <a href=\"#\" class=\"phone-number\" data-phone=\"8888888888\">888 888-8888</a> <a href=\"mailto:user@email.com\">user@email.com</a> <a href=\"irc:///mIRC\">irc:///mIRC</a>"
assert AutoLinker.link(text,
phone: true,
markdown: true,
email: true,
scheme: true,
extra: true,

View file

@ -29,13 +29,6 @@ defmodule AutoLinker.BuilderTest do
assert create_link("http://text", %{rel: false, strip_prefix: false}) == expected
end
test "create_markdown_links/2" do
expected =
"<a href='url' class=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\">text</a>"
assert create_markdown_links("[text](url)", %{}) == expected
end
test "format_hashtag/3" do
expected = "<a href=\"/t/girls\">#girls</a>"
assert format_hashtag(%{href: "/t/girls"}, "girls", nil) == expected