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.

69 lines
3.3 KiB

5 years ago
5 years ago
5 years ago
5 years ago
  1. defmodule AutoLinker 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\" target=\"_blank\" rel=\"noopener noreferrer\">google.com</a>"`
  6. ## Examples
  7. iex> AutoLinker.link("google.com")
  8. ~s(<a href="http://google.com" class="auto-linker" target="_blank" rel="noopener noreferrer">google.com</a>)
  9. iex> AutoLinker.link("google.com", new_window: false, rel: false)
  10. ~s(<a href="http://google.com" class="auto-linker">google.com</a>)
  11. iex> AutoLinker.link("google.com", new_window: false, rel: false, class: false)
  12. ~s(<a href="http://google.com">google.com</a>)
  13. iex> AutoLinker.link("[Google](http://google.com)", markdown: true, new_window: false, rel: false, class: false)
  14. ~s(<a href='http://google.com'>Google</a>)
  15. iex> AutoLinker.link("[Google Search](http://google.com)", markdown: true)
  16. ~s(<a href='http://google.com' class="auto-linker" target="_blank" rel="noopener noreferrer">Google Search</a>)
  17. """
  18. import AutoLinker.Parser
  19. @doc """
  20. Auto link a string.
  21. Options:
  22. * `class: "auto-linker"` - specify the class to be added to the generated link. false to clear
  23. * `rel: "noopener noreferrer"` - override the rel attribute. false to clear
  24. * `new_window: true` - set to false to remove `target='_blank'` attribute
  25. * `scheme: false` - Set to true to link urls with schema `http://google`
  26. * `truncate: false` - Set to a number to truncate urls longer then the number. Truncated urls will end in `..`
  27. * `strip_prefix: true` - Strip the scheme prefix
  28. * `exclude_class: false` - Set to a class name when you don't want urls auto linked in the html of the give class
  29. * `exclude_id: false` - Set to an element id when you don't want urls auto linked in the html of the give element
  30. * `exclude_patterns: ["```"]` - Don't link anything between the the pattern
  31. * `markdown: false` - link markdown style links
  32. * `email: false` - link email links
  33. * `mention: false` - link @mentions (when `true`, requires `mention_prefix` or `mention_handler` options to be set)
  34. * `mention_prefix: nil` - a prefix to build a link for a mention (example: `https://example.com/user/`)
  35. * `mention_handler: nil` - a custom handler to validate and formart a mention
  36. * `hashtag: false` - link #hashtags (when `true`, requires `hashtag_prefix` or `hashtag_handler` options to be set)
  37. * `hashtag_prefix: nil` - a prefix to build a link for a hashtag (example: `https://example.com/tag/`)
  38. * `hashtag_handler: nil` - a custom handler to validate and formart a hashtag
  39. * `extra: false` - link urls with rarely used schemes (magnet, ipfs, irc, etc.)
  40. * `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)
  41. Each of the above options can be specified when calling `link(text, opts)`
  42. or can be set in the `:auto_linker`'s configuration. For example:
  43. config :auto_linker,
  44. class: false,
  45. new_window: false
  46. Note that passing opts to `link/2` will override the configuration settings.
  47. """
  48. def link(text, opts \\ []) do
  49. parse(text, opts)
  50. end
  51. def link_map(text, acc, opts \\ []) do
  52. parse({text, acc}, opts)
  53. end
  54. end