Browse Source

Rename to Linkify

merge-requests/18/head
Egor Kislitsyn 4 years ago
parent
commit
2690f2b680
8 changed files with 119 additions and 131 deletions
  1. +14
    -21
      README.md
  2. +10
    -10
      lib/linkify.ex
  3. +2
    -2
      lib/linkify/builder.ex
  4. +4
    -6
      lib/linkify/parser.ex
  5. +8
    -9
      mix.exs
  6. +11
    -12
      test/builder_test.exs
  7. +66
    -67
      test/linkify_test.exs
  8. +4
    -4
      test/parser_test.exs

+ 14
- 21
README.md View File

@ -1,13 +1,6 @@
# AutoLinker
# Linkify
[![Build Status](https://travis-ci.org/smpallen99/auto_linker.png?branch=master)](https://travis-ci.org/smpallen99/auto_linker) [![Hex Version][hex-img]][hex] [![License][license-img]][license]
[hex-img]: https://img.shields.io/hexpm/v/auto_linker.svg
[hex]: https://hex.pm/packages/auto_linker
[license-img]: http://img.shields.io/badge/license-MIT-brightgreen.svg
[license]: http://opensource.org/licenses/MIT
AutoLinker is a basic package for turning website names, and phone numbers into links.
Linkify is a basic package for turning website names, and phone numbers into links.
Use this package in your web view to convert web references into click-able links.
@ -15,11 +8,11 @@ This is a very early version. Some of the described options are not yet function
## Installation
The package can be installed by adding `auto_linker` to your list of dependencies in `mix.exs`:
The package can be installed by adding `linkify` to your list of dependencies in `mix.exs`:
```elixir
def deps do
[{:auto_linker, "~> 0.2"}]
[{:linkify, "~> 0.1"}]
end
```
@ -28,26 +21,26 @@ end
The following examples illustrate some examples on how to use the auto linker.
```iex
iex> AutoLinker.link("google.com")
"<a href='http://google.com' class='auto-linker' target='_blank' rel='noopener noreferrer'>google.com</a>"
iex> Linkify.link("google.com")
"<a href='http://google.com' class='linkified' 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> Linkify.link("google.com", new_window: false, rel: false)
"<a href='http://google.com' class='linkified'>google.com</a>"
iex> AutoLinker.link("google.com", new_window: false, rel: false, class: false)
iex> Linkify.link("google.com", new_window: false, rel: false, class: false)
"<a href='http://google.com'>google.com</a>"
iex> AutoLinker.link("call me at x9999", phone: true)
iex> Linkify.link("call me at x9999", phone: true)
"call me at <a href=\"#\" class=\"phone-number\" data-phone=\"9999\">x9999</a>"
iex> AutoLinker.link("or at home on 555.555.5555", phone: true)
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> AutoLinker.link(", work (555) 555-5555", phone: true)
iex> Linkify.link(", work (555) 555-5555", phone: true)
", work <a href=\"#\" class=\"phone-number\" data-phone=\"5555555555\">(555) 555-5555</a>"
```
See the [Docs](https://hexdocs.pm/auto_linker/) for more examples
See the [Docs](https://hexdocs.pm/linkify/) for more examples
## Configuration
@ -55,7 +48,7 @@ By default, link parsing is enabled and phone parsing is disabled.
```elixir
# enable phone parsing, and disable link parsing
config :auto_linker, opts: [phone: true, url: false]
config :linkify, opts: [phone: true, url: false]
```


lib/auto_linker.ex → lib/linkify.ex View File

@ -1,4 +1,4 @@
defmodule AutoLinker do
defmodule Linkify do
@moduledoc """
Create url links from text containing urls.
@ -7,24 +7,24 @@ defmodule AutoLinker do
## Examples
iex> AutoLinker.link("google.com")
~s(<a href="http://google.com" class="auto-linker" target="_blank" rel="noopener noreferrer">google.com</a>)
iex> Linkify.link("google.com")
~s(<a href="http://google.com" class="linkified" target="_blank" rel="noopener noreferrer">google.com</a>)
iex> AutoLinker.link("google.com", new_window: false, rel: false)
~s(<a href="http://google.com" class="auto-linker">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> AutoLinker.link("google.com", new_window: false, rel: false, class: false)
iex> Linkify.link("google.com", new_window: false, rel: false, class: false)
~s(<a href="http://google.com">google.com</a>)
"""
import AutoLinker.Parser
import Linkify.Parser
@doc """
Auto link a string.
Options:
* `class: "auto-linker"` - specify the class to be added to the generated link. false to clear
* `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 `..`
@ -42,9 +42,9 @@ defmodule AutoLinker do
* `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 `:auto_linker`'s configuration. For example:
or can be set in the `:linkify`'s configuration. For example:
config :auto_linker,
config :linkify,
class: false,
new_window: false

lib/auto_linker/builder.ex → lib/linkify/builder.ex View File

@ -1,4 +1,4 @@
defmodule AutoLinker.Builder do
defmodule Linkify.Builder do
@moduledoc """
Module for building the auto generated link.
"""
@ -36,7 +36,7 @@ defmodule AutoLinker.Builder do
end
defp build_attrs(attrs, _, opts, :class) do
case Map.get(opts, :class, "auto-linker") do
case Map.get(opts, :class, "linkified") do
cls when is_binary(cls) -> [{:class, cls} | attrs]
_ -> attrs
end

lib/auto_linker/parser.ex → lib/linkify/parser.ex View File

@ -1,9 +1,9 @@
defmodule AutoLinker.Parser do
defmodule Linkify.Parser do
@moduledoc """
Module to handle parsing the the input string.
"""
alias AutoLinker.Builder
alias Linkify.Builder
@invalid_url ~r/(\.\.+)|(^(\d+\.){1,2}\d+$)/
@ -50,8 +50,8 @@ defmodule AutoLinker.Parser do
## Examples
iex> AutoLinker.Parser.parse("Check out google.com")
~s{Check out <a href="http://google.com" class="auto-linker" target="_blank" rel="noopener noreferrer">google.com</a>}
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>}
"""
@types [:url, :email, :hashtag, :mention, :extra]
@ -214,8 +214,6 @@ defmodule AutoLinker.Parser do
defp strip_parens(buffer), do: buffer
# @doc false
def url?(buffer, opts) do
valid_url?(buffer) && Regex.match?(@match_url, buffer) && valid_tld?(buffer, opts)
end

+ 8
- 9
mix.exs View File

@ -1,21 +1,21 @@
defmodule AutoLinker.Mixfile do
defmodule Linkify.Mixfile do
use Mix.Project
@version "0.2.2"
@version "0.1.0"
def project do
[
app: :auto_linker,
app: :linkify,
version: @version,
elixir: "~> 1.4",
elixir: "~> 1.7",
build_embedded: Mix.env() == :prod,
start_permanent: Mix.env() == :prod,
deps: deps(),
docs: [extras: ["README.md"]],
package: package(),
name: "AutoLinker",
name: "Linkify",
description: """
AutoLinker is a basic package for turning website names into links.
Linkify is a basic package for turning website names into links.
"""
]
end
@ -37,10 +37,9 @@ defmodule AutoLinker.Mixfile do
defp package do
[
maintainers: ["Stephen Pallen"],
licenses: ["MIT"],
links: %{"Github" => "https://github.com/smpallen99/auto_linker"},
files: ~w(lib README.md mix.exs LICENSE)
links: %{"GitLab" => "https://git.pleroma.social/pleroma/linkify"},
files: ~w(lib priv README.md mix.exs LICENSE)
]
end
end

+ 11
- 12
test/builder_test.exs View File

@ -1,31 +1,30 @@
defmodule AutoLinker.BuilderTest do
defmodule Linkify.BuilderTest do
use ExUnit.Case, async: true
doctest AutoLinker.Builder
doctest Linkify.Builder
import AutoLinker.Builder
import Linkify.Builder
test "create_link/2" do
expected =
"<a href=\"http://text\" class=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\">text</a>"
"<a href=\"http://text\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">text</a>"
assert create_link("text", %{}) == expected
expected = "<a href=\"http://text\" class=\"auto-linker\" target=\"_blank\">text</a>"
expected = "<a href=\"http://text\" class=\"linkified\" target=\"_blank\">text</a>"
assert create_link("text", %{rel: nil}) == expected
expected =
"<a href=\"http://text\" class=\"auto-linker\" target=\"_blank\" rel=\"me\">text</a>"
expected = "<a href=\"http://text\" class=\"linkified\" target=\"_blank\" rel=\"me\">text</a>"
assert create_link("text", %{rel: "me"}) == expected
expected = "<a href=\"http://text\" class=\"auto-linker\" target=\"_blank\">t...</a>"
expected = "<a href=\"http://text\" class=\"linkified\" target=\"_blank\">t...</a>"
assert create_link("text", %{truncate: 3, rel: false}) == expected
expected = "<a href=\"http://text\" class=\"auto-linker\" target=\"_blank\">text</a>"
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\" class=\"auto-linker\" target=\"_blank\">http://text</a>"
expected = "<a href=\"http://text\" class=\"linkified\" target=\"_blank\">http://text</a>"
assert create_link("http://text", %{rel: false, strip_prefix: false}) == expected
end
@ -48,13 +47,13 @@ defmodule AutoLinker.BuilderTest do
test "create_mention_link/3" do
expected =
"<a href=\"/u/navi\" class=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\">@navi</a>"
"<a href=\"/u/navi\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">@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=\"auto-linker\">user@example.org</a>"
expected = "<a href=\"mailto:user@example.org\" class=\"linkified\">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


test/auto_linker_test.exs → test/linkify_test.exs View File

@ -1,15 +1,15 @@
defmodule AutoLinkerTest do
defmodule LinkifyTest do
use ExUnit.Case, async: true
doctest AutoLinker
doctest Linkify
test "default link" do
assert AutoLinker.link("google.com") ==
"<a href=\"http://google.com\" class=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\">google.com</a>"
assert Linkify.link("google.com") ==
"<a href=\"http://google.com\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">google.com</a>"
end
test "does on link existing links" do
text = ~s(<a href="http://google.com">google.com</a>)
assert AutoLinker.link(text) == text
assert Linkify.link(text) == text
end
test "all kinds of links" do
@ -18,7 +18,7 @@ defmodule AutoLinkerTest do
expected =
"hello <a href=\"http://google.com\">google.com</a> <a href=\"https://ddg.com\">ddg.com</a> <a href=\"mailto:user@email.com\">user@email.com</a> <a href=\"irc:///mIRC\">irc:///mIRC</a>"
assert AutoLinker.link(text,
assert Linkify.link(text,
email: true,
extra: true,
class: false,
@ -36,7 +36,7 @@ defmodule AutoLinkerTest do
url |> String.split(".") |> List.last()
end
assert AutoLinker.link(text,
assert Linkify.link(text,
class: false,
new_window: false,
rel: custom_rel
@ -48,7 +48,7 @@ defmodule AutoLinkerTest do
custom_rel = fn _ -> nil end
assert AutoLinker.link(text,
assert Linkify.link(text,
class: false,
new_window: false,
rel: custom_rel
@ -56,8 +56,8 @@ defmodule AutoLinkerTest do
end
test "link_map/2" do
assert AutoLinker.link_map("google.com", []) ==
{"<a href=\"http://google.com\" class=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\">google.com</a>",
assert Linkify.link_map("google.com", []) ==
{"<a href=\"http://google.com\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">google.com</a>",
[]}
end
@ -76,7 +76,7 @@ defmodule AutoLinkerTest do
end
{result_text, %{mentions: mentions}} =
AutoLinker.link_map(text, %{mentions: MapSet.new()},
Linkify.link_map(text, %{mentions: MapSet.new()},
mention: true,
mention_handler: handler
)
@ -91,12 +91,12 @@ defmodule AutoLinkerTest do
text = "#hello #world"
handler = fn hashtag, buffer, opts, acc ->
link = AutoLinker.Builder.create_hashtag_link(hashtag, buffer, opts)
link = Linkify.Builder.create_hashtag_link(hashtag, buffer, opts)
{link, %{acc | tags: MapSet.put(acc.tags, hashtag)}}
end
{result_text, %{tags: tags}} =
AutoLinker.link_map(text, %{tags: MapSet.new()},
Linkify.link_map(text, %{tags: MapSet.new()},
hashtag: true,
hashtag_handler: handler,
hashtag_prefix: "https://example.com/user/",
@ -120,9 +120,9 @@ defmodule AutoLinkerTest do
end
expected =
~s(Hello again, <span class="h-card"><a href="#/user/user">@<span>@user</span></a></span>.&lt;script&gt;&lt;/script&gt;\nThis is on another :moominmamma: line. <a href="/tag/2hu" class="auto-linker" target="_blank" rel="noopener noreferrer">#2hu</a> <a href="/tag/epic" class="auto-linker" target="_blank" rel="noopener noreferrer">#epic</a> <a href="/tag/phantasmagoric" class="auto-linker" target="_blank" rel="noopener noreferrer">#phantasmagoric</a>)
~s(Hello again, <span class="h-card"><a href="#/user/user">@<span>@user</span></a></span>.&lt;script&gt;&lt;/script&gt;\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>)
assert AutoLinker.link(text,
assert Linkify.link(text,
mention: true,
mention_handler: handler,
hashtag: true,
@ -134,9 +134,9 @@ defmodule AutoLinkerTest do
describe "mentions" do
test "simple mentions" do
expected =
~s{hello <a href="https://example.com/user/user" class="auto-linker" target="_blank" rel="noopener noreferrer">@user</a> and <a href="https://example.com/user/anotherUser" class="auto-linker" target="_blank" rel="noopener noreferrer">@anotherUser</a>.}
~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>.}
assert AutoLinker.link("hello @user and @anotherUser.",
assert Linkify.link("hello @user and @anotherUser.",
mention: true,
mention_prefix: "https://example.com/user/"
) == expected
@ -149,7 +149,7 @@ defmodule AutoLinkerTest 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 AutoLinker.link(text,
assert Linkify.link(text,
mention: true,
mention_prefix: "u/",
class: false,
@ -162,9 +162,9 @@ defmodule AutoLinkerTest do
text = "hey @user@example.com"
expected =
"hey <a href=\"https://example.com/user/user@example.com\" class=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\">@user@example.com</a>"
"hey <a href=\"https://example.com/user/user@example.com\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">@user@example.com</a>"
assert AutoLinker.link(text,
assert Linkify.link(text,
mention: true,
mention_prefix: "https://example.com/user/"
) == expected
@ -174,9 +174,9 @@ defmodule AutoLinkerTest do
describe "hashtag links" do
test "hashtag" do
expected =
" one <a href=\"https://example.com/tag/2two\" class=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\">#2two</a> three <a href=\"https://example.com/tag/four\" class=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\">#four</a>."
" 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>."
assert AutoLinker.link(" one #2two three #four.",
assert Linkify.link(" one #2two three #four.",
hashtag: true,
hashtag_prefix: "https://example.com/tag/"
) == expected
@ -185,7 +185,7 @@ defmodule AutoLinkerTest do
test "must have non-numbers" do
expected = "<a href=\"/t/1ok\">#1ok</a> #42 #7"
assert AutoLinker.link("#1ok #42 #7",
assert Linkify.link("#1ok #42 #7",
hashtag: true,
hashtag_prefix: "/t/",
class: false,
@ -200,7 +200,7 @@ defmodule AutoLinkerTest do
expected =
"<a href=\"/t/administrateur·rice·s\">#administrateur·rice·s</a> <a href=\"/t/ingénieur·e·s\">#ingénieur·e·s</a>"
assert AutoLinker.link(text,
assert Linkify.link(text,
hashtag: true,
hashtag_prefix: "/t/",
class: false,
@ -215,7 +215,7 @@ defmodule AutoLinkerTest do
expected =
"<a href=\"/t/చక్రం\">#చక్రం</a> <a href=\"/t/కకకకక్\">#కకకకక్</a> <a href=\"/t/కకకకాక\">#కకకకాక</a> <a href=\"/t/కకకక్రకకకక\">#కకకక్రకకకక</a>"
assert AutoLinker.link(text,
assert Linkify.link(text,
hashtag: true,
hashtag_prefix: "/t/",
class: false,
@ -230,7 +230,7 @@ defmodule AutoLinkerTest do
expected =
"<a href=\"http://google.com#test\">google.com#test</a> <a href=\"https://example.com/tag/test\">#test</a> <a href=\"http://google.com/#test\">google.com/#test</a> <a href=\"https://example.com/tag/tag\">#tag</a>"
assert AutoLinker.link(text,
assert Linkify.link(text,
hashtag: true,
class: false,
new_window: false,
@ -245,7 +245,7 @@ defmodule AutoLinkerTest do
expected =
"<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 AutoLinker.link(text,
assert Linkify.link(text,
class: false,
new_window: false,
rel: false,
@ -260,75 +260,75 @@ defmodule AutoLinkerTest 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=\"auto-linker\" 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\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla</a> ."
assert AutoLinker.link(text) == expected
assert Linkify.link(text) == expected
# no scheme
text = "Hey, check out www.youtube.com/watch?v=8Zg1-TufF%20zY?x=1&y=2#blabla ."
assert AutoLinker.link(text) == expected
assert Linkify.link(text) == expected
end
test "turn urls with schema into urls" do
text = "📌https://google.com"
expected = "📌<a href=\"https://google.com\">google.com</a>"
assert AutoLinker.link(text, class: false, new_window: false, rel: false) == expected
assert Linkify.link(text, class: false, new_window: false, rel: false) == expected
end
test "hostname/@user" do
text = "https://example.com/@user"
expected =
"<a href=\"https://example.com/@user\" class=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\">example.com/@user</a>"
"<a href=\"https://example.com/@user\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">example.com/@user</a>"
assert AutoLinker.link(text) == expected
assert Linkify.link(text) == expected
text = "https://example.com:4000/@user"
expected =
"<a href=\"https://example.com:4000/@user\" class=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\">example.com:4000/@user</a>"
"<a href=\"https://example.com:4000/@user\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">example.com:4000/@user</a>"
assert AutoLinker.link(text) == expected
assert Linkify.link(text) == expected
text = "https://example.com:4000/@user"
expected =
"<a href=\"https://example.com:4000/@user\" class=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\">example.com:4000/@user</a>"
"<a href=\"https://example.com:4000/@user\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">example.com:4000/@user</a>"
assert AutoLinker.link(text) == expected
assert Linkify.link(text) == expected
text = "@username"
expected = "@username"
assert AutoLinker.link(text) == expected
assert Linkify.link(text) == expected
text = "http://www.cs.vu.nl/~ast/intel/"
expected =
"<a href=\"http://www.cs.vu.nl/~ast/intel/\" class=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\">cs.vu.nl/~ast/intel/</a>"
"<a href=\"http://www.cs.vu.nl/~ast/intel/\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">cs.vu.nl/~ast/intel/</a>"
assert AutoLinker.link(text) == expected
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=\"auto-linker\" 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\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">forum.zdoom.org/viewtopic.php?f=44&t=57087</a>"
assert AutoLinker.link(text) == expected
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=\"auto-linker\" 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\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">en.wikipedia.org/wiki/Sophia_(Gnosticism)#Mythos_of_the_soul</a>"
assert AutoLinker.link(text) == expected
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=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\">en.wikipedia.org/wiki/Duff's_device</a>"
"<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>"
assert AutoLinker.link(text) == expected
assert Linkify.link(text) == expected
end
end
@ -336,16 +336,15 @@ defmodule AutoLinkerTest do
test "xmpp" do
text = "xmpp:user@example.com"
expected =
"<a href=\"xmpp:user@example.com\" class=\"auto-linker\">xmpp:user@example.com</a>"
expected = "<a href=\"xmpp:user@example.com\" class=\"linkified\">xmpp:user@example.com</a>"
assert AutoLinker.link(text, extra: true, new_window: false, rel: false) == expected
assert Linkify.link(text, extra: true, new_window: false, rel: false) == expected
end
test "email" do
text = "user@example.com"
expected = "<a href=\"mailto:user@example.com\" class=\"auto-linker\">user@example.com</a>"
assert AutoLinker.link(text, email: true) == expected
expected = "<a href=\"mailto:user@example.com\" class=\"linkified\">user@example.com</a>"
assert Linkify.link(text, email: true) == expected
end
test "magnet" do
@ -353,9 +352,9 @@ defmodule AutoLinkerTest 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=\"auto-linker\">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\" 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>"
assert AutoLinker.link(text, extra: true, new_window: false, rel: false) == expected
assert Linkify.link(text, extra: true, new_window: false, rel: false) == expected
end
test "dweb" do
@ -363,9 +362,9 @@ defmodule AutoLinkerTest do
"dweb://584faa05d394190ab1a3f0240607f9bf2b7e2bd9968830a11cf77db0cea36a21+v1.0.0/path/to/file.txt"
expected =
"<a href=\"dweb://584faa05d394190ab1a3f0240607f9bf2b7e2bd9968830a11cf77db0cea36a21+v1.0.0/path/to/file.txt\" class=\"auto-linker\">dweb://584faa05d394190ab1a3f0240607f9bf2b7e2bd9968830a11cf77db0cea36a21+v1.0.0/path/to/file.txt</a>"
"<a href=\"dweb://584faa05d394190ab1a3f0240607f9bf2b7e2bd9968830a11cf77db0cea36a21+v1.0.0/path/to/file.txt\" class=\"linkified\">dweb://584faa05d394190ab1a3f0240607f9bf2b7e2bd9968830a11cf77db0cea36a21+v1.0.0/path/to/file.txt</a>"
assert AutoLinker.link(text, extra: true, new_window: false, rel: false) == expected
assert Linkify.link(text, extra: true, new_window: false, rel: false) == expected
end
end
@ -374,51 +373,51 @@ defmodule AutoLinkerTest do
text = "https://google.com"
expected =
"<a href=\"https://google.com\" class=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\">google.com</a>"
"<a href=\"https://google.com\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">google.com</a>"
assert AutoLinker.link(text) == expected
assert Linkify.link(text) == expected
end
test "only existing TLDs with scheme" do
text = "this url https://google.foobar.blah11blah/ has invalid TLD"
expected = "this url https://google.foobar.blah11blah/ has invalid TLD"
assert AutoLinker.link(text) == expected
assert Linkify.link(text) == expected
text = "this url https://google.foobar.com/ has valid TLD"
expected =
"this url <a href=\"https://google.foobar.com/\" class=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\">google.foobar.com/</a> has valid TLD"
"this url <a href=\"https://google.foobar.com/\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">google.foobar.com/</a> has valid TLD"
assert AutoLinker.link(text) == expected
assert Linkify.link(text) == expected
end
test "only existing TLDs without scheme" do
text = "this url google.foobar.blah11blah/ has invalid TLD"
assert AutoLinker.link(text) == text
assert Linkify.link(text) == text
text = "this url google.foobar.com/ has valid TLD"
expected =
"this url <a href=\"http://google.foobar.com/\" class=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\">google.foobar.com/</a> has valid TLD"
"this url <a href=\"http://google.foobar.com/\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">google.foobar.com/</a> has valid TLD"
assert AutoLinker.link(text) == expected
assert Linkify.link(text) == expected
end
test "only existing TLDs with and without scheme" do
text = "this url http://google.foobar.com/ has valid TLD"
expected =
"this url <a href=\"http://google.foobar.com/\" class=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\">google.foobar.com/</a> has valid TLD"
"this url <a href=\"http://google.foobar.com/\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">google.foobar.com/</a> has valid TLD"
assert AutoLinker.link(text) == expected
assert Linkify.link(text) == expected
text = "this url google.foobar.com/ has valid TLD"
expected =
"this url <a href=\"http://google.foobar.com/\" class=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\">google.foobar.com/</a> has valid TLD"
"this url <a href=\"http://google.foobar.com/\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">google.foobar.com/</a> has valid TLD"
assert AutoLinker.link(text) == expected
assert Linkify.link(text) == expected
end
end
end

+ 4
- 4
test/parser_test.exs View File

@ -1,8 +1,8 @@
defmodule AutoLinker.ParserTest do
defmodule Linkify.ParserTest do
use ExUnit.Case, async: true
doctest AutoLinker.Parser
doctest Linkify.Parser
import AutoLinker.Parser
import Linkify.Parser
describe "url?/2" do
test "valid scheme true" do
@ -111,7 +111,7 @@ defmodule AutoLinker.ParserTest do
text = "google.com\r\nssss"
expected =
"<a href=\"http://google.com\" class=\"auto-linker\" target=\"_blank\" rel=\"noopener noreferrer\">google.com</a>\r\nssss"
"<a href=\"http://google.com\" class=\"linkified\" target=\"_blank\" rel=\"noopener noreferrer\">google.com</a>\r\nssss"
assert parse(text) == expected
end


Loading…
Cancel
Save