Refactor url?/2 to use opts
This commit is contained in:
parent
c8026cd16c
commit
c19c7afa5b
2 changed files with 20 additions and 21 deletions
|
@ -262,7 +262,7 @@ defmodule AutoLinker.Parser do
|
||||||
def check_and_link(buffer, opts, _user_acc) do
|
def check_and_link(buffer, opts, _user_acc) do
|
||||||
str = strip_parens(buffer)
|
str = strip_parens(buffer)
|
||||||
|
|
||||||
if url?(str, opts[:scheme], opts[:validate_tld]) do
|
if url?(str, opts) do
|
||||||
case parse_link(str, opts) do
|
case parse_link(str, opts) do
|
||||||
^buffer -> link_url(buffer, opts)
|
^buffer -> link_url(buffer, opts)
|
||||||
url -> String.replace(buffer, url, link_url(url, opts))
|
url -> String.replace(buffer, url, link_url(url, opts))
|
||||||
|
@ -315,16 +315,15 @@ defmodule AutoLinker.Parser do
|
||||||
end
|
end
|
||||||
|
|
||||||
# @doc false
|
# @doc false
|
||||||
def url?(buffer, scheme, validate_tld \\ true)
|
|
||||||
|
|
||||||
def url?(buffer, true, validate_tld) do
|
def url?(buffer, opts) do
|
||||||
valid_url?(buffer) && Regex.match?(@match_scheme, buffer) &&
|
if opts[:scheme] do
|
||||||
(!validate_tld or validate_tld == :no_scheme || valid_tld?(buffer))
|
valid_url?(buffer) && Regex.match?(@match_scheme, buffer) &&
|
||||||
end
|
(!opts[:validate_tld] or opts[:validate_tld] == :no_scheme || valid_tld?(buffer))
|
||||||
|
else
|
||||||
def url?(buffer, _, validate_tld) do
|
valid_url?(buffer) && Regex.match?(@match_url, buffer) &&
|
||||||
valid_url?(buffer) && Regex.match?(@match_url, buffer) &&
|
(opts[:validate_tld] == false || valid_tld?(buffer))
|
||||||
(validate_tld == false || valid_tld?(buffer))
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def email?(buffer) do
|
def email?(buffer) do
|
||||||
|
|
|
@ -4,74 +4,74 @@ defmodule AutoLinker.ParserTest do
|
||||||
|
|
||||||
import AutoLinker.Parser
|
import AutoLinker.Parser
|
||||||
|
|
||||||
describe "url?/3" do
|
describe "url?/2" do
|
||||||
test "valid scheme true" do
|
test "valid scheme true" do
|
||||||
valid_scheme_urls()
|
valid_scheme_urls()
|
||||||
|> Enum.each(fn url ->
|
|> Enum.each(fn url ->
|
||||||
assert url?(url, true)
|
assert url?(url, scheme: true, validate_tld: true)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "invalid scheme true" do
|
test "invalid scheme true" do
|
||||||
invalid_scheme_urls()
|
invalid_scheme_urls()
|
||||||
|> Enum.each(fn url ->
|
|> Enum.each(fn url ->
|
||||||
refute url?(url, true)
|
refute url?(url, scheme: true, validate_tld: true)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "valid scheme false" do
|
test "valid scheme false" do
|
||||||
valid_non_scheme_urls()
|
valid_non_scheme_urls()
|
||||||
|> Enum.each(fn url ->
|
|> Enum.each(fn url ->
|
||||||
assert url?(url, false)
|
assert url?(url, scheme: false, validate_tld: true)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "invalid scheme false" do
|
test "invalid scheme false" do
|
||||||
invalid_non_scheme_urls()
|
invalid_non_scheme_urls()
|
||||||
|> Enum.each(fn url ->
|
|> Enum.each(fn url ->
|
||||||
refute url?(url, false)
|
refute url?(url, scheme: false, validate_tld: true)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "checks the tld for url with a scheme when validate_tld: true" do
|
test "checks the tld for url with a scheme when validate_tld: true" do
|
||||||
custom_tld_scheme_urls()
|
custom_tld_scheme_urls()
|
||||||
|> Enum.each(fn url ->
|
|> Enum.each(fn url ->
|
||||||
refute url?(url, true, true)
|
refute url?(url, scheme: true, validate_tld: true)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "does not check the tld for url with a scheme when validate_tld: false" do
|
test "does not check the tld for url with a scheme when validate_tld: false" do
|
||||||
custom_tld_scheme_urls()
|
custom_tld_scheme_urls()
|
||||||
|> Enum.each(fn url ->
|
|> Enum.each(fn url ->
|
||||||
assert url?(url, true, false)
|
assert url?(url, scheme: true, validate_tld: false)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "does not check the tld for url with a scheme when validate_tld: :no_scheme" do
|
test "does not check the tld for url with a scheme when validate_tld: :no_scheme" do
|
||||||
custom_tld_scheme_urls()
|
custom_tld_scheme_urls()
|
||||||
|> Enum.each(fn url ->
|
|> Enum.each(fn url ->
|
||||||
assert url?(url, true, :no_scheme)
|
assert url?(url, scheme: true, validate_tld: :no_scheme)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "checks the tld for url without a scheme when validate_tld: true" do
|
test "checks the tld for url without a scheme when validate_tld: true" do
|
||||||
custom_tld_non_scheme_urls()
|
custom_tld_non_scheme_urls()
|
||||||
|> Enum.each(fn url ->
|
|> Enum.each(fn url ->
|
||||||
refute url?(url, false, true)
|
refute url?(url, scheme: false, validate_tld: true)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "checks the tld for url without a scheme when validate_tld: :no_scheme" do
|
test "checks the tld for url without a scheme when validate_tld: :no_scheme" do
|
||||||
custom_tld_non_scheme_urls()
|
custom_tld_non_scheme_urls()
|
||||||
|> Enum.each(fn url ->
|
|> Enum.each(fn url ->
|
||||||
refute url?(url, false, :no_scheme)
|
refute url?(url, scheme: false, validate_tld: :no_scheme)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
test "does not check the tld for url without a scheme when validate_tld: false" do
|
test "does not check the tld for url without a scheme when validate_tld: false" do
|
||||||
custom_tld_non_scheme_urls()
|
custom_tld_non_scheme_urls()
|
||||||
|> Enum.each(fn url ->
|
|> Enum.each(fn url ->
|
||||||
assert url?(url, false, false)
|
assert url?(url, scheme: false, validate_tld: false)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Reference in a new issue