logo

auto_linker

AutoLinker-shim, based on https://git.pleroma.social/pleroma/auto_linker git clone https://hacktivis.me/git/auto_linker.git

linkify.ex (3121B)


  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("http://google.com")
  8. ~s(<a href="http://google.com">http://google.com</a>)
  9. iex> Linkify.link("http://google.com", new_window: true, rel: "noopener noreferrer")
  10. ~s(<a href="http://google.com" target="_blank" rel="noopener noreferrer">http://google.com</a>)
  11. iex> Linkify.link("http://google.com", class: "linkified")
  12. ~s(<a href="http://google.com" class="linkified">http://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 format 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 format a hashtag
  32. * `extra: false` - ignored, moved to detection if `extra_prefixes` is a non-empty list
  33. * `extra_prefixes` - list of prefixes that aren't http(s)://, mailto: or xmpp:
  34. * `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)
  35. * `iodata` - Set to `true` to return iodata as a result, or `:safe` for iodata with linkified anchor tags wrapped in Phoenix.HTML `:safe` tuples (removes need for further sanitization)
  36. * `href_handler: nil` - a custom handler to process a url before it is set as the link href, useful for generating exit links
  37. """
  38. def link(text, opts \\ []) do
  39. parse(text, opts)
  40. end
  41. def link_to_iodata(text, opts \\ []) do
  42. parse(text, Keyword.merge(opts, iodata: true))
  43. end
  44. def link_safe(text, opts \\ []) do
  45. parse(text, Keyword.merge(opts, iodata: :safe))
  46. end
  47. def link_map(text, acc, opts \\ []) do
  48. parse({text, acc}, opts)
  49. end
  50. end