Do not add class
, target
and rel
attributes by default
This commit is contained in:
parent
de6e003b8c
commit
66f5165655
7 changed files with 112 additions and 145 deletions
23
README.md
23
README.md
|
@ -20,24 +20,19 @@ end
|
|||
|
||||
The following examples illustrate some examples on how to use the auto linker.
|
||||
|
||||
```iex
|
||||
```elixir
|
||||
|
||||
iex> Linkify.link("google.com")
|
||||
"<a href='http://google.com' class='linkified' target='_blank' rel='noopener noreferrer'>google.com</a>"
|
||||
"<a href=\"http://google.com\">google.com</a>"
|
||||
|
||||
iex> Linkify.link("google.com", new_window: false, rel: false)
|
||||
"<a href='http://google.com' class='linkified'>google.com</a>"
|
||||
iex> Linkify.link("google.com", class: "linkified")
|
||||
"<a href=\"http://google.com\" class=\"linkified\">google.com</a>"
|
||||
|
||||
iex> Linkify.link("google.com", new_window: false, rel: false, class: false)
|
||||
"<a href='http://google.com'>google.com</a>"
|
||||
iex> Linkify.link("google.com", new_window: true)
|
||||
"<a href=\"http://google.com\" target=\"_blank\">google.com</a>"
|
||||
|
||||
iex> Linkify.link("call me at x9999", phone: true)
|
||||
"call me at <a href=\"#\" class=\"phone-number\" data-phone=\"9999\">x9999</a>"
|
||||
|
||||
iex> Linkify.link("or at home on 555.555.5555", phone: true)
|
||||
"or at home on <a href=\"#\" class=\"phone-number\" data-phone=\"5555555555\">555.555.5555</a>"
|
||||
|
||||
iex> Linkify.link(", work (555) 555-5555", phone: true)
|
||||
", work <a href=\"#\" class=\"phone-number\" data-phone=\"5555555555\">(555) 555-5555</a>"
|
||||
iex> Linkify.link("google.com", new_window: true, rel: "noopener noreferrer")
|
||||
"<a href=\"http://google.com\" target=\"_blank\" rel=\"noopener noreferrer\">google.com</a>"
|
||||
```
|
||||
|
||||
See the [Docs](https://hexdocs.pm/linkify/) for more examples
|
||||
|
|
|
@ -3,52 +3,43 @@ defmodule Linkify do
|
|||
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>"`
|
||||
`Check out "<a href=\"http://google.com\">google.com</a>"`
|
||||
|
||||
## Examples
|
||||
|
||||
iex> Linkify.link("google.com")
|
||||
~s(<a href="http://google.com" class="linkified" target="_blank" rel="noopener noreferrer">google.com</a>)
|
||||
|
||||
iex> Linkify.link("google.com", new_window: false, rel: false)
|
||||
~s(<a href="http://google.com" class="linkified">google.com</a>)
|
||||
|
||||
iex> Linkify.link("google.com", new_window: false, rel: false, class: false)
|
||||
~s(<a href="http://google.com">google.com</a>)
|
||||
|
||||
iex> Linkify.link("google.com", new_window: true, rel: "noopener noreferrer")
|
||||
~s(<a href="http://google.com" target="_blank" rel="noopener noreferrer">google.com</a>)
|
||||
|
||||
iex> Linkify.link("google.com", class: "linkified")
|
||||
~s(<a href="http://google.com" class="linkified">google.com</a>)
|
||||
"""
|
||||
|
||||
import Linkify.Parser
|
||||
|
||||
@doc """
|
||||
Auto link a string.
|
||||
Finds links and turns them into HTML `<a>` tag.
|
||||
|
||||
Options:
|
||||
|
||||
* `class: "linkified"` - specify the class to be added to the generated link. false to clear
|
||||
* `rel: "noopener noreferrer"` - override the rel attribute. false to clear
|
||||
* `new_window: true` - set to false to remove `target='_blank'` attribute
|
||||
* `truncate: false` - Set to a number to truncate urls longer then the number. Truncated urls will end in `..`
|
||||
* `strip_prefix: true` - Strip the scheme prefix
|
||||
* `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
|
||||
* `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/`)
|
||||
* `mention_handler: nil` - a custom handler to validate and formart a mention
|
||||
* `class` - specify the class to be added to the generated link.
|
||||
* `rel` - specify the rel attribute.
|
||||
* `new_window` - set to `true` to add `target="_blank"` attribute
|
||||
* `truncate` - Set to a number to truncate urls longer then the number. Truncated urls will end in `...`
|
||||
* `strip_prefix` - Strip the scheme prefix (default: `false`)
|
||||
* `exclude_class` - Set to a class name when you don't want urls auto linked in the html of the give class (default: `false`)
|
||||
* `exclude_id` - Set to an element id when you don't want urls auto linked in the html of the give element (default: `false`)
|
||||
* `email` - link email links (default: `false`)
|
||||
* `mention` - link @mentions (when `true`, requires `mention_prefix` or `mention_handler` options to be set) (default: `false`)
|
||||
* `mention_prefix` - a prefix to build a link for a mention (example: `https://example.com/user/`, default: `nil`)
|
||||
* `mention_handler` - a custom handler to validate and formart a mention (default: `nil`)
|
||||
* `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.)
|
||||
* `validate_tld: true` - Set to false to disable TLD validation for urls/emails, also can be set to :no_scheme to validate TLDs only for urls without a scheme (e.g `example.com` will be validated, but `http://example.loki` won't)
|
||||
|
||||
Each of the above options can be specified when calling `link(text, opts)`
|
||||
or can be set in the `:linkify`'s configuration. For example:
|
||||
|
||||
config :linkify,
|
||||
class: false,
|
||||
new_window: false
|
||||
|
||||
Note that passing opts to `link/2` will override the configuration settings.
|
||||
"""
|
||||
def link(text, opts \\ []) do
|
||||
parse(text, opts)
|
||||
|
|
|
@ -25,18 +25,18 @@ defmodule Linkify.Builder do
|
|||
end
|
||||
|
||||
defp build_attrs(attrs, _, opts, :rel) do
|
||||
case Map.get(opts, :rel, "noopener noreferrer") do
|
||||
case Map.get(opts, :rel) do
|
||||
rel when is_binary(rel) -> [{:rel, rel} | attrs]
|
||||
_ -> attrs
|
||||
end
|
||||
end
|
||||
|
||||
defp build_attrs(attrs, _, opts, :target) do
|
||||
if Map.get(opts, :new_window, true), do: [{:target, :_blank} | attrs], else: attrs
|
||||
if Map.get(opts, :new_window), do: [{:target, :_blank} | attrs], else: attrs
|
||||
end
|
||||
|
||||
defp build_attrs(attrs, _, opts, :class) do
|
||||
case Map.get(opts, :class, "linkified") do
|
||||
case Map.get(opts, :class) do
|
||||
cls when is_binary(cls) -> [{:class, cls} | attrs]
|
||||
_ -> attrs
|
||||
end
|
||||
|
|
|
@ -51,7 +51,7 @@ defmodule Linkify.Parser do
|
|||
## Examples
|
||||
|
||||
iex> Linkify.Parser.parse("Check out google.com")
|
||||
~s{Check out <a href="http://google.com" class="linkified" target="_blank" rel="noopener noreferrer">google.com</a>}
|
||||
~s{Check out <a href="http://google.com">google.com</a>}
|
||||
"""
|
||||
|
||||
@types [:url, :email, :hashtag, :mention, :extra]
|
||||
|
|
|
@ -5,27 +5,30 @@ defmodule Linkify.BuilderTest do
|
|||
import Linkify.Builder
|
||||
|
||||
test "create_link/2" do
|
||||
expected =
|
||||
"<a href=\"http://text\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">text</a>"
|
||||
expected = "<a href=\"http://text\">text</a>"
|
||||
|
||||
assert create_link("text", %{}) == expected
|
||||
|
||||
expected = "<a href=\"http://text\" class=\"linkified\" target=\"_blank\">text</a>"
|
||||
assert create_link("text", %{rel: nil}) == expected
|
||||
expected = "<a href=\"http://text\" target=\"_blank\">text</a>"
|
||||
|
||||
expected = "<a href=\"http://text\" class=\"linkified\" target=\"_blank\" rel=\"me\">text</a>"
|
||||
assert create_link("text", %{new_window: true}) == expected
|
||||
|
||||
expected = "<a href=\"http://text\" class=\"linkified\">text</a>"
|
||||
assert create_link("text", %{class: "linkified"}) == expected
|
||||
|
||||
expected = "<a href=\"http://text\" rel=\"me\">text</a>"
|
||||
|
||||
assert create_link("text", %{rel: "me"}) == expected
|
||||
|
||||
expected = "<a href=\"http://text\" class=\"linkified\" target=\"_blank\">t...</a>"
|
||||
expected = "<a href=\"http://text\">t...</a>"
|
||||
|
||||
assert create_link("text", %{truncate: 3, rel: false}) == expected
|
||||
assert create_link("text", %{truncate: 3}) == expected
|
||||
|
||||
expected = "<a href=\"http://text\" class=\"linkified\" target=\"_blank\">text</a>"
|
||||
assert create_link("text", %{truncate: 2, rel: false}) == expected
|
||||
expected = "<a href=\"http://text\">text</a>"
|
||||
assert create_link("text", %{truncate: 2}) == expected
|
||||
|
||||
expected = "<a href=\"http://text\" class=\"linkified\" target=\"_blank\">http://text</a>"
|
||||
assert create_link("http://text", %{rel: false, strip_prefix: false}) == expected
|
||||
expected = "<a href=\"http://text\">http://text</a>"
|
||||
assert create_link("http://text", %{strip_prefix: false}) == expected
|
||||
end
|
||||
|
||||
test "format_hashtag/3" do
|
||||
|
@ -46,14 +49,13 @@ defmodule Linkify.BuilderTest do
|
|||
end
|
||||
|
||||
test "create_mention_link/3" do
|
||||
expected =
|
||||
"<a href=\"/u/navi\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">@navi</a>"
|
||||
expected = "<a href=\"/u/navi\">@navi</a>"
|
||||
|
||||
assert create_mention_link("@navi", "hello @navi", %{mention_prefix: "/u/"}) == expected
|
||||
end
|
||||
|
||||
test "create_email_link/3" do
|
||||
expected = "<a href=\"mailto:user@example.org\" class=\"linkified\">user@example.org</a>"
|
||||
expected = "<a href=\"mailto:user@example.org\">user@example.org</a>"
|
||||
assert create_email_link("user@example.org", %{}) == expected
|
||||
assert create_email_link("user@example.org", %{href: "mailto:user@example.org"}) == expected
|
||||
end
|
||||
|
|
|
@ -4,7 +4,7 @@ defmodule LinkifyTest do
|
|||
|
||||
test "default link" do
|
||||
assert Linkify.link("google.com") ==
|
||||
"<a href=\"http://google.com\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">google.com</a>"
|
||||
"<a href=\"http://google.com\">google.com</a>"
|
||||
end
|
||||
|
||||
test "does on link existing links" do
|
||||
|
@ -20,13 +20,20 @@ defmodule LinkifyTest do
|
|||
|
||||
assert Linkify.link(text,
|
||||
email: true,
|
||||
extra: true,
|
||||
class: false,
|
||||
new_window: false,
|
||||
rel: false
|
||||
extra: true
|
||||
) == expected
|
||||
end
|
||||
|
||||
test "class attribute" do
|
||||
assert Linkify.link("google.com", class: "linkified") ==
|
||||
"<a href=\"http://google.com\" class=\"linkified\">google.com</a>"
|
||||
end
|
||||
|
||||
test "rel attribute" do
|
||||
assert Linkify.link("google.com", rel: "noopener noreferrer") ==
|
||||
"<a href=\"http://google.com\" rel=\"noopener noreferrer\">google.com</a>"
|
||||
end
|
||||
|
||||
test "rel as function" do
|
||||
text = "google.com"
|
||||
|
||||
|
@ -36,11 +43,7 @@ defmodule LinkifyTest do
|
|||
url |> String.split(".") |> List.last()
|
||||
end
|
||||
|
||||
assert Linkify.link(text,
|
||||
class: false,
|
||||
new_window: false,
|
||||
rel: custom_rel
|
||||
) == expected
|
||||
assert Linkify.link(text, rel: custom_rel) == expected
|
||||
|
||||
text = "google.com"
|
||||
|
||||
|
@ -48,17 +51,12 @@ defmodule LinkifyTest do
|
|||
|
||||
custom_rel = fn _ -> nil end
|
||||
|
||||
assert Linkify.link(text,
|
||||
class: false,
|
||||
new_window: false,
|
||||
rel: custom_rel
|
||||
) == expected
|
||||
assert Linkify.link(text, rel: custom_rel) == expected
|
||||
end
|
||||
|
||||
test "link_map/2" do
|
||||
assert Linkify.link_map("google.com", []) ==
|
||||
{"<a href=\"http://google.com\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">google.com</a>",
|
||||
[]}
|
||||
{"<a href=\"http://google.com\">google.com</a>", []}
|
||||
end
|
||||
|
||||
describe "custom handlers" do
|
||||
|
@ -100,8 +98,6 @@ defmodule LinkifyTest do
|
|||
hashtag: true,
|
||||
hashtag_handler: handler,
|
||||
hashtag_prefix: "https://example.com/user/",
|
||||
class: false,
|
||||
new_window: false,
|
||||
rel: false
|
||||
)
|
||||
|
||||
|
@ -120,13 +116,14 @@ defmodule LinkifyTest do
|
|||
end
|
||||
|
||||
expected =
|
||||
~s(Hello again, <span class="h-card"><a href="#/user/user">@<span>@user</span></a></span>.<script></script>\nThis is on another :moominmamma: line. <a href="/tag/2hu" class="linkified" target="_blank" rel="noopener noreferrer">#2hu</a> <a href="/tag/epic" class="linkified" target="_blank" rel="noopener noreferrer">#epic</a> <a href="/tag/phantasmagoric" class="linkified" target="_blank" rel="noopener noreferrer">#phantasmagoric</a>)
|
||||
~s(Hello again, <span class="h-card"><a href="#/user/user">@<span>@user</span></a></span>.<script></script>\nThis is on another :moominmamma: line. <a href="/tag/2hu" target="_blank">#2hu</a> <a href="/tag/epic" target="_blank">#epic</a> <a href="/tag/phantasmagoric" target="_blank">#phantasmagoric</a>)
|
||||
|
||||
assert Linkify.link(text,
|
||||
mention: true,
|
||||
mention_handler: handler,
|
||||
hashtag: true,
|
||||
hashtag_prefix: "/tag/"
|
||||
hashtag_prefix: "/tag/",
|
||||
new_window: true
|
||||
) == expected
|
||||
end
|
||||
end
|
||||
|
@ -134,11 +131,12 @@ defmodule LinkifyTest do
|
|||
describe "mentions" do
|
||||
test "simple mentions" do
|
||||
expected =
|
||||
~s{hello <a href="https://example.com/user/user" class="linkified" target="_blank" rel="noopener noreferrer">@user</a> and <a href="https://example.com/user/anotherUser" class="linkified" target="_blank" rel="noopener noreferrer">@anotherUser</a>.}
|
||||
~s{hello <a href="https://example.com/user/user" target="_blank">@user</a> and <a href="https://example.com/user/anotherUser" target="_blank">@anotherUser</a>.}
|
||||
|
||||
assert Linkify.link("hello @user and @anotherUser.",
|
||||
mention: true,
|
||||
mention_prefix: "https://example.com/user/"
|
||||
mention_prefix: "https://example.com/user/",
|
||||
new_window: true
|
||||
) == expected
|
||||
end
|
||||
|
||||
|
@ -149,24 +147,19 @@ defmodule LinkifyTest do
|
|||
expected =
|
||||
"<p><strong>hello world</strong></p>\n<p><`em>another <a href=\"u/user__test\">@user__test</a> and <a href=\"u/user__test\">@user__test</a> <a href=\"http://google.com\">google.com</a> paragraph</em></p>\n"
|
||||
|
||||
assert Linkify.link(text,
|
||||
mention: true,
|
||||
mention_prefix: "u/",
|
||||
class: false,
|
||||
rel: false,
|
||||
new_window: false
|
||||
) == expected
|
||||
assert Linkify.link(text, mention: true, mention_prefix: "u/") == expected
|
||||
end
|
||||
|
||||
test "metion @user@example.com" do
|
||||
text = "hey @user@example.com"
|
||||
|
||||
expected =
|
||||
"hey <a href=\"https://example.com/user/user@example.com\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">@user@example.com</a>"
|
||||
"hey <a href=\"https://example.com/user/user@example.com\" target=\"_blank\">@user@example.com</a>"
|
||||
|
||||
assert Linkify.link(text,
|
||||
mention: true,
|
||||
mention_prefix: "https://example.com/user/"
|
||||
mention_prefix: "https://example.com/user/",
|
||||
new_window: true
|
||||
) == expected
|
||||
end
|
||||
end
|
||||
|
@ -174,11 +167,12 @@ defmodule LinkifyTest do
|
|||
describe "hashtag links" do
|
||||
test "hashtag" do
|
||||
expected =
|
||||
" one <a href=\"https://example.com/tag/2two\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">#2two</a> three <a href=\"https://example.com/tag/four\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">#four</a>."
|
||||
" one <a href=\"https://example.com/tag/2two\" target=\"_blank\">#2two</a> three <a href=\"https://example.com/tag/four\" target=\"_blank\">#four</a>."
|
||||
|
||||
assert Linkify.link(" one #2two three #four.",
|
||||
hashtag: true,
|
||||
hashtag_prefix: "https://example.com/tag/"
|
||||
hashtag_prefix: "https://example.com/tag/",
|
||||
new_window: true
|
||||
) == expected
|
||||
end
|
||||
|
||||
|
@ -188,9 +182,7 @@ defmodule LinkifyTest do
|
|||
assert Linkify.link("#1ok #42 #7",
|
||||
hashtag: true,
|
||||
hashtag_prefix: "/t/",
|
||||
class: false,
|
||||
rel: false,
|
||||
new_window: false
|
||||
rel: false
|
||||
) == expected
|
||||
end
|
||||
|
||||
|
@ -203,9 +195,7 @@ defmodule LinkifyTest do
|
|||
assert Linkify.link(text,
|
||||
hashtag: true,
|
||||
hashtag_prefix: "/t/",
|
||||
class: false,
|
||||
rel: false,
|
||||
new_window: false
|
||||
rel: false
|
||||
) == expected
|
||||
end
|
||||
|
||||
|
@ -218,9 +208,7 @@ defmodule LinkifyTest do
|
|||
assert Linkify.link(text,
|
||||
hashtag: true,
|
||||
hashtag_prefix: "/t/",
|
||||
class: false,
|
||||
rel: false,
|
||||
new_window: false
|
||||
rel: false
|
||||
) == expected
|
||||
end
|
||||
|
||||
|
@ -232,8 +220,6 @@ defmodule LinkifyTest do
|
|||
|
||||
assert Linkify.link(text,
|
||||
hashtag: true,
|
||||
class: false,
|
||||
new_window: false,
|
||||
rel: false,
|
||||
hashtag_prefix: "https://example.com/tag/"
|
||||
) == expected
|
||||
|
@ -246,8 +232,6 @@ defmodule LinkifyTest do
|
|||
"<a href=\"https://example.com/tag/漢字\">#漢字</a> <a href=\"https://example.com/tag/は\">#は</a> <a href=\"https://example.com/tag/тест\">#тест</a> <a href=\"https://example.com/tag/ทดสอบ\">#ทดสอบ</a>"
|
||||
|
||||
assert Linkify.link(text,
|
||||
class: false,
|
||||
new_window: false,
|
||||
rel: false,
|
||||
hashtag: true,
|
||||
hashtag_prefix: "https://example.com/tag/"
|
||||
|
@ -260,73 +244,71 @@ defmodule LinkifyTest do
|
|||
text = "Hey, check out http://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla ."
|
||||
|
||||
expected =
|
||||
"Hey, check out <a href=\"http://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla</a> ."
|
||||
"Hey, check out <a href=\"http://www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla\" target=\"_blank\">youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla</a> ."
|
||||
|
||||
assert Linkify.link(text) == expected
|
||||
assert Linkify.link(text, new_window: true) == expected
|
||||
|
||||
# no scheme
|
||||
text = "Hey, check out www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla ."
|
||||
assert Linkify.link(text) == expected
|
||||
assert Linkify.link(text, new_window: true) == expected
|
||||
end
|
||||
|
||||
test "turn urls with schema into urls" do
|
||||
text = "📌https://google.com"
|
||||
expected = "📌<a href=\"https://google.com\">google.com</a>"
|
||||
|
||||
assert Linkify.link(text, class: false, new_window: false, rel: false) == expected
|
||||
assert Linkify.link(text, rel: false) == expected
|
||||
end
|
||||
|
||||
test "hostname/@user" do
|
||||
text = "https://example.com/@user"
|
||||
|
||||
expected =
|
||||
"<a href=\"https://example.com/@user\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">example.com/@user</a>"
|
||||
expected = "<a href=\"https://example.com/@user\" target=\"_blank\">example.com/@user</a>"
|
||||
|
||||
assert Linkify.link(text) == expected
|
||||
assert Linkify.link(text, new_window: true) == expected
|
||||
|
||||
text = "https://example.com:4000/@user"
|
||||
|
||||
expected =
|
||||
"<a href=\"https://example.com:4000/@user\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">example.com:4000/@user</a>"
|
||||
"<a href=\"https://example.com:4000/@user\" target=\"_blank\">example.com:4000/@user</a>"
|
||||
|
||||
assert Linkify.link(text) == expected
|
||||
assert Linkify.link(text, new_window: true) == expected
|
||||
|
||||
text = "https://example.com:4000/@user"
|
||||
|
||||
expected =
|
||||
"<a href=\"https://example.com:4000/@user\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">example.com:4000/@user</a>"
|
||||
"<a href=\"https://example.com:4000/@user\" target=\"_blank\">example.com:4000/@user</a>"
|
||||
|
||||
assert Linkify.link(text) == expected
|
||||
assert Linkify.link(text, new_window: true) == expected
|
||||
|
||||
text = "@username"
|
||||
expected = "@username"
|
||||
assert Linkify.link(text) == expected
|
||||
assert Linkify.link(text, new_window: true) == expected
|
||||
|
||||
text = "http://www.cs.vu.nl/~ast/intel/"
|
||||
|
||||
expected =
|
||||
"<a href=\"http://www.cs.vu.nl/~ast/intel/\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">cs.vu.nl/~ast/intel/</a>"
|
||||
expected = "<a href=\"http://www.cs.vu.nl/~ast/intel/\">cs.vu.nl/~ast/intel/</a>"
|
||||
|
||||
assert Linkify.link(text) == expected
|
||||
|
||||
text = "https://forum.zdoom.org/viewtopic.php?f=44&t=57087"
|
||||
|
||||
expected =
|
||||
"<a href=\"https://forum.zdoom.org/viewtopic.php?f=44&t=57087\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">forum.zdoom.org/viewtopic.php?f=44&t=57087</a>"
|
||||
"<a href=\"https://forum.zdoom.org/viewtopic.php?f=44&t=57087\">forum.zdoom.org/viewtopic.php?f=44&t=57087</a>"
|
||||
|
||||
assert Linkify.link(text) == expected
|
||||
|
||||
text = "https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul"
|
||||
|
||||
expected =
|
||||
"<a href=\"https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul</a>"
|
||||
"<a href=\"https://en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul\">en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul</a>"
|
||||
|
||||
assert Linkify.link(text) == expected
|
||||
|
||||
text = "https://en.wikipedia.org/wiki/Duff's_device"
|
||||
|
||||
expected =
|
||||
"<a href=\"https://en.wikipedia.org/wiki/Duff's_device\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">en.wikipedia.org/wiki/Duff's_device</a>"
|
||||
"<a href=\"https://en.wikipedia.org/wiki/Duff's_device\">en.wikipedia.org/wiki/Duff's_device</a>"
|
||||
|
||||
assert Linkify.link(text) == expected
|
||||
end
|
||||
|
@ -336,14 +318,14 @@ defmodule LinkifyTest do
|
|||
test "xmpp" do
|
||||
text = "xmpp:user@example.com"
|
||||
|
||||
expected = "<a href=\"xmpp:user@example.com\" class=\"linkified\">xmpp:user@example.com</a>"
|
||||
expected = "<a href=\"xmpp:user@example.com\">xmpp:user@example.com</a>"
|
||||
|
||||
assert Linkify.link(text, extra: true, new_window: false, rel: false) == expected
|
||||
assert Linkify.link(text, extra: true) == expected
|
||||
end
|
||||
|
||||
test "email" do
|
||||
text = "user@example.com"
|
||||
expected = "<a href=\"mailto:user@example.com\" class=\"linkified\">user@example.com</a>"
|
||||
expected = "<a href=\"mailto:user@example.com\">user@example.com</a>"
|
||||
assert Linkify.link(text, email: true) == expected
|
||||
end
|
||||
|
||||
|
@ -352,9 +334,9 @@ defmodule LinkifyTest do
|
|||
"magnet:?xt=urn:btih:a4104a9d2f5615601c429fe8bab8177c47c05c84&dn=ubuntu-18.04.1.0-live-server-amd64.iso&tr=http%3A%2F%2Ftorrent.ubuntu.com%3A6969%2Fannounce&tr=http%3A%2F%2Fipv6.torrent.ubuntu.com%3A6969%2Fannounce"
|
||||
|
||||
expected =
|
||||
"<a href=\"magnet:?xt=urn:btih:a4104a9d2f5615601c429fe8bab8177c47c05c84&dn=ubuntu-18.04.1.0-live-server-amd64.iso&tr=http%3A%2F%2Ftorrent.ubuntu.com%3A6969%2Fannounce&tr=http%3A%2F%2Fipv6.torrent.ubuntu.com%3A6969%2Fannounce\" class=\"linkified\">magnet:?xt=urn:btih:a4104a9d2f5615601c429fe8bab8177c47c05c84&dn=ubuntu-18.04.1.0-live-server-amd64.iso&tr=http%3A%2F%2Ftorrent.ubuntu.com%3A6969%2Fannounce&tr=http%3A%2F%2Fipv6.torrent.ubuntu.com%3A6969%2Fannounce</a>"
|
||||
"<a href=\"magnet:?xt=urn:btih:a4104a9d2f5615601c429fe8bab8177c47c05c84&dn=ubuntu-18.04.1.0-live-server-amd64.iso&tr=http%3A%2F%2Ftorrent.ubuntu.com%3A6969%2Fannounce&tr=http%3A%2F%2Fipv6.torrent.ubuntu.com%3A6969%2Fannounce\">magnet:?xt=urn:btih:a4104a9d2f5615601c429fe8bab8177c47c05c84&dn=ubuntu-18.04.1.0-live-server-amd64.iso&tr=http%3A%2F%2Ftorrent.ubuntu.com%3A6969%2Fannounce&tr=http%3A%2F%2Fipv6.torrent.ubuntu.com%3A6969%2Fannounce</a>"
|
||||
|
||||
assert Linkify.link(text, extra: true, new_window: false, rel: false) == expected
|
||||
assert Linkify.link(text, extra: true) == expected
|
||||
end
|
||||
|
||||
test "dweb" do
|
||||
|
@ -362,9 +344,9 @@ defmodule LinkifyTest do
|
|||
"dweb://584faa05d394190ab1a3f0240607f9bf2b7e2bd9968830a11cf77db0cea36a21+v1.0.0/path/to/file.txt"
|
||||
|
||||
expected =
|
||||
"<a href=\"dweb://584faa05d394190ab1a3f0240607f9bf2b7e2bd9968830a11cf77db0cea36a21+v1.0.0/path/to/file.txt\" class=\"linkified\">dweb://584faa05d394190ab1a3f0240607f9bf2b7e2bd9968830a11cf77db0cea36a21+v1.0.0/path/to/file.txt</a>"
|
||||
"<a href=\"dweb://584faa05d394190ab1a3f0240607f9bf2b7e2bd9968830a11cf77db0cea36a21+v1.0.0/path/to/file.txt\">dweb://584faa05d394190ab1a3f0240607f9bf2b7e2bd9968830a11cf77db0cea36a21+v1.0.0/path/to/file.txt</a>"
|
||||
|
||||
assert Linkify.link(text, extra: true, new_window: false, rel: false) == expected
|
||||
assert Linkify.link(text, extra: true) == expected
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -372,8 +354,7 @@ defmodule LinkifyTest do
|
|||
test "parse with scheme" do
|
||||
text = "https://google.com"
|
||||
|
||||
expected =
|
||||
"<a href=\"https://google.com\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">google.com</a>"
|
||||
expected = "<a href=\"https://google.com\">google.com</a>"
|
||||
|
||||
assert Linkify.link(text) == expected
|
||||
end
|
||||
|
@ -387,7 +368,7 @@ defmodule LinkifyTest do
|
|||
text = "this url https://google.foobar.com/ has valid TLD"
|
||||
|
||||
expected =
|
||||
"this url <a href=\"https://google.foobar.com/\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">google.foobar.com/</a> has valid TLD"
|
||||
"this url <a href=\"https://google.foobar.com/\">google.foobar.com/</a> has valid TLD"
|
||||
|
||||
assert Linkify.link(text) == expected
|
||||
end
|
||||
|
@ -399,7 +380,7 @@ defmodule LinkifyTest do
|
|||
text = "this url google.foobar.com/ has valid TLD"
|
||||
|
||||
expected =
|
||||
"this url <a href=\"http://google.foobar.com/\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">google.foobar.com/</a> has valid TLD"
|
||||
"this url <a href=\"http://google.foobar.com/\">google.foobar.com/</a> has valid TLD"
|
||||
|
||||
assert Linkify.link(text) == expected
|
||||
end
|
||||
|
@ -408,14 +389,14 @@ defmodule LinkifyTest do
|
|||
text = "this url http://google.foobar.com/ has valid TLD"
|
||||
|
||||
expected =
|
||||
"this url <a href=\"http://google.foobar.com/\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">google.foobar.com/</a> has valid TLD"
|
||||
"this url <a href=\"http://google.foobar.com/\">google.foobar.com/</a> has valid TLD"
|
||||
|
||||
assert Linkify.link(text) == expected
|
||||
|
||||
text = "this url google.foobar.com/ has valid TLD"
|
||||
|
||||
expected =
|
||||
"this url <a href=\"http://google.foobar.com/\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">google.foobar.com/</a> has valid TLD"
|
||||
"this url <a href=\"http://google.foobar.com/\">google.foobar.com/</a> has valid TLD"
|
||||
|
||||
assert Linkify.link(text) == expected
|
||||
end
|
||||
|
|
|
@ -109,9 +109,7 @@ defmodule Linkify.ParserTest do
|
|||
describe "parse" do
|
||||
test "handle line breakes" do
|
||||
text = "google.com\r\nssss"
|
||||
|
||||
expected =
|
||||
"<a href=\"http://google.com\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">google.com</a>\r\nssss"
|
||||
expected = "<a href=\"http://google.com\">google.com</a>\r\nssss"
|
||||
|
||||
assert parse(text) == expected
|
||||
end
|
||||
|
@ -141,20 +139,20 @@ defmodule Linkify.ParserTest do
|
|||
|
||||
expected = "<div><a href=\"http://google.com\">google.com</a></div>"
|
||||
|
||||
assert parse(text, class: false, rel: false, new_window: false) == expected
|
||||
assert parse(text, class: false, rel: false) == expected
|
||||
|
||||
text = "Check out <div class='section'>google.com</div>"
|
||||
|
||||
expected =
|
||||
"Check out <div class='section'><a href=\"http://google.com\">google.com</a></div>"
|
||||
|
||||
assert parse(text, class: false, rel: false, new_window: false) == expected
|
||||
assert parse(text, class: false, rel: false) == expected
|
||||
end
|
||||
|
||||
test "links url inside nested html" do
|
||||
text = "<p><strong>google.com</strong></p>"
|
||||
expected = "<p><strong><a href=\"http://google.com\">google.com</a></strong></p>"
|
||||
assert parse(text, class: false, rel: false, new_window: false) == expected
|
||||
assert parse(text, class: false, rel: false) == expected
|
||||
end
|
||||
|
||||
test "do not link parens" do
|
||||
|
@ -163,14 +161,14 @@ defmodule Linkify.ParserTest do
|
|||
expected =
|
||||
" foo (<a href=\"https://example.com/path/folder/\">example.com/path/folder/</a>), bar"
|
||||
|
||||
assert parse(text, class: false, rel: false, new_window: false, scheme: true) == expected
|
||||
assert parse(text, class: false, rel: false, scheme: true) == expected
|
||||
|
||||
text = " foo (example.com/path/folder/), bar"
|
||||
|
||||
expected =
|
||||
" foo (<a href=\"http://example.com/path/folder/\">example.com/path/folder/</a>), bar"
|
||||
|
||||
assert parse(text, class: false, rel: false, new_window: false) == expected
|
||||
assert parse(text, class: false, rel: false) == expected
|
||||
end
|
||||
|
||||
test "do not link urls" do
|
||||
|
|
Reference in a new issue