You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
2.4 KiB

5 years ago
5 years ago
  1. defmodule Linkify do
  2. @moduledoc """
  3. Create url links from text containing urls.
  4. Turns an input string like `"Check out google.com"` into
  5. `Check out "<a href=\"http://google.com\">google.com</a>"`
  6. ## Examples
  7. iex> Linkify.link("google.com")
  8. ~s(<a href="http://google.com">google.com</a>)
  9. iex> Linkify.link("google.com", new_window: true, rel: "noopener noreferrer")
  10. ~s(<a href="http://google.com" target="_blank" rel="noopener noreferrer">google.com</a>)
  11. iex> Linkify.link("google.com", class: "linkified")
  12. ~s(<a href="http://google.com" class="linkified">google.com</a>)
  13. """
  14. import Linkify.Parser
  15. @doc """
  16. Finds links and turns them into HTML `<a>` tag.
  17. Options:
  18. * `class` - specify the class to be added to the generated link.
  19. * `rel` - specify the rel attribute.
  20. * `new_window` - set to `true` to add `target="_blank"` attribute
  21. * `truncate` - Set to a number to truncate urls longer then the number. Truncated urls will end in `...`
  22. * `strip_prefix` - Strip the scheme prefix (default: `false`)
  23. * `exclude_class` - Set to a class name when you don't want urls auto linked in the html of the give class (default: `false`)
  24. * `exclude_id` - Set to an element id when you don't want urls auto linked in the html of the give element (default: `false`)
  25. * `email` - link email links (default: `false`)
  26. * `mention` - link @mentions (when `true`, requires `mention_prefix` or `mention_handler` options to be set) (default: `false`)
  27. * `mention_prefix` - a prefix to build a link for a mention (example: `https://example.com/user/`, default: `nil`)
  28. * `mention_handler` - a custom handler to validate and formart a mention (default: `nil`)
  29. * `hashtag: false` - link #hashtags (when `true`, requires `hashtag_prefix` or `hashtag_handler` options to be set)
  30. * `hashtag_prefix: nil` - a prefix to build a link for a hashtag (example: `https://example.com/tag/`)
  31. * `hashtag_handler: nil` - a custom handler to validate and formart a hashtag
  32. * `extra: false` - link urls with rarely used schemes (magnet, ipfs, irc, etc.)
  33. * `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)
  34. """
  35. def link(text, opts \\ []) do
  36. parse(text, opts)
  37. end
  38. def link_map(text, acc, opts \\ []) do
  39. parse({text, acc}, opts)
  40. end
  41. end