logo

auto_linker

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

auto_linker.ex (3332B)


      1 defmodule AutoLinker do
      2   @moduledoc """
      3   Create url links from text containing urls.
      4 
      5   Turns an input string like `"Check out google.com"` into
      6   `Check out "<a href=\"http://google.com\" target=\"_blank\" rel=\"noopener noreferrer\">google.com</a>"`
      7 
      8   ## Examples
      9 
     10       iex> AutoLinker.link("http://google.com")
     11       ~s(<a href="http://google.com" class="auto-linker" target="_blank" rel="noopener noreferrer">http://google.com</a>)
     12 
     13       iex> AutoLinker.link("http://google.com", new_window: false, rel: false)
     14       ~s(<a href="http://google.com" class="auto-linker">http://google.com</a>)
     15 
     16       iex> AutoLinker.link("http://google.com", new_window: false, rel: false, class: false)
     17       ~s(<a href="http://google.com">http://google.com</a>)
     18 
     19       iex> AutoLinker.link("[Google](http://google.com)", markdown: true, new_window: false, rel: false, class: false)
     20       ~s(<a href='http://google.com'>Google</a>)
     21 
     22       iex> AutoLinker.link("[Google Search](http://google.com)", markdown: true)
     23       ~s(<a href='http://google.com' class="auto-linker" target="_blank" rel="noopener noreferrer">Google Search</a>)
     24   """
     25 
     26   import AutoLinker.Parser
     27 
     28   @doc """
     29   Auto link a string.
     30 
     31   Options:
     32 
     33   * `class: "auto-linker"` - specify the class to be added to the generated link. false to clear
     34   * `rel: "noopener noreferrer"` - override the rel attribute. false to clear
     35   * `new_window: true` - set to false to remove `target='_blank'` attribute
     36   * `scheme: false` - Set to true to link urls with schema `http://google`
     37   * `truncate: false` - Set to a number to truncate urls longer then the number. Truncated urls will end in `..`
     38   * `strip_prefix: false` - Strip the scheme prefix
     39   * `exclude_class: false` - Set to a class name when you don't want urls auto linked in the html of the give class
     40   * `exclude_id: false` - Set to an element id when you don't want urls auto linked in the html of the give element
     41   * `exclude_patterns: ["```"]` - Don't link anything between the the pattern
     42   * `markdown: false` - link markdown style links
     43   * `email: false` - ignored
     44   * `mention: false` - link @mentions (when `true`, requires `mention_prefix` or `mention_handler` options to be set)
     45   * `mention_prefix: nil` - a prefix to build a link for a mention (example: `https://example.com/user/`)
     46   * `mention_handler: nil` - a custom handler to validate and formart a mention
     47   * `hashtag: false` - link #hashtags (when `true`, requires `hashtag_prefix` or `hashtag_handler` options to be set)
     48   * `hashtag_prefix: nil` - a prefix to build a link for a hashtag (example: `https://example.com/tag/`)
     49   * `hashtag_handler: nil` - a custom handler to validate and formart a hashtag
     50   * `extra: false` - ignored, moved to detection if `extra_prefixes` is a non-empty list
     51   * `extra_prefixes: ["magnet:?", …]` - list of prefixes that aren't http(s)://, mailto: or xmpp:
     52 
     53   Each of the above options can be specified when calling `link(text, opts)`
     54   or can be set in the `:auto_linker`'s configuration. For example:
     55 
     56        config :auto_linker,
     57          class: false,
     58          new_window: false
     59 
     60   Note that passing opts to `link/2` will override the configuration settings.
     61   """
     62   def link(text, opts \\ []) do
     63     parse(text, opts)
     64   end
     65 
     66   def link_map(text, acc, opts \\ []) do
     67     parse({text, acc}, opts)
     68   end
     69 end