Commit 66f51656 authored by minibikini's avatar minibikini

Do not add `class`, `target` and `rel` attributes by default

parent de6e003b
......@@ -20,24 +20,19 @@ end
The following examples illustrate some examples on how to use the auto linker.
```iex
iex> Linkify.link("google.com")
"<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)
"<a href='http://google.com' class='linkified'>google.com</a>"
```elixir
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")
"<a href=\"http://google.com\">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("google.com", class: "linkified")
"<a href=\"http://google.com\" class=\"linkified\">google.com</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("google.com", new_window: true)
"<a href=\"http://google.com\" target=\"_blank\">google.com</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>)
~s(<a href="http://google.com">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: true, rel: "noopener noreferrer")
~s(<a href="http://google.com" target="_blank" rel="noopener noreferrer">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", 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
......
This diff is collapsed.
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment