auto linking working for simple url with no surounding text
This commit is contained in:
parent
dd3d135611
commit
a84a17d01e
2 changed files with 31 additions and 12 deletions
|
@ -1,27 +1,43 @@
|
|||
defmodule AutoLinker do
|
||||
@moduledoc """
|
||||
Documentation for AutoLinker.
|
||||
Create url links from text containing urls.
|
||||
|
||||
Turns an input string like `"Check out google.com"` into
|
||||
`Check out `"<a href='http://google.com' target='_blank' rel='noopener noreferrer'>google.com</a>"`
|
||||
|
||||
## Examples
|
||||
|
||||
iex> AutoLinker.link("google.com")
|
||||
"<a href='http://google.com' class='auto-linker' target='_blank' rel='noopener noreferrer'>google.com</a>"
|
||||
|
||||
iex> AutoLinker.link("google.com", new_window: false, rel: false)
|
||||
"<a href='http://google.com' class='auto-linker'>google.com</a>"
|
||||
|
||||
iex> AutoLinker.link("google.com", new_window: false, rel: false, class: false)
|
||||
"<a href='http://google.com'>google.com</a>"
|
||||
"""
|
||||
|
||||
@doc """
|
||||
Auto link a string.
|
||||
"""
|
||||
def link(text, opts \\ []) do
|
||||
opts =
|
||||
:url_linker
|
||||
:auto_linker
|
||||
|> Application.get_all_env()
|
||||
|> Keyword.merge(opts)
|
||||
|
||||
# rel = opts[:rel] || "noopener noreferrer"
|
||||
# new_window if opts[:target]
|
||||
parse text, opts
|
||||
end
|
||||
|
||||
# state = {buffer, acc, state}
|
||||
defp parse(text, opts) do
|
||||
parse(text, Keyword.get(opts, :scheme, false), {"", "", false})
|
||||
parse(text, Keyword.get(opts, :scheme, false), opts, {"", "", false})
|
||||
end
|
||||
|
||||
defp parse("", _scheme, opts ,{_, acc, _}), do: acc
|
||||
defp parse("", _scheme, _opts ,{_, acc, _}), do: acc
|
||||
|
||||
defp parse(text, scheme, opts, {buffer, acc, state}) do
|
||||
acc <> create_link(text, opts)
|
||||
acc = acc <> create_link(text, opts)
|
||||
parse("", scheme, opts, {buffer, acc, state})
|
||||
end
|
||||
|
||||
|
@ -29,8 +45,9 @@ defmodule AutoLinker do
|
|||
[]
|
||||
|> build_attrs(url, opts, :rel)
|
||||
|> build_attrs(url, opts, :target)
|
||||
|> build_attrs(url, opts, :class)
|
||||
|> build_attrs(url, opts, :scheme)
|
||||
|> build_url(url, opts)
|
||||
|> format_url(url, opts)
|
||||
end
|
||||
|
||||
defp build_attrs(attrs, _, opts, :rel) do
|
||||
|
@ -41,7 +58,11 @@ defmodule AutoLinker do
|
|||
if Keyword.get(opts, :new_window, true),
|
||||
do: [{:target, :_blank} | attrs], else: attrs
|
||||
end
|
||||
defp build_attrs(attrs, url, opts, :scheme) do
|
||||
defp build_attrs(attrs, _, opts, :class) do
|
||||
if cls = Keyword.get(opts, :class, "auto-linker"),
|
||||
do: [{:class, cls} | attrs], else: attrs
|
||||
end
|
||||
defp build_attrs(attrs, url, _opts, :scheme) do
|
||||
if String.starts_with?(url, ["http://", "https://"]),
|
||||
do: [{:href, url} | attrs], else: [{:href, "http://" <> url} | attrs]
|
||||
end
|
||||
|
|
|
@ -2,7 +2,5 @@ defmodule AutoLinkerTest do
|
|||
use ExUnit.Case
|
||||
doctest AutoLinker
|
||||
|
||||
test "the truth" do
|
||||
assert 1 + 1 == 2
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Reference in a new issue