class Slack::Notifier::Util::LinkFormatter

def format string, opts={}

def format string, opts={}
  LinkFormatter.new(string, **opts).formatted
end

def formatted

rubocop:disable Lint/RescueWithoutErrorClass
def formatted
  return @orig unless @orig.respond_to?(:gsub)
  sub_markdown_links(sub_html_links(@orig))
rescue => e
  raise e unless RUBY_VERSION < "2.1" && e.message.include?("invalid byte sequence")
  raise e, "#{e.message}. Consider including the 'string-scrub' gem to strip invalid characters"
end

def initialize string, formats: %i[html markdown]

def initialize string, formats: %i[html markdown]
  @formats = formats
  @orig    = string.respond_to?(:scrub) ? string.scrub : string
end

def slack_link link, text=nil

def slack_link link, text=nil
  "<#{link}" \
  "#{text && !text.empty? ? "|#{text}" : ''}" \
  ">"
end

def sub_html_links string

def sub_html_links string
  return string unless formats.include?(:html)
  string.gsub(HTML_PATTERN) do
    slack_link Regexp.last_match[1], Regexp.last_match[2]
  end
end

def sub_markdown_links string

def sub_markdown_links string
  return string unless formats.include?(:markdown)
  string.gsub(MARKDOWN_PATTERN) do
    slack_link Regexp.last_match[2], Regexp.last_match[1]
  end
end