commit: 2232e45a0cb1692c874cac4da14e8e4e963923ad
parent: d8a4ea56a9a5f3aa593f3c6a971884434a42e547
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Wed, 27 Feb 2019 04:39:46 +0100
Move extra and prefixes
Diffstat:
2 files changed, 11 insertions(+), 42 deletions(-)
diff --git a/lib/auto_linker.ex b/lib/auto_linker.ex
@@ -47,7 +47,8 @@ defmodule AutoLinker do
* `hashtag: false` - link #hashtags (when `true`, requires `hashtag_prefix` or `hashtag_handler` options to be set)
* `hashtag_prefix: nil` - a prefix to build a link for a hashtag (example: `https://example.com/tag/`)
* `hashtag_handler: nil` - a custom handler to validate and formart a hashtag
- * `extra: false` - link urls with rarely used schemes (magnet, ipfs, irc, etc.)
+ * `extra: false` - ignored, moved to detection if `extra_prefixes` is a non-empty list
+ * `extra_prefixes: ["magnet:?", …]` - list of prefixes that aren't http(s)://, mailto: or xmpp:
Each of the above options can be specified when calling `link(text, opts)`
or can be set in the `:auto_linker`'s configuration. For example:
diff --git a/lib/auto_linker/parser.ex b/lib/auto_linker/parser.ex
@@ -25,9 +25,7 @@ defmodule AutoLinker.Parser do
~s{, work <a href="#" class="phone-number" data-phone="5555555555">(555) 555-5555</a>}
"""
- @invalid_url ~r/(\.\.+)|(^(\d+\.){1,2}\d+$)/
-
- @match_scheme ~r{^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w\.-]+)+[\w\-\._~%:/?#[\]@!\$&'\(\)\*\+,;=.]+$}
+ @valid_url ~r/[0-9a-z+\-\.]+:[0-9a-z$-_.+!*'(),]+/ui
@match_phone ~r"((?:x\d{2,7})|(?:(?:\+?1\s?(?:[.-]\s?)?)?(?:\(\s?(?:[2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s?\)|(?:[2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s?(?:[.-]\s?)?)(?:[2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s?(?:[.-]\s?)?(?:[0-9]{4}))"
@@ -44,20 +42,6 @@ defmodule AutoLinker.Parser do
@match_hashtag ~r/^(?<tag>\#[[:word:]_]*[[:alpha:]_·][[:word:]_·]*)/u
- @prefix_extra [
- "magnet:?",
- "dweb://",
- "dat://",
- "gopher://",
- "ipfs://",
- "ipns://",
- "irc://",
- "ircs://",
- "irc6://",
- "mumble://",
- "ssb://"
- ]
-
@default_opts ~w(url)a
def parse(input, opts \\ %{})
@@ -101,12 +85,6 @@ defmodule AutoLinker.Parser do
|> do_parse(Map.delete(opts, :hashtag))
end
- defp do_parse(input, %{extra: true} = opts) do
- input
- |> do_parse(opts, {"", "", :parsing}, &check_and_link_extra/3)
- |> do_parse(Map.delete(opts, :extra))
- end
-
defp do_parse({text, user_acc}, %{markdown: true} = opts) do
text
|> Builder.create_markdown_links(opts)
@@ -262,8 +240,10 @@ defmodule AutoLinker.Parser do
end
def check_and_link(buffer, opts, _user_acc) do
+ prefixes = ["http://", "https://"] ++ (opts[:extra_prefixes] || [])
+
buffer
- |> is_url?()
+ |> is_prefixed_url?(prefixes)
|> link_url(buffer, opts)
end
@@ -285,23 +265,17 @@ defmodule AutoLinker.Parser do
|> link_hashtag(buffer, opts, user_acc)
end
- def check_and_link_extra(buffer, opts, _user_acc) do
- buffer
- |> String.starts_with?(@prefix_extra)
- |> link_extra(buffer, opts)
- end
-
# @doc false
def is_url?(buffer, _) do
is_url(buffer)
end
def is_url?(buffer) do
- if Regex.match?(@invalid_url, buffer) do
- false
- else
- @match_scheme |> Regex.match?(buffer)
- end
+ Regex.match?(@valid_url, buffer)
+ end
+
+ def is_prefixed_url?(buffer, prefixes) do
+ is_url?(buffer) and String.starts_with?(buffer, prefixes)
end
def is_email?(buffer) do
@@ -394,12 +368,6 @@ defmodule AutoLinker.Parser do
def link_email(_, buffer, _opts), do: buffer
- def link_extra(true, buffer, opts) do
- Builder.create_extra_link(buffer, opts)
- end
-
- def link_extra(_, buffer, _opts), do: buffer
-
defp run_handler(handler, buffer, opts, user_acc) do
case handler.(buffer, opts, user_acc) do
{buffer, user_acc} -> {buffer, user_acc}